|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
from flask import Blueprint, render_template, redirect, url_for, flash, request
|
|
|
|
|
|
|
|
|
|
from suchwow.models import AuditEvent, Post, Profile, Moderator, Ban, get_ban_reason
|
|
|
|
|
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
|
|
|
|
@ -13,8 +13,8 @@ bp = Blueprint("mod", "mod")
|
|
|
|
|
def main():
|
|
|
|
|
live_posts = Post.select().where(Post.approved == True).count()
|
|
|
|
|
pending_posts = Post.select().where(Post.approved == False).count()
|
|
|
|
|
active_posters = Profile.select().join(Post, on=Post.submitter == Profile.username).distinct().count()
|
|
|
|
|
mods = Moderator.select().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,
|
|
|
|
@ -37,73 +37,76 @@ def pending_posts():
|
|
|
|
|
@bp.route('/mods/manage', methods=['GET', 'POST'])
|
|
|
|
|
@moderator_required
|
|
|
|
|
def manage_mods():
|
|
|
|
|
to_delete = request.args.get('delete')
|
|
|
|
|
if to_delete:
|
|
|
|
|
m = Moderator.select().where(Moderator.username == to_delete).first()
|
|
|
|
|
if not m:
|
|
|
|
|
flash('No moderator exists with that name', 'is-danger')
|
|
|
|
|
elif m.username == get_session_user():
|
|
|
|
|
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 m.username == config.SUPER_ADMIN:
|
|
|
|
|
elif u.username == config.SUPER_ADMIN:
|
|
|
|
|
flash('Cannot delete super admin you son-of-a-bitch.', 'is-danger')
|
|
|
|
|
else:
|
|
|
|
|
m.delete_instance()
|
|
|
|
|
audit_event(f'Deleted {to_delete} from mods')
|
|
|
|
|
flash(f'Removed {to_delete} from mods!', 'is-success')
|
|
|
|
|
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 = Profile.select().where(Profile.username == to_add).first()
|
|
|
|
|
u = User.select().where(User.username == to_add).first()
|
|
|
|
|
if not u:
|
|
|
|
|
flash('That user does not appear to exist (no profile setup yet)', 'is-danger')
|
|
|
|
|
elif Moderator.select().where(Moderator.username == to_add).first():
|
|
|
|
|
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:
|
|
|
|
|
m = Moderator(username=to_add)
|
|
|
|
|
m.save()
|
|
|
|
|
u.moderator = True
|
|
|
|
|
u.save()
|
|
|
|
|
audit_event(f'Added {to_add} to mods')
|
|
|
|
|
flash(f'Added {to_add} to mods!', 'is-success')
|
|
|
|
|
mods = Profile.select().join(Moderator, on=(Profile.username == Moderator.username))
|
|
|
|
|
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_delete = request.args.get('delete')
|
|
|
|
|
if to_delete:
|
|
|
|
|
ban = Ban.select().join(Profile).where(Profile.username == to_delete).first()
|
|
|
|
|
if not ban:
|
|
|
|
|
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 ban.user == get_session_user():
|
|
|
|
|
elif u.username == get_session_user():
|
|
|
|
|
flash('Cannot ban yourself.', 'is-danger')
|
|
|
|
|
elif ban.user == config.SUPER_ADMIN:
|
|
|
|
|
elif u.username == config.SUPER_ADMIN:
|
|
|
|
|
flash('Cannot ban super admin you son-of-a-bitch.', 'is-danger')
|
|
|
|
|
else:
|
|
|
|
|
ban.delete_instance()
|
|
|
|
|
audit_event(f'Removed ban on {to_delete}')
|
|
|
|
|
flash(f'Unbanned {to_delete}!', 'is-success')
|
|
|
|
|
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_add = request.form.get('username', None)
|
|
|
|
|
if to_add:
|
|
|
|
|
u = Profile.select().where(Profile.username == to_add).first()
|
|
|
|
|
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 (no profile setup yet)', 'is-danger')
|
|
|
|
|
elif Ban.select().join(Profile).where(Profile.username == to_add).first():
|
|
|
|
|
flash(f'{to_add} is already banned, ya dingus.', 'is-warning')
|
|
|
|
|
elif to_add == config.SUPER_ADMIN:
|
|
|
|
|
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()
|
|
|
|
|
ban = Ban(user=u, reason=reason)
|
|
|
|
|
ban.save()
|
|
|
|
|
audit_event(f'Banned {to_add} ({reason})')
|
|
|
|
|
flash(f'Banned {to_add}!', 'is-success')
|
|
|
|
|
bans = Ban.select()
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|