From bba6bee92ca9940aae585151c612902d0ec15d92 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Sat, 8 Oct 2022 14:11:28 +0200 Subject: [PATCH] Allow editing comments --- db/sqlite.py | 15 +++++++++++++++ main.py | 29 ++++++++++++++++++++++++++++- templates/comment.html | 1 + templates/edit_comment.html | 17 +++++++++++++++++ templates/thread.html | 2 +- 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 templates/edit_comment.html diff --git a/db/sqlite.py b/db/sqlite.py index 4598ffd..407d885 100644 --- a/db/sqlite.py +++ b/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) diff --git a/main.py b/main.py index 60f1314..4f8ac59 100644 --- a/main.py +++ b/main.py @@ -229,6 +229,33 @@ def edit_thread(thread_id): text = text, ) +@app.route('/comment//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'
{l}
', paragraphs) paragraphs = map(lambda l: f'

{l}

', paragraphs) # Glue together again diff --git a/templates/comment.html b/templates/comment.html index 08b9529..4e510be 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -11,6 +11,7 @@ parent {%- endif -%} {%- if comment.author_id == session.get('user_id') -%} + edit {%- endif -%} {%- if comment.author_id == session.get('user_id') -%} delete diff --git a/templates/edit_comment.html b/templates/edit_comment.html new file mode 100644 index 0000000..438cf8c --- /dev/null +++ b/templates/edit_comment.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block content %} +
+ + + + + + + + + +
Thread{{ thread_title }}
Text
+

+
+{% endblock %} diff --git a/templates/thread.html b/templates/thread.html index 0000bae..80a6c62 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -2,7 +2,7 @@ {%- from 'comment.html' import render_comment, reply, thread_author with context %} {%- block content %} -{{ thread_author(author_id, author, create_time, modify_time) }} +{{ thread_author(author_id, author, create_time, modify_time) }}

{{ minimd(text) | safe }}

{{- reply() }}