From 47f92b2d8341f3f6f1ff5f49c846d4ff091c412b Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Sat, 8 Oct 2022 13:34:09 +0200 Subject: [PATCH] Allow editing threads --- db/sqlite.py | 24 ++++++++++++++++++++++++ main.py | 28 ++++++++++++++++++++++++++++ templates/comment.html | 1 + templates/edit_thread.html | 17 +++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 templates/edit_thread.html diff --git a/db/sqlite.py b/db/sqlite.py index 1d1561b..4598ffd 100644 --- a/db/sqlite.py +++ b/db/sqlite.py @@ -67,6 +67,15 @@ class DB: (thread_id,) ).fetchone() + def get_thread_title_text(self, thread_id): + return self._db().execute(''' + select title, text + from threads + where thread_id = ? + ''', + (thread_id,) + ).fetchone() + def get_recent_threads(self, limit): return self._db().execute(''' select thread_id, title, modify_date @@ -244,5 +253,20 @@ class DB: return True return False + def modify_thread(self, thread_id, user_id, title, text, time): + db = self._db() + c = db.cursor() + c.execute(''' + update threads + set title = ?, text = ?, modify_time = ? + where thread_id = ? and author_id = ? + ''', + (title, text, time, thread_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 8a5453a..60f1314 100644 --- a/main.py +++ b/main.py @@ -201,6 +201,34 @@ def delete_comment(comment_id): # TODO return 403, maybe? return redirect(url_for('index')) +@app.route('/thread//edit', methods = ['GET', 'POST']) +def edit_thread(thread_id): + user_id = session.get('user_id') + if user_id is None: + return redirect(url_for('login')) + + if request.method == 'POST': + if db.modify_thread( + thread_id, + user_id, + request.form['title'], + request.form['text'].replace('\r', ''), + time.time_ns(), + ): + flash('Thread has been edited', 'success') + else: + flash('Thread could not be edited', 'error') + return redirect(url_for('thread', thread_id = thread_id)) + + title, text = db.get_thread_title_text(thread_id) + + return render_template( + 'edit_thread.html', + title = 'Edit thread', + thread_title = title, + text = text, + ) + class Comment: def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id): diff --git a/templates/comment.html b/templates/comment.html index 8b64716..08b9529 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -22,6 +22,7 @@

{{- author(name, ctime, mtime) -}} {%- if author_id == session.get('user_id') -%} + edit {%- endif -%} {%- if author_id == session.get('user_id') -%} delete diff --git a/templates/edit_thread.html b/templates/edit_thread.html new file mode 100644 index 0000000..8eba96e --- /dev/null +++ b/templates/edit_thread.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block content %} +

+ + + + + + + + + +
Title
Text
+

+
+{% endblock %}