mostly implement topics app

This commit is contained in:
Lera Elvoé 2025-06-30 19:50:57 +03:00
parent 19bf98f5b5
commit 453aeff95a
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
5 changed files with 81 additions and 4 deletions

View File

@ -69,3 +69,44 @@ def topic(slug):
current_page = page,
page_count = page_count
)
@bp.get("/<slug>/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("/<slug>/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("/<slug>/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"))

View File

@ -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")

View File

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block title %}creating a topic{% endblock %}
{% block content %}
<div class="darkbg settings-container">
<h1>Editing topic {{ topic['name'] }}</h1>
<form method="post">
<label for=name>Name</label>
<input type="text" name="name" id="name" required value="{{ topic['name'] }}"><br>
<label for="description">Description</label>
<textarea id="description" name="description" required rows=5>{{ topic['description'] }}</textarea><br>
<input type="submit" value="Save changes">
<a class="linkbutton warn" href={{ url_for("topics.topic", slug=topic['slug'] )}}>Cancel</a><br>
<i> Note: to preserve history, you cannot change the topic URL.</i>
</form>
</div>
{% endblock %}

View File

@ -7,6 +7,12 @@
<span>{{topic['description']}}</span>
<div>
{% if active_user and active_user.is_mod() %}
<a class="linkbutton" href="{{url_for("topics.edit", slug=topic['slug'])}}">Edit topic</a>
<form class="modform" method="post" action="{{url_for("topics.edit", slug=topic['slug']) }}">
<input type="hidden" name="is_locked" value="{{ (not topic.is_locked) | int }}">
<input class="warn" type="submit" id="lock" value="{{"Unlock topic" if topic['is_locked'] else "Lock topic"}}">
</form>
<button type="button" class="critical" id="topic-delete-dialog-open">Delete</button>
{% endif %}
</div>
</nav>
@ -52,4 +58,17 @@
<nav id="bottomnav">
{{ pager(current_page = current_page, page_count = page_count) }}
</nav>
<dialog id="delete-dialog">
<div class="delete-dialog-inner">
Are you sure you want to delete this topic?
<span>
<button id=topic-delete-dialog-close>Cancel</button>
<button class="critical" form=topic-delete-form>Delete</button>
<form id="topic-delete-form" method="post" action="{{ url_for("topics.delete", slug = topic.slug) }}"></form>
</span>
</div>
</dialog>
<script src="/static/js/topic.js"></script>
{% endblock %}

View File

@ -1,3 +1,4 @@
{% from 'common/macros.html' import timestamp %}
{% extends "base.html" %}
{% block content %}
<nav class="darkbg">
@ -16,12 +17,12 @@
{{ topic['description'] }}
{% if topic['latest_thread_username'] %}
<span>
Latest thread: <a href="{{ url_for("threads.thread", slug=topic['latest_thread_slug'])}}">{{topic['latest_thread_title']}}</a> by <a href="{{url_for("users.page", username=topic['latest_thread_username'])}}">{{topic['latest_thread_username']}}</a> on ...
Latest thread: <a href="{{ url_for("threads.thread", slug=topic['latest_thread_slug'])}}">{{topic['latest_thread_title']}}</a> by <a href="{{url_for("users.page", username=topic['latest_thread_username'])}}">{{topic['latest_thread_username']}}</a> on {{ timestamp(topic['latest_thread_created_at']) }}
</span>
{% if topic['id'] in active_threads %}
{% with thread=active_threads[topic['id']] %}
<span>
Latest post in: <a href="{{ url_for("threads.thread", slug=thread['thread_slug'])}}">{{ thread['thread_title'] }}</a> by <a href="{{ url_for("users.page", username=thread['username'])}}">{{ thread['username'] }}</a> on ...
Latest post in: <a href="{{ url_for("threads.thread", slug=thread['thread_slug'])}}">{{ thread['thread_title'] }}</a> by <a href="{{ url_for("users.page", username=thread['username'])}}">{{ thread['username'] }}</a> at <a href="">{{ timestamp(thread['post_created_at']) }}</a>
</span>
{% endwith %}
{% endif %}