Add delete button to comments
This commit is contained in:
26
db/sqlite.py
26
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()
|
||||
|
||||
33
main.py
33
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/<int:comment_id>/')
|
||||
@@ -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/<int:comment_id>/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/<int:comment_id>/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):
|
||||
|
||||
@@ -1,13 +1,33 @@
|
||||
{% macro author(name, ctime, mtime, thread_id, parent_id) %}
|
||||
{% macro author(name, ctime, mtime) %}
|
||||
<i>{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro comment_author(comment, thread_id) %}
|
||||
<p>
|
||||
<sub>
|
||||
<i>{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i>
|
||||
{% if thread_id is not none %}
|
||||
{{ author(comment.author, comment.create_time, comment.modify_time) }}
|
||||
{# Suffixing a # prevents unnecessary reloads #}
|
||||
<a href="{{ url_for('thread', thread_id = thread_id) }}#">thread</a>
|
||||
{% if comment.parent_id is not none %}
|
||||
<a href="{{ url_for('comment', comment_id = comment.parent_id) }}#">parent</a>
|
||||
{% endif %}
|
||||
{% if parent_id is not none %}
|
||||
<a href="{{ url_for('comment', comment_id = parent_id) }}#">parent</a>
|
||||
{% if comment.author_id == session.get('user_id') %}
|
||||
{% endif %}
|
||||
{% if comment.author_id == session.get('user_id') %}
|
||||
<a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}">delete</a>
|
||||
{% endif %}
|
||||
</sub>
|
||||
</p>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro thread_author(author_id, name, ctime, mtime) %}
|
||||
<p>
|
||||
<sub>
|
||||
{{ author(name, ctime, mtime) }}
|
||||
{% if author_id == session.get('user_id') %}
|
||||
{% endif %}
|
||||
{% if author_id == session.get('user_id') %}
|
||||
<a href="{{ url_for('confirm_delete_thread', thread_id = thread_id) }}">delete</a>
|
||||
{% endif %}
|
||||
</sub>
|
||||
</p>
|
||||
@@ -15,7 +35,7 @@
|
||||
|
||||
{% macro render_comment_pre(comment, thread_id) %}
|
||||
<div class=comment>
|
||||
{{ author(comment.author, comment.create_time, comment.modify_time, thread_id, comment.parent_id) }}
|
||||
{{ comment_author(comment, thread_id) }}
|
||||
<p>{{ minimd(comment.text) | safe }}</p>
|
||||
{% endmacro %}
|
||||
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
{% from 'comment.html' import render_comment, render_comment_pre, render_comment_post, reply with context %}
|
||||
|
||||
{% block content %}
|
||||
<sup><a href="{{ url_for('thread', thread_id = thread_id) }}">thread</a></sup>
|
||||
{% if parent_id %}
|
||||
<sup><a href="../{{ parent_id }}">parent</a></sup>
|
||||
{% endif %}
|
||||
|
||||
{{ render_comment_pre(reply_comment, thread_id) }}
|
||||
|
||||
|
||||
14
templates/confirm_delete_comment.html
Normal file
14
templates/confirm_delete_comment.html
Normal file
@@ -0,0 +1,14 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<p>Are you sure you want to delete this comment on "{{ thread_title }}"?</p>
|
||||
<div class=comment>{{ minimd(text) | safe }}</div>
|
||||
<p>
|
||||
<form method="post" action="../delete" style=inline>
|
||||
<input type="submit" value="Yes">
|
||||
</form>
|
||||
<form method="get" action=".." style=inline>
|
||||
<input type="submit" value="No">
|
||||
</form>
|
||||
</p>
|
||||
{% endblock %}
|
||||
@@ -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 %}
|
||||
<div>
|
||||
<form method="get" action="confirm_delete/">
|
||||
<input type="submit" value="Delete">
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
<i>{{ f_author(author, create_time, modify_time, None, None) }}</i>
|
||||
<i>{{ thread_author(author_id, author, create_time, modify_time) }}</i>
|
||||
<p>{{ minimd(text) | safe }}</p>
|
||||
|
||||
{{ reply() }}
|
||||
|
||||
Reference in New Issue
Block a user