You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.5 KiB
117 lines
4.5 KiB
from flask import Blueprint, render_template, redirect, url_for, flash, request
|
|
|
|
from suchwow._models import AuditEvent, Post, User, get_ban_reason
|
|
from suchwow.utils.decorators import moderator_required
|
|
from suchwow.utils.helpers import get_session_user, audit_event
|
|
from suchwow import config
|
|
|
|
|
|
bp = Blueprint("mod", "mod")
|
|
|
|
@bp.route('/mods')
|
|
@moderator_required
|
|
def main():
|
|
live_posts = Post.select().where(Post.approved == True).count()
|
|
pending_posts = Post.select().where(Post.approved == False).count()
|
|
active_posters = User.select().join(Post, on=Post.user).distinct().count()
|
|
mods = User.select().where(User.moderator == True).count()
|
|
return render_template(
|
|
'mod/main.html',
|
|
live_posts=live_posts,
|
|
pending_posts=pending_posts,
|
|
active_posters=active_posters,
|
|
mods=mods
|
|
)
|
|
|
|
|
|
@bp.route('/mods/pending')
|
|
@moderator_required
|
|
def pending_posts():
|
|
posts = Post.select().where(Post.approved == False).order_by(Post.timestamp.asc())
|
|
if not posts:
|
|
flash('no posts pending', 'is-warning')
|
|
return redirect(url_for('mod.main'))
|
|
return render_template('mod/posts.html', posts=posts)
|
|
|
|
|
|
@bp.route('/mods/manage', methods=['GET', 'POST'])
|
|
@moderator_required
|
|
def manage_mods():
|
|
to_remove = request.args.get('delete')
|
|
if to_remove:
|
|
u = User.select().where(User.username == to_remove).first()
|
|
if not u.moderator:
|
|
flash('That user is not a moderator', 'is-danger')
|
|
elif u.username == get_session_user():
|
|
flash('Cannot remove yourself.', 'is-danger')
|
|
elif u.username == config.SUPER_ADMIN:
|
|
flash('Cannot delete super admin you son-of-a-bitch.', 'is-danger')
|
|
else:
|
|
u.moderator = False
|
|
u.save()
|
|
audit_event(f'Removed {to_remove} from mods')
|
|
flash(f'Removed {to_remove} from mods!', 'is-success')
|
|
return redirect(url_for('mod.manage_mods'))
|
|
if request.method == 'POST':
|
|
to_add = request.form.get('username', None)
|
|
if to_add:
|
|
u = User.select().where(User.username == to_add).first()
|
|
if not u:
|
|
flash('That user does not appear to exist', 'is-danger')
|
|
elif u.moderator:
|
|
flash(f'{to_add} is already a mod, ya dingus.', 'is-warning')
|
|
else:
|
|
u.moderator = True
|
|
u.save()
|
|
audit_event(f'Added {to_add} to mods')
|
|
flash(f'Added {to_add} to mods!', 'is-success')
|
|
mods = User.select().where(User.moderator == True)
|
|
return render_template('mod/manage.html', mods=mods)
|
|
|
|
|
|
@bp.route('/mods/bans', methods=['GET', 'POST'])
|
|
@moderator_required
|
|
def manage_bans():
|
|
to_unban = request.args.get('delete')
|
|
if to_unban:
|
|
u = User.select().where(User.username == to_unban).first()
|
|
if not u.banned:
|
|
flash('No ban exists for that user', 'is-danger')
|
|
elif u.username == get_session_user():
|
|
flash('Cannot ban yourself.', 'is-danger')
|
|
elif u.username == config.SUPER_ADMIN:
|
|
flash('Cannot ban super admin you son-of-a-bitch.', 'is-danger')
|
|
else:
|
|
u.banned = False
|
|
u.save()
|
|
audit_event(f'Removed ban on {to_unban}')
|
|
flash(f'Unbanned {to_unban}!', 'is-success')
|
|
return redirect(url_for('mod.manage_bans'))
|
|
if request.method == 'POST':
|
|
to_ban = request.form.get('username', None)
|
|
if to_ban:
|
|
u = User.select().where(User.username == to_ban).first()
|
|
if not u:
|
|
flash('That user does not appear to exist', 'is-danger')
|
|
elif u.banned:
|
|
flash(f'{to_ban} is already banned, ya dingus.', 'is-warning')
|
|
elif u.username == config.SUPER_ADMIN:
|
|
flash('Cannot ban the super admin you son-of-a-bitch.', 'is-danger')
|
|
else:
|
|
reason = request.form.get('reason')
|
|
if not reason:
|
|
reason = get_ban_reason()
|
|
u.banned = True
|
|
u.ban_reason = reason
|
|
u.save()
|
|
audit_event(f'Banned {to_ban} ({reason})')
|
|
flash(f'Banned {to_ban}!', 'is-success')
|
|
bans = User.select().where(User.banned == True)
|
|
return render_template('mod/bans.html', bans=bans)
|
|
|
|
|
|
@bp.route('/mods/logs')
|
|
@moderator_required
|
|
def view_logs():
|
|
events = AuditEvent.select().order_by(AuditEvent.timestamp.desc()).limit(50)
|
|
return render_template('mod/logs.html', logs=events) |