nopaque/web/app/admin/views.py

58 lines
1.9 KiB
Python
Raw Normal View History

from flask import flash, redirect, render_template, url_for
from flask_login import login_required
from . import admin
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
2020-10-23 08:17:14 +00:00
@admin.route('/users')
@login_required
@admin_required
2020-10-23 08:17:14 +00:00
def users():
users = [user.to_dict() for user in User.query.all()]
2020-10-23 08:17:14 +00:00
return render_template('admin/users.html.j2', title='Users', users=users)
2020-10-23 08:17:14 +00:00
@admin.route('/users/<int: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)
2020-10-23 08:17:14 +00:00
@admin.route('/users/<int: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'))
@admin.route('/users/<int:user_id>/edit_general_settings', methods=['GET', 'POST']) # noqa
@login_required
@admin_required
def edit_general_settings(user_id):
user = User.query.get_or_404(user_id)
form = EditGeneralSettingsAdminForm(user=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_general_settings', 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_general_settings.html.j2',
form=form, title='General settings', user=user)