from flask import Blueprint, abort, redirect, url_for, request, render_template from ..auth import is_logged_in, get_active_user, csrf_verified from ..models import Topics, Threads bp = Blueprint('mod', __name__, url_prefix='/mod/') @bp.before_request def mod_only(): if not is_logged_in(): abort(403) if not get_active_user().is_mod(): abort(403) @bp.get('/') def index(): return 'stub' @bp.get('/topics/new/') def new_topic(): return render_template('mod/new_topic.html') @bp.post('/topics/new/') def new_topic_post(): topic = Topics.new(request.form.get('name'), request.form.get('description')) return redirect(url_for('topics.topic', slug=topic.slug)) @bp.get('/topics/sort/') def sort_topics(): return 'stub' @bp.get('/topics//edit/') def edit_topic(topic_id): topic = Topics.find({'id': topic_id}) if not topic: abort(404) return render_template('mod/edit_topic.html', topic=topic) @bp.post('/topics//edit/') def edit_topic_post(topic_id): topic = Topics.find({'id': topic_id}) if not topic: abort(404) topic.update({ 'name': request.form.get('name').strip(), 'description': request.form.get('description').strip(), }) return redirect(url_for('topics.topic', slug=topic.slug)) @bp.post('/topics//lock/') def lock_topic(topic_id): topic = Topics.find({'id': topic_id}) if not topic: abort(404) topic.update({'is_locked': request.form.get('lock', default=0)}) return redirect(url_for('topics.topic', slug=topic.slug)) @bp.post('/threads//move/') def move_thread(thread_id): thread = Threads.find({'id': thread_id}) if not thread: abort(404) target_topic = Topics.find({'id': request.form.get('new_topic_id', default=None)}) if not target_topic: abort(404) thread.update({'topic_id': target_topic.id}) return redirect(url_for('threads.thread', slug=thread.slug)) @bp.post('/threads//lock/') def lock_thread(thread_id): thread = Threads.find({'id': thread_id}) if not thread: abort(404) thread.update({'is_locked': request.form.get('lock')}) return redirect(url_for('threads.thread', slug=thread.slug)) @bp.post('/threads//sticky/') def sticky_thread(thread_id): thread = Threads.find({'id': thread_id}) if not thread: abort(404) thread.update({'is_stickied': request.form.get('sticky')}) return redirect(url_for('threads.thread', slug=thread.slug)) @bp.post('/users//make-guest/') @csrf_verified def make_user_guest(user_id): return 'stub' @bp.post('/users//make-user/') @csrf_verified def make_user_regular(user_id): return 'stub' @bp.post('/users//make-mod/') @csrf_verified def make_user_mod(user_id): return 'stub'