nopaque/app/admin/routes.py

67 lines
2.0 KiB
Python
Raw Normal View History

from flask import flash, redirect, render_template, url_for
from flask_login import login_required
from . import bp
from .forms import EditGeneralSettingsAdminForm
2020-03-27 11:06:11 +00:00
from .. import db
from ..decorators import admin_required
from ..models import Role, User
2020-10-26 10:17:25 +00:00
from ..settings import tasks as settings_tasks
@bp.route('/')
@login_required
@admin_required
def index():
return redirect(url_for('.users'))
@bp.route('/users')
@login_required
@admin_required
2020-10-23 08:17:14 +00:00
def users():
2021-11-30 15:22:16 +00:00
dict_users = {user.id: user.to_dict(backrefs=True, relationships=False)
for user in User.query.all()}
return render_template(
'admin/users.html.j2', title='Users', dict_users=dict_users)
2021-11-30 15:22:16 +00:00
@bp.route('/users/<hashid:user_id>')
@login_required
@admin_required
2019-11-15 10:45:04 +00:00
def user(user_id):
user = User.query.get_or_404(user_id)
2020-10-28 14:34:12 +00:00
return render_template('admin/user.html.j2', title='User', user=user)
2021-11-30 15:22:16 +00:00
@bp.route('/users/<hashid:user_id>/delete')
@login_required
@admin_required
2019-11-15 10:45:04 +00:00
def delete_user(user_id):
2020-10-26 10:17:25 +00:00
settings_tasks.delete_user(user_id)
flash('User has been marked for deletion!')
2020-10-28 14:34:12 +00:00
return redirect(url_for('.users'))
2021-11-30 15:22:16 +00:00
@bp.route('/users/<hashid:user_id>/edit', methods=['GET', 'POST']) # noqa
@login_required
@admin_required
def edit_user(user_id):
user = User.query.get_or_404(user_id)
form = EditGeneralSettingsAdminForm(user)
if form.validate_on_submit():
user.setting_dark_mode = form.dark_mode.data
user.email = form.email.data
user.username = form.username.data
user.confirmed = form.confirmed.data
user.role = Role.query.get(form.role.data)
db.session.commit()
flash('Settings have been updated.')
return redirect(url_for('.edit_user', user_id=user.id))
form.confirmed.data = user.confirmed
form.dark_mode.data = user.setting_dark_mode
form.email.data = user.email
form.role.data = user.role_id
form.username.data = user.username
return render_template('admin/edit_user.html.j2', form=form,
title='Edit user', user=user)