From 453aeff95a57e5a1d432360d1a09d033c8ee78c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 30 Jun 2025 19:50:57 +0300 Subject: [PATCH] mostly implement topics app --- app/routes/topics.py | 41 ++++++++++++++++++++++++++++++++ app/routes/users.py | 4 ++-- app/templates/topics/edit.html | 16 +++++++++++++ app/templates/topics/topic.html | 19 +++++++++++++++ app/templates/topics/topics.html | 5 ++-- 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 app/templates/topics/edit.html diff --git a/app/routes/topics.py b/app/routes/topics.py index a49bb84..dcfaca6 100644 --- a/app/routes/topics.py +++ b/app/routes/topics.py @@ -69,3 +69,44 @@ def topic(slug): current_page = page, page_count = page_count ) + + +@bp.get("//edit") +@login_required +@mod_only(".topic", slug = lambda slug: slug) +def edit(slug): + topic = Topics.find({"slug": slug}) + if not topic: + return "no" + return render_template("topics/edit.html", topic=topic) + + +@bp.post("//edit") +@login_required +@mod_only(".topic", slug = lambda slug: slug) +def edit_post(slug): + topic = Topics.find({"slug": slug}) + if not topic: + return "no" + + topic.update({ + "name": request.form.get('name', default = topic.name).strip(), + "description": request.form.get('description', default = topic.description), + "is_locked": int(request.form.get("is_locked", default = topic.is_locked)), + }) + + return redirect(url_for("topics.topic", slug=slug)) + + +@bp.post("//delete") +@login_required +@mod_only(".topic", slug = lambda slug: slug) +def delete(slug): + topic = Topics.find({"slug": slug}) + if not topic: + return "no" + + topic.delete() + + flash("Topic deleted.", InfoboxKind.INFO) + return redirect(url_for("topics.all_topics")) diff --git a/app/routes/users.py b/app/routes/users.py index 83abbd5..3464681 100644 --- a/app/routes/users.py +++ b/app/routes/users.py @@ -50,7 +50,7 @@ def redirect_if_logged_in(*args, **kwargs): if is_logged_in(): # resolve callables processed_kwargs = { - k: v() if callable(v) else v + k: v(**view_kwargs) if callable(v) else v for k, v in kwargs.items() } endpoint = args[0] if args else processed_kwargs.get("endpoint") @@ -81,7 +81,7 @@ def mod_only(*args, **kwargs): if not get_active_user().is_mod(): # resolve callables processed_kwargs = { - k: v() if callable(v) else v + k: v(**view_kwargs) if callable(v) else v for k, v in kwargs.items() } endpoint = args[0] if args else processed_kwargs.get("endpoint") diff --git a/app/templates/topics/edit.html b/app/templates/topics/edit.html new file mode 100644 index 0000000..bf91575 --- /dev/null +++ b/app/templates/topics/edit.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% block title %}creating a topic{% endblock %} +{% block content %} +
+

Editing topic {{ topic['name'] }}

+
+ +
+ +
+ + Cancel
+ Note: to preserve history, you cannot change the topic URL. +
+
+{% endblock %} diff --git a/app/templates/topics/topic.html b/app/templates/topics/topic.html index 2c95f34..ca4c59d 100644 --- a/app/templates/topics/topic.html +++ b/app/templates/topics/topic.html @@ -7,6 +7,12 @@ {{topic['description']}}
{% if active_user and active_user.is_mod() %} + Edit topic +
+ + +
+ {% endif %}
@@ -52,4 +58,17 @@ + + +
+ Are you sure you want to delete this topic? + + + +
+
+
+
+ + {% endblock %} diff --git a/app/templates/topics/topics.html b/app/templates/topics/topics.html index 6379d6b..58473cd 100644 --- a/app/templates/topics/topics.html +++ b/app/templates/topics/topics.html @@ -1,3 +1,4 @@ +{% from 'common/macros.html' import timestamp %} {% extends "base.html" %} {% block content %}