From a0c86f33b4b1b686f1df47bebc609bcbd5679865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 11 Aug 2025 17:46:22 +0300 Subject: [PATCH] finish that a tag in topics view --- app/__init__.py | 7 +++++++ app/routes/threads.py | 14 ++++++++++++++ app/templates/topics/topics.html | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index b487475..7b76095 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,6 +3,7 @@ from dotenv import load_dotenv from .models import Avatars, Users from .auth import digest from .routes.users import is_logged_in, get_active_user +from .routes.threads import get_post_url from .constants import ( PermissionLevel, permission_level_string, InfoboxKind, InfoboxIcons, InfoboxHTMLClass, @@ -116,6 +117,12 @@ def create_app(): def inject_auth(): return {"is_logged_in": is_logged_in, "get_active_user": get_active_user, "active_user": get_active_user()} + @app.context_processor + def inject_funcs(): + return { + 'get_post_url': get_post_url, + } + @app.template_filter("ts_datetime") def ts_datetime(ts, format): return datetime.utcfromtimestamp(ts or int(time.time())).strftime(format) diff --git a/app/routes/threads.py b/app/routes/threads.py index 65ce44c..ab16fb6 100644 --- a/app/routes/threads.py +++ b/app/routes/threads.py @@ -13,6 +13,20 @@ import time bp = Blueprint("threads", __name__, url_prefix = "/threads/") +def get_post_url(post_id, _anchor=False): + post = Posts.find({'id': post_id}) + if not post: + return "" + + thread = Threads.find({'id': post.thread_id}) + + res = url_for('threads.thread', slug=thread.slug, after=post_id) + if not _anchor: + return res + + return f"{res}#post-{post_id}" + + @bp.get("/") def thread(slug): POSTS_PER_PAGE = 10 diff --git a/app/templates/topics/topics.html b/app/templates/topics/topics.html index dd60874..6296d3e 100644 --- a/app/templates/topics/topics.html +++ b/app/templates/topics/topics.html @@ -23,7 +23,7 @@ {% if topic['id'] in active_threads %} {% with thread=active_threads[topic['id']] %} - Latest post in: {{ thread['thread_title'] }} by {{ thread['username'] }} at {{ timestamp(thread['post_created_at']) }} + Latest post in: {{ thread['thread_title'] }} by {{ thread['username'] }} at {{ timestamp(thread['post_created_at']) }} {% endwith %} {% endif %}