From 49dc242db6773cf4169f10bc76bfc00ef04f689d Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Sat, 8 Oct 2022 09:58:00 +0200 Subject: [PATCH] Add links to thread & parent in comment --- main.py | 12 +++++++----- templates/comment.html | 29 ++++++++++++++++++++--------- templates/comments.html | 6 +++--- templates/thread.html | 4 ++-- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 3724474..2aa3733 100644 --- a/main.py +++ b/main.py @@ -38,6 +38,7 @@ def thread(thread_id): title = title, text = text, author = author, + thread_id = thread_id, create_time = create_time, modify_time = modify_time, comments = comments, @@ -177,27 +178,28 @@ def add_comment_parent(comment_id): 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.author = author self.text = text self.children = [] self.create_time = create_time self.modify_time = modify_time + self.parent_id = parent_id 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) + comment_id: Comment(comment_id, author, text, create_time, modify_time, parent_id) for comment_id, parent_id, author, text, create_time, modify_time in comments } root = [] # Build tree - for comment, parent_id in comment_map.values(): - parent = comment_map.get(parent_id) + for comment in comment_map.values(): + parent = comment_map.get(comment.parent_id) if parent is not None: - parent[0].children.append(comment) + parent.children.append(comment) else: root.append(comment) # Sort each comment based on create time diff --git a/templates/comment.html b/templates/comment.html index 6983c9e..395c450 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -1,24 +1,35 @@ -{% macro author(name, ctime, mtime) %} -

{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}

+{% macro author(name, ctime, mtime, thread_id, parent_id) %} +

+ + {{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %} + {% if thread_id is not none %} + {# Suffixing a # prevents unnecessary reloads #} + thread + {% endif %} + {% if parent_id is not none %} + parent + {% endif %} + +

{% endmacro %} -{% macro render_comment_pre(comment) %} +{% macro render_comment_pre(comment, thread_id) %}
- {{ author(comment.author, comment.create_time, comment.modify_time) }} + {{ author(comment.author, comment.create_time, comment.modify_time, thread_id, comment.parent_id) }}

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

{% endmacro %} -{% macro render_comment_post(comment) %} +{% macro render_comment_post(comment, thread_id) %} {% for c in comment.children %} - {{ render_comment(c) }} + {{ render_comment(c, thread_id) }} {% endfor %}
{% endmacro %} -{% macro render_comment(comment) %} -{{ render_comment_pre(comment) }} +{% macro render_comment(comment, thread_id) %} +{{ render_comment_pre(comment, thread_id) }} reply -{{ render_comment_post(comment) }} +{{ render_comment_post(comment, thread_id) }} {% endmacro %} {% macro reply() %} diff --git a/templates/comments.html b/templates/comments.html index 0e197ee..b3309b8 100644 --- a/templates/comments.html +++ b/templates/comments.html @@ -7,14 +7,14 @@ parent {% endif %} -{{ render_comment_pre(reply_comment) }} +{{ render_comment_pre(reply_comment, thread_id) }} {{ reply() }} {% for c in comments %} -{{ render_comment(c) }} +{{ render_comment(c, thread_id) }} {% endfor %} -{{ render_comment_post(reply_comment) }} +{{ render_comment_post(reply_comment, thread_id) }} {% endblock %} diff --git a/templates/thread.html b/templates/thread.html index e53efe1..b3aa699 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -9,12 +9,12 @@ {% endif %} -{{ f_author(author, create_time, modify_time) }} +{{ f_author(author, create_time, modify_time, None, None) }}

{{ minimd(text) | safe }}

{{ reply() }} {% for c in comments %} -{{ render_comment(c) }} +{{ render_comment(c, thread_id) }} {% endfor %} {% endblock %}