pyfedi/app/admin/routes.py

50 lines
2.1 KiB
Python
Raw Normal View History

2023-11-03 21:59:48 +13:00
from flask import request, flash
from flask_login import login_required, current_user
from flask_babel import _
from sqlalchemy import text, desc
2023-11-03 21:59:48 +13:00
from app import db
from app.admin.forms import AdminForm
from app.models import AllowedInstances, BannedInstances, ActivityPubLog
2023-11-03 21:59:48 +13:00
from app.utils import render_template, permission_required, set_setting, get_setting
from app.admin import bp
@bp.route('/', methods=['GET', 'POST'])
@login_required
@permission_required('change instance settings')
def admin_home():
form = AdminForm()
if form.validate_on_submit():
if form.use_allowlist.data:
set_setting('use_allowlist', True)
db.session.execute(text('DELETE FROM allowed_instances'))
for allow in form.allowlist.data.split('\n'):
if allow.strip():
db.session.add(AllowedInstances(domain=allow.strip()))
if form.use_blocklist.data:
set_setting('use_allowlist', False)
db.session.execute(text('DELETE FROM banned_instances'))
for banned in form.blocklist.data.split('\n'):
if banned.strip():
db.session.add(BannedInstances(domain=banned.strip()))
db.session.commit()
flash(_('Admin settings saved'))
elif request.method == 'GET':
form.use_allowlist.data = get_setting('use_allowlist', False)
form.use_blocklist.data = not form.use_allowlist.data
instances = BannedInstances.query.all()
form.blocklist.data = '\n'.join([instance.domain for instance in instances])
instances = AllowedInstances.query.all()
form.allowlist.data = '\n'.join([instance.domain for instance in instances])
return render_template('admin/home.html', title=_('Admin settings'), form=form)
@bp.route('/activities', methods=['GET'])
@login_required
@permission_required('change instance settings')
def admin_activities():
return render_template('admin/activities.html', title=_('ActivityPub Log'),
activities=ActivityPubLog.query.order_by(desc(ActivityPubLog.created_at)).all())