implement thread mod ops

This commit is contained in:
Lera Elvoé 2025-07-01 23:34:48 +03:00
parent 29bb9872d3
commit bd556d102b
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
2 changed files with 47 additions and 6 deletions

View File

@ -1,9 +1,10 @@
from flask import ( from flask import (
Blueprint, render_template, request, redirect, url_for Blueprint, render_template, request, redirect, url_for, flash
) )
from .users import login_required, mod_only, get_active_user, is_logged_in from .users import login_required, mod_only, get_active_user, is_logged_in
from ..db import db from ..db import db
from ..models import Threads, Topics, Posts, Subscriptions from ..models import Threads, Topics, Posts, Subscriptions
from ..constants import InfoboxKind
from .posts import create_post from .posts import create_post
from slugify import slugify from slugify import slugify
import math import math
@ -118,21 +119,61 @@ def create_form():
@bp.post("/<slug>/lock") @bp.post("/<slug>/lock")
@login_required @login_required
def lock(slug): def lock(slug):
pass user = get_active_user()
thread = Threads.find({'slug': slug})
if not ((thread.user_id == user.id) or user.is_mod()):
return 'no'
target_op = request.form.get('target_op')
thread.update({
'is_locked': target_op
})
return redirect(url_for('.thread', slug=slug))
@bp.post("/<slug>/sticky") @bp.post("/<slug>/sticky")
@login_required @login_required
@mod_only(".thread", slug = lambda slug: slug) @mod_only(".thread", slug = lambda slug: slug)
def sticky(slug): def sticky(slug):
pass user = get_active_user()
thread = Threads.find({'slug': slug})
if not ((thread.user_id == user.id) or user.is_mod()):
return 'no'
target_op = request.form.get('target_op')
thread.update({
'is_stickied': target_op
})
return redirect(url_for('.thread', slug=slug))
@bp.post("/<slug>/move") @bp.post("/<slug>/move")
@login_required @login_required
@mod_only(".thread", slug = lambda slug: slug) @mod_only(".thread", slug = lambda slug: slug)
def move(slug): def move(slug):
pass user = get_active_user()
new_topic_id = request.form.get('new_topic_id', default=None)
if new_topic_id is None:
flash('Thread is already in this topic.', InfoboxKind.ERROR)
return redirect(url_for('.thread', slug=slug))
new_topic = Topics.find({
'id': new_topic_id
})
if not new_topic:
return 'no'
thread = Threads.find({
'slug': slug
})
if not thread:
return 'no'
if new_topic.id == thread.topic_id:
flash('Thread is already in this topic.', InfoboxKind.ERROR)
return redirect(url_for('.thread', slug=slug))
old_topic = Topics.find({'id': thread.topic_id})
thread.update({'topic_id': new_topic_id})
flash(f'Topic moved from "{old_topic.name}" to "{new_topic.name}".', InfoboxKind.INFO)
return redirect(url_for('.thread', slug=slug))
@bp.post("/<slug>/subscribe") @bp.post("/<slug>/subscribe")

View File

@ -28,13 +28,13 @@
{% endif %} {% endif %}
{% if can_lock %} {% if can_lock %}
<form class="modform" action="{{ url_for("threads.lock", slug=thread.slug) }}" method="post"> <form class="modform" action="{{ url_for("threads.lock", slug=thread.slug) }}" method="post">
<input type=hidden value="{{ (not thread.is_locked) | int }}"> <input type=hidden name='target_op' value="{{ (not thread.is_locked) | int }}">
<input class="warn" type="submit" value="{{"Unlock thread" if thread.is_locked else "Lock thread"}}"> <input class="warn" type="submit" value="{{"Unlock thread" if thread.is_locked else "Lock thread"}}">
</form> </form>
{% endif %} {% endif %}
{% if active_user.is_mod() %} {% if active_user.is_mod() %}
<form class="modform" action="{{ url_for("threads.sticky", slug=thread.slug) }}" method="post"> <form class="modform" action="{{ url_for("threads.sticky", slug=thread.slug) }}" method="post">
<input type=hidden value="{{ (not thread.is_stickied) | int }}"> <input type=hidden name='target_op' value="{{ (not thread.is_stickied) | int }}">
<input class="warn" type="submit" value="{{"Unsticky thread" if thread.is_stickied else "Sticky thread"}}"> <input class="warn" type="submit" value="{{"Unsticky thread" if thread.is_stickied else "Sticky thread"}}">
</form> </form>
<form class="modform" action="{{ url_for("threads.move", slug=thread.slug) }}" method="post"> <form class="modform" action="{{ url_for("threads.move", slug=thread.slug) }}" method="post">