Add links to thread & parent in comment

This commit is contained in:
David Hoppenbrouwers
2022-10-08 09:58:00 +02:00
parent 0d0135b5be
commit 49dc242db6
4 changed files with 32 additions and 19 deletions

12
main.py
View File

@@ -38,6 +38,7 @@ def thread(thread_id):
title = title, title = title,
text = text, text = text,
author = author, author = author,
thread_id = thread_id,
create_time = create_time, create_time = create_time,
modify_time = modify_time, modify_time = modify_time,
comments = comments, comments = comments,
@@ -177,27 +178,28 @@ def add_comment_parent(comment_id):
class Comment: class Comment:
def __init__(self, id, author, text, create_time, modify_time): def __init__(self, id, author, text, create_time, modify_time, parent_id):
self.id = id self.id = id
self.author = author self.author = author
self.text = text self.text = text
self.children = [] self.children = []
self.create_time = create_time self.create_time = create_time
self.modify_time = modify_time self.modify_time = modify_time
self.parent_id = parent_id
def create_comment_tree(comments): def create_comment_tree(comments):
# Collect comments first, then build the tree in case we encounter a child before a parent # Collect comments first, then build the tree in case we encounter a child before a parent
comment_map = { comment_map = {
comment_id: (Comment(comment_id, author, text, create_time, modify_time), parent_id) comment_id: Comment(comment_id, author, text, create_time, modify_time, parent_id)
for comment_id, parent_id, author, text, create_time, modify_time for comment_id, parent_id, author, text, create_time, modify_time
in comments in comments
} }
root = [] root = []
# Build tree # Build tree
for comment, parent_id in comment_map.values(): for comment in comment_map.values():
parent = comment_map.get(parent_id) parent = comment_map.get(comment.parent_id)
if parent is not None: if parent is not None:
parent[0].children.append(comment) parent.children.append(comment)
else: else:
root.append(comment) root.append(comment)
# Sort each comment based on create time # Sort each comment based on create time

View File

@@ -1,24 +1,35 @@
{% macro author(name, ctime, mtime) %} {% macro author(name, ctime, mtime, thread_id, parent_id) %}
<p><sub><i>{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i></sub></p> <p>
<sub>
<i>{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i>
{% if thread_id is not none %}
{# Suffixing a # prevents unnecessary reloads #}
<a href="{{ url_for('thread', thread_id = thread_id) }}#">thread</a>
{% endif %}
{% if parent_id is not none %}
<a href="{{ url_for('comment', comment_id = parent_id) }}#">parent</a>
{% endif %}
</sub>
</p>
{% endmacro %} {% endmacro %}
{% macro render_comment_pre(comment) %} {% macro render_comment_pre(comment, thread_id) %}
<div class=comment> <div class=comment>
{{ author(comment.author, comment.create_time, comment.modify_time) }} {{ author(comment.author, comment.create_time, comment.modify_time, thread_id, comment.parent_id) }}
<p>{{ minimd(comment.text) | safe }}</p> <p>{{ minimd(comment.text) | safe }}</p>
{% endmacro %} {% endmacro %}
{% macro render_comment_post(comment) %} {% macro render_comment_post(comment, thread_id) %}
{% for c in comment.children %} {% for c in comment.children %}
{{ render_comment(c) }} {{ render_comment(c, thread_id) }}
{% endfor %} {% endfor %}
</div> </div>
{% endmacro %} {% endmacro %}
{% macro render_comment(comment) %} {% macro render_comment(comment, thread_id) %}
{{ render_comment_pre(comment) }} {{ render_comment_pre(comment, thread_id) }}
<sup><a href="{{ url_for("comment", comment_id = comment.id) }}">reply</a></sup> <sup><a href="{{ url_for("comment", comment_id = comment.id) }}">reply</a></sup>
{{ render_comment_post(comment) }} {{ render_comment_post(comment, thread_id) }}
{% endmacro %} {% endmacro %}
{% macro reply() %} {% macro reply() %}

View File

@@ -7,14 +7,14 @@
<sup><a href="../{{ parent_id }}">parent</a></sup> <sup><a href="../{{ parent_id }}">parent</a></sup>
{% endif %} {% endif %}
{{ render_comment_pre(reply_comment) }} {{ render_comment_pre(reply_comment, thread_id) }}
{{ reply() }} {{ reply() }}
{% for c in comments %} {% for c in comments %}
{{ render_comment(c) }} {{ render_comment(c, thread_id) }}
{% endfor %} {% endfor %}
{{ render_comment_post(reply_comment) }} {{ render_comment_post(reply_comment, thread_id) }}
{% endblock %} {% endblock %}

View File

@@ -9,12 +9,12 @@
</form> </form>
</div> </div>
{% endif %} {% endif %}
<i>{{ f_author(author, create_time, modify_time) }}</i> <i>{{ f_author(author, create_time, modify_time, None, None) }}</i>
<p>{{ minimd(text) | safe }}</p> <p>{{ minimd(text) | safe }}</p>
{{ reply() }} {{ reply() }}
{% for c in comments %} {% for c in comments %}
{{ render_comment(c) }} {{ render_comment(c, thread_id) }}
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}