diff --git a/app/routes/threads.py b/app/routes/threads.py index 23c4286..cd7249e 100644 --- a/app/routes/threads.py +++ b/app/routes/threads.py @@ -1,9 +1,10 @@ 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 ..db import db from ..models import Threads, Topics, Posts, Subscriptions +from ..constants import InfoboxKind from .posts import create_post from slugify import slugify import math @@ -118,21 +119,61 @@ def create_form(): @bp.post("//lock") @login_required 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("//sticky") @login_required @mod_only(".thread", slug = lambda slug: 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("//move") @login_required @mod_only(".thread", slug = lambda slug: 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("//subscribe") diff --git a/app/templates/threads/thread.html b/app/templates/threads/thread.html index 15bbc34..acf1c2f 100644 --- a/app/templates/threads/thread.html +++ b/app/templates/threads/thread.html @@ -28,13 +28,13 @@ {% endif %} {% if can_lock %}
- +
{% endif %} {% if active_user.is_mod() %}
- +