105 lines
2.8 KiB
Python
105 lines
2.8 KiB
Python
from flask import (
|
|
Blueprint, render_template, request, redirect, url_for,
|
|
flash
|
|
)
|
|
from .users import get_active_user, is_logged_in
|
|
from ..models import Users, PasswordResetLinks, MOTD
|
|
from ..constants import InfoboxKind, MOTD_BANNED_TAGS
|
|
from ..lib.babycode import babycode_to_html, BABYCODE_VERSION
|
|
from ..db import db
|
|
import secrets
|
|
import time
|
|
|
|
bp = Blueprint("mod", __name__, url_prefix = "/mod/")
|
|
|
|
@bp.before_request
|
|
def _before_request():
|
|
if not is_logged_in():
|
|
return redirect(url_for("users.log_in"))
|
|
|
|
if not get_active_user().is_mod():
|
|
return redirect(url_for("topics.all_topics"))
|
|
|
|
|
|
@bp.get("/sort-topics")
|
|
def sort_topics():
|
|
topics = db.query("SELECT * FROM topics ORDER BY sort_order ASC")
|
|
return render_template("mod/sort-topics.html", topics = topics)
|
|
|
|
|
|
@bp.post("/sort-topics")
|
|
def sort_topics_post():
|
|
with db.transaction():
|
|
for topic_id, new_order in request.form.items():
|
|
db.execute("UPDATE topics SET sort_order = ? WHERE id = ?", new_order, topic_id)
|
|
|
|
return redirect(url_for(".sort_topics"))
|
|
|
|
|
|
@bp.get("/user-list")
|
|
def user_list():
|
|
users = Users.select()
|
|
return render_template("mod/user-list.html", users = users)
|
|
|
|
|
|
@bp.post("/reset-pass/<user_id>")
|
|
def create_reset_pass(user_id):
|
|
now = int(time.time())
|
|
key = secrets.token_urlsafe(20)
|
|
reset_link = PasswordResetLinks.create({
|
|
'user_id': int(user_id),
|
|
'expires_at': now + 24 * 60 * 60,
|
|
'key': key,
|
|
})
|
|
|
|
return redirect(url_for('users.reset_link_login', key=key))
|
|
|
|
|
|
@bp.get('/panel')
|
|
def panel():
|
|
return render_template('mod/panel.html')
|
|
|
|
|
|
@bp.get('/motd')
|
|
def motd_editor():
|
|
current = MOTD.get_all()[0] if MOTD.has_motd() else None
|
|
return render_template('mod/motd.html', current=current)
|
|
|
|
|
|
@bp.post('/motd')
|
|
def motd_editor_form():
|
|
orig_body = request.form.get('body', default='')
|
|
title = request.form.get('title', default='')
|
|
data = {
|
|
'title': title,
|
|
'body_original_markup': orig_body,
|
|
'body_rendered': babycode_to_html(orig_body, banned_tags=MOTD_BANNED_TAGS).result,
|
|
'format_version': BABYCODE_VERSION,
|
|
'edited_at': int(time.time()),
|
|
}
|
|
|
|
if MOTD.has_motd():
|
|
motd = MOTD.get_all()[0]
|
|
motd.update(data)
|
|
message = 'MOTD updated.'
|
|
else:
|
|
data['created_at'] = int(time.time())
|
|
data['user_id'] = get_active_user().id
|
|
motd = MOTD.create(data)
|
|
message = 'MOTD created.'
|
|
|
|
flash(message, InfoboxKind.INFO)
|
|
return redirect(url_for('.motd_editor'))
|
|
|
|
|
|
@bp.post('/motd/delete')
|
|
def motd_delete():
|
|
if not MOTD.has_motd():
|
|
flash('No MOTD to delete.', InfoboxKind.WARN)
|
|
return redirect(url_for('.motd_editor'))
|
|
|
|
current = MOTD.get_all()[0]
|
|
current.delete()
|
|
flash('MOTD deleted.', InfoboxKind.INFO)
|
|
return redirect(url_for('.motd_editor'))
|