From 9f74e1a2af01557f6be815920806e13fa2801967 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Sat, 8 Oct 2022 11:00:20 +0200 Subject: [PATCH] Add delete button to comments --- db/sqlite.py | 26 +++++++++++++++++++-- main.py | 33 +++++++++++++++++++++++---- templates/comment.html | 32 +++++++++++++++++++++----- templates/comments.html | 4 ---- templates/confirm_delete_comment.html | 14 ++++++++++++ templates/thread.html | 11 ++------- 6 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 templates/confirm_delete_comment.html diff --git a/db/sqlite.py b/db/sqlite.py index b88dcef..1617946 100644 --- a/db/sqlite.py +++ b/db/sqlite.py @@ -50,7 +50,7 @@ class DB: (thread,) ).fetchone() comments = db.execute(''' - select comment_id, parent_id, name, text, create_time, modify_time + select comment_id, parent_id, author_id, name, text, create_time, modify_time from comments, users where thread_id = ? and author_id = user_id ''', @@ -86,6 +86,15 @@ class DB: (thread,) ) + def get_comment(self, comment_id): + return self._db().execute(''' + select title, c.text + from comments c, threads t + where comment_id = ? and c.thread_id = t.thread_id + ''', + (comment_id,) + ).fetchone() + def get_subcomments(self, comment_id): db = self._db() thread_id, parent_id, title = db.execute(''' @@ -103,7 +112,7 @@ class DB: union select comment_id from descendant_of, comments where id = parent_id ) - select id, parent_id, name, text, create_time, modify_time from descendant_of, comments, users + select id, parent_id, author_id, name, text, create_time, modify_time from descendant_of, comments, users where id = comment_id and user_id = author_id ''', (comment_id,) @@ -180,6 +189,19 @@ class DB: db.commit() return c.rowcount > 0 + def delete_comment(self, user_id, comment_id): + db = self._db() + c = db.cursor() + c.execute(''' + delete + from comments + where comment_id = ? and author_id = ? + ''', + (comment_id, user_id) + ) + db.commit() + return c.rowcount > 0 + def add_comment_to_thread(self, thread_id, author_id, text, time): db = self._db() c = db.cursor() diff --git a/main.py b/main.py index 2aa3733..d79a74b 100644 --- a/main.py +++ b/main.py @@ -38,11 +38,11 @@ def thread(thread_id): title = title, text = text, author = author, + author_id = author_id, thread_id = thread_id, create_time = create_time, modify_time = modify_time, comments = comments, - manage = author_id == user_id, ) @app.route('/comment//') @@ -176,10 +176,34 @@ def add_comment_parent(comment_id): flash('Failed to add comment', 'error') return redirect(url_for('comment', comment_id = comment_id)) +@app.route('/comment//confirm_delete/') +def confirm_delete_comment(comment_id): + title, text = db.get_comment(comment_id) + return render_template( + 'confirm_delete_comment.html', + title = 'Delete comment', + thread_title = title, + text = text, + ) + +@app.route('/comment//delete/', methods = ['POST']) +def delete_comment(comment_id): + user_id = session.get('user_id') + if user_id is None: + return redirect(url_for('login')) + + if db.delete_comment(user_id, comment_id): + flash('Comment has been deleted', 'success') + else: + flash('Comment could not be removed', 'error') + # TODO return 403, maybe? + return redirect(url_for('index')) + class Comment: - def __init__(self, id, author, text, create_time, modify_time, parent_id): + def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id): self.id = id + self.author_id = author_id self.author = author self.text = text self.children = [] @@ -190,8 +214,8 @@ class Comment: def create_comment_tree(comments): # Collect comments first, then build the tree in case we encounter a child before a parent comment_map = { - comment_id: Comment(comment_id, author, text, create_time, modify_time, parent_id) - for comment_id, parent_id, author, text, create_time, modify_time + comment_id: Comment(comment_id, author_id, author, text, create_time, modify_time, parent_id) + for comment_id, parent_id, author_id, author, text, create_time, modify_time in comments } root = [] @@ -210,6 +234,7 @@ def create_comment_tree(comments): sort_time(root) return root + @app.context_processor def utility_processor(): def format_since(t): diff --git a/templates/comment.html b/templates/comment.html index 395c450..2ece02d 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -1,13 +1,33 @@ -{% macro author(name, ctime, mtime, thread_id, parent_id) %} +{% macro author(name, ctime, mtime) %} +{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %} +{% endmacro %} + +{% macro comment_author(comment, thread_id) %}

- {{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %} - {% if thread_id is not none %} + {{ author(comment.author, comment.create_time, comment.modify_time) }} {# Suffixing a # prevents unnecessary reloads #} thread + {% if comment.parent_id is not none %} + parent {% endif %} - {% if parent_id is not none %} - parent + {% if comment.author_id == session.get('user_id') %} + {% endif %} + {% if comment.author_id == session.get('user_id') %} + delete + {% endif %} + +

+{% endmacro %} + +{% macro thread_author(author_id, name, ctime, mtime) %} +

+ + {{ author(name, ctime, mtime) }} + {% if author_id == session.get('user_id') %} + {% endif %} + {% if author_id == session.get('user_id') %} + delete {% endif %}

@@ -15,7 +35,7 @@ {% macro render_comment_pre(comment, thread_id) %}
- {{ author(comment.author, comment.create_time, comment.modify_time, thread_id, comment.parent_id) }} + {{ comment_author(comment, thread_id) }}

{{ minimd(comment.text) | safe }}

{% endmacro %} diff --git a/templates/comments.html b/templates/comments.html index b3309b8..ff864f9 100644 --- a/templates/comments.html +++ b/templates/comments.html @@ -2,10 +2,6 @@ {% from 'comment.html' import render_comment, render_comment_pre, render_comment_post, reply with context %} {% block content %} -thread -{% if parent_id %} -parent -{% endif %} {{ render_comment_pre(reply_comment, thread_id) }} diff --git a/templates/confirm_delete_comment.html b/templates/confirm_delete_comment.html new file mode 100644 index 0000000..7e1c506 --- /dev/null +++ b/templates/confirm_delete_comment.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} + +{% block content %} +

Are you sure you want to delete this comment on "{{ thread_title }}"?

+
{{ minimd(text) | safe }}
+

+

+ +
+
+ +
+

+{% endblock %} diff --git a/templates/thread.html b/templates/thread.html index b3aa699..60d5902 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -1,15 +1,8 @@ {% extends 'base.html' %} -{% from 'comment.html' import render_comment, reply, author as f_author with context %} +{% from 'comment.html' import render_comment, reply, thread_author with context %} {% block content %} -{% if manage %} -
-
- -
-
-{% endif %} -{{ f_author(author, create_time, modify_time, None, None) }} +{{ thread_author(author_id, author, create_time, modify_time) }}

{{ minimd(text) | safe }}

{{ reply() }}