forum: enable topic deletion

Topic modification does not work in this snapshot, this is normal.
This commit is contained in:
Lephe 2021-07-12 18:30:31 +02:00 committed by Gitea
parent 9727c2a986
commit b8ed0bba99
4 changed files with 57 additions and 30 deletions

View File

@ -227,6 +227,10 @@ class Member(User):
return self.can_access_post(post) and \
((post.author == self) or self.priv("delete.posts"))
def can_punish_post(self, post):
"""Whether this member can delete the post with penalty."""
return self.can_access_post(post) and self.priv("delete.posts")
def can_set_topcomment(self, comment):
"""Whether this member can designate the comment as top comment."""
if comment.type != "comment":

View File

@ -67,11 +67,16 @@ def edit_post(postid):
@login_required
@check_csrf
def delete_post(postid):
next_page = request.referrer
p = Post.query.filter_by(id=postid).first_or_404()
if current_user.is_anonymous or not current_user.can_delete_post(p):
abort(403)
# When deleting topics, return to forum page
if isinstance(p, Topic):
next_page = url_for('forum_page', f=p.forum)
if isinstance(p.author, Member):
amount = -3 if request.args.get('penalty') == 'True' else -1
p.author.add_xp(amount)
@ -79,7 +84,7 @@ def delete_post(postid):
p.delete()
db.session.commit()
return redirect(request.referrer)
return redirect(next_page)
@app.route('/post/entete/<int:postid>', methods=['GET'])
@login_required

View File

@ -3,6 +3,7 @@
{% import "widgets/thread.html" as widget_thread with context %}
{% import "widgets/user.html" as widget_user %}
{% import "widgets/pagination.html" as widget_pagination with context %}
{% import "widgets/attachments.html" as widget_attachments %}
{% block title %}
<a href='/forum'>Forum de Planète Casio</a> » <a href="{{ url_for('forum_page', f=t.forum) }}">{{ t.forum.name }}</a> » <h1>{{ t.title }}</h1>
@ -13,7 +14,12 @@
<h1>{{ t.title }}</h1>
{% call widget_thread.thread_leader(t.thread.top_comment) %}
<div class="info">
<div>Posté le {{ t.date_created | dyndate }}</div>
{{ widget_thread.post_actions(t) }}
</div>
{{ t.thread.top_comment.text | md }}
{{ widget_attachments.attachments(t.thread.top_comment) }}
{% endcall %}
{{ widget_pagination.paginate(comments, 'forum_topic', t, {'f': t.forum}) }}

View File

@ -1,6 +1,46 @@
{% import "widgets/user.html" as widget_user %}
{% import "widgets/attachments.html" as widget_attachments %}
{# Post actions: this widget expands to a context menu with actions controlling
a post, supporting different types of posts. #}
{% macro post_actions(post) %}
{# TODO (Guest edit): determine permissions in post_actions widget #}
{% set auth = current_user.is_authenticated %}
{% set can_edit = auth and current_user.can_edit_post(post) %}
{% set can_delete = auth and current_user.can_delete_post(post) %}
{% set can_punish = auth and current_user.can_punish_post(post) %}
{% set can_topcomm = auth and current_user.can_set_topcomment(post) %}
{% if post.type == "topic" %}
{% set suffix = " le topic" %}
{% elif post.type == "program" %}
{% set suffix = " le programme" %}
{% endif %}
{% if can_edit or can_delete or can_punish or can_topcomm %}
<details>
<summary><b></b></summary>
<div class='context-menu'>
{% if can_edit %}
<a href="{{ url_for('edit_post', postid=post.id, r=request.path) }}">Modifier{{ suffix }}</a>
{% endif %}
{% if can_punish %}
<a href="{{ url_for('delete_post', postid=post.id, penalty=False, csrf_token=csrf_token()) }}" onclick="return confirm('Le post sera supprimé.')">Supprimer{{ suffix }} (normal)</a>
<a href="{{ url_for('delete_post', postid=post.id, penalty=True, csrf_token=csrf_token()) }}" onclick="return confirm('Le post sera supprimé avec pénalité d\'XP.')">Supprimer{{ suffix }} (pénalité)</a>
{% elif can_delete %}
<a href="{{ url_for('delete_post', postid=post.id, penalty=False, csrf_token=csrf_token()) }}" onclick="return confirm('Le post sera supprimé !')">Supprimer{{ suffix }}</a>
{% endif %}
{% if can_topcomm %}
<a href="{{ url_for('set_post_topcomment', postid=post.id, csrf_token=csrf_token()) }}">Utiliser comme en-tête</a>
{% endif %}
</div>
</details>
{% endif %}
{% endmacro %}
{# Thread widget: this widget expands to a table that shows a list of comments
from a thread, along with message controls.
@ -19,38 +59,10 @@
{% if c.date_created != c.date_modified %}
<div>Modifié le <a href="{{ request.path }}#{{ c.id }}">{{ c.date_modified | dyndate }}</a></div>
{% endif %}
{# TODO: Let guests edit their posts #}
{% set can_edit = current_user.is_authenticated and current_user.can_edit_post(c) %}
{% set can_delete = current_user.is_authenticated and current_user.can_delete_post(c) %}
{% set can_punish = current_user.is_authenticated and current_user.priv("delete.posts") %}
{% set can_topcomm = current_user.is_authenticated and current_user.can_set_topcomment(c) %}
{% if can_edit or can_delete or can_punish %}
<details>
<summary><b></b></summary>
<div class='context-menu'>
{% if can_edit %}
<a href="{{ url_for('edit_post', postid=c.id, r=request.path) }}">Modifier</a>
{% endif %}
{% if can_punish %}
<a href="{{ url_for('delete_post', postid=c.id, penalty=False, csrf_token=csrf_token()) }}" onclick="return confirm('Le message sera supprimé.')">Supprimer (normal)</a>
<a href="{{ url_for('delete_post', postid=c.id, penalty=True, csrf_token=csrf_token()) }}" onclick="return confirm('Le message sera supprimé avec pénalité d\'XP.')">Supprimer (pénalité)</a>
{% elif can_delete %}
<a href="{{ url_for('delete_post', postid=c.id, penalty=False, csrf_token=csrf_token()) }}" onclick="return confirm('Le message sera supprimé !')">Supprimer</a>
{% endif %}
{% if can_topcomm %}
<a href="{{ url_for('set_post_topcomment', postid=c.id, csrf_token=csrf_token()) }}">Utiliser comme en-tête</a>
{% endif %}
</div>
</details>
{% endif %}
{{ post_actions(c) }}
</div>
{{ c.text|md }}
{{ widget_attachments.attachments(c) }}
{% if c.author.signature %}