Allow editing comments
This commit is contained in:
15
db/sqlite.py
15
db/sqlite.py
@@ -268,5 +268,20 @@ class DB:
|
||||
return True
|
||||
return False
|
||||
|
||||
def modify_comment(self, comment_id, user_id, text, time):
|
||||
db = self._db()
|
||||
c = db.cursor()
|
||||
c.execute('''
|
||||
update comments
|
||||
set text = ?, modify_time = ?
|
||||
where comment_id = ? and author_id = ?
|
||||
''',
|
||||
(text, time, comment_id, user_id)
|
||||
)
|
||||
if c.rowcount > 0:
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
def _db(self):
|
||||
return sqlite3.connect(self.conn)
|
||||
|
||||
29
main.py
29
main.py
@@ -229,6 +229,33 @@ def edit_thread(thread_id):
|
||||
text = text,
|
||||
)
|
||||
|
||||
@app.route('/comment/<int:comment_id>/edit', methods = ['GET', 'POST'])
|
||||
def edit_comment(comment_id):
|
||||
user_id = session.get('user_id')
|
||||
if user_id is None:
|
||||
return redirect(url_for('login'))
|
||||
|
||||
if request.method == 'POST':
|
||||
if db.modify_comment(
|
||||
comment_id,
|
||||
user_id,
|
||||
request.form['text'].replace('\r', ''),
|
||||
time.time_ns(),
|
||||
):
|
||||
flash('Comment has been edited', 'success')
|
||||
else:
|
||||
flash('Comment could not be edited', 'error')
|
||||
return redirect(url_for('comment', comment_id = comment_id))
|
||||
|
||||
title, text = db.get_comment(comment_id)
|
||||
|
||||
return render_template(
|
||||
'edit_comment.html',
|
||||
title = 'Edit comment',
|
||||
thread_title = title,
|
||||
text = text,
|
||||
)
|
||||
|
||||
|
||||
class Comment:
|
||||
def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id):
|
||||
@@ -311,7 +338,7 @@ def utility_processor():
|
||||
# Also replace ampersands to prevent surprises.
|
||||
text = text.replace('&', '&').replace('<', '<').replace('>', '>')
|
||||
# Split into paragraphs
|
||||
paragraphs = text.split('\n\n')
|
||||
paragraphs = map(lambda l: l.strip('\n'), text.split('\n\n'))
|
||||
paragraphs = map(lambda l: l if not l.startswith(' ') else f'<pre>{l}</pre>', paragraphs)
|
||||
paragraphs = map(lambda l: f'<p>{l}</p>', paragraphs)
|
||||
# Glue together again
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<a href="{{ url_for('comment', comment_id = comment.parent_id) }}#"> parent</a>
|
||||
{%- endif -%}
|
||||
{%- if comment.author_id == session.get('user_id') -%}
|
||||
<a href="{{ url_for('edit_comment', comment_id = comment.id) }}"> edit</a>
|
||||
{%- endif -%}
|
||||
{%- if comment.author_id == session.get('user_id') -%}
|
||||
<a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}"> delete</a>
|
||||
|
||||
17
templates/edit_comment.html
Normal file
17
templates/edit_comment.html
Normal file
@@ -0,0 +1,17 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
<table class=form>
|
||||
<tr>
|
||||
<td>Thread</td>
|
||||
<td>{{ thread_title }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Text</td>
|
||||
<td><textarea name="text">{{ text }}</textarea></td>
|
||||
</tr>
|
||||
</table>
|
||||
<p><input type="submit" value="Post"></p>
|
||||
</form>
|
||||
{% endblock %}
|
||||
@@ -2,7 +2,7 @@
|
||||
{%- from 'comment.html' import render_comment, reply, thread_author with context %}
|
||||
|
||||
{%- block content %}
|
||||
<i>{{ thread_author(author_id, author, create_time, modify_time) }}</i>
|
||||
{{ thread_author(author_id, author, create_time, modify_time) }}
|
||||
<p>{{ minimd(text) | safe }}</p>
|
||||
|
||||
{{- reply() }}
|
||||
|
||||
Reference in New Issue
Block a user