2020-04-23 06:24:11 +00:00
|
|
|
from flask import flash, redirect, render_template, url_for
|
2020-11-04 13:54:27 +00:00
|
|
|
from flask_login import login_required
|
2021-09-13 09:45:43 +00:00
|
|
|
from . import bp
|
2020-10-27 13:23:23 +00:00
|
|
|
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
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/')
|
2021-01-13 10:57:46 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def index():
|
|
|
|
return redirect(url_for('.users'))
|
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/users')
|
2019-08-15 13:56:53 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-10-23 08:17:14 +00:00
|
|
|
def users():
|
2021-01-13 10:57:46 +00:00
|
|
|
# users = [user.to_dict() for user in User.query.all()]
|
|
|
|
users = {user.id: 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)
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/users/<int:user_id>')
|
2019-08-15 13:56:53 +00:00
|
|
|
@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)
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/users/<int:user_id>/delete')
|
2019-08-15 13:56:53 +00:00
|
|
|
@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)
|
2020-11-13 09:01:51 +00:00
|
|
|
flash('User has been marked for deletion!')
|
2020-10-28 14:34:12 +00:00
|
|
|
return redirect(url_for('.users'))
|
2019-09-10 11:49:01 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/users/<int:user_id>/edit', methods=['GET', 'POST']) # noqa
|
2019-09-10 11:49:01 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2021-01-13 10:57:46 +00:00
|
|
|
def edit_user(user_id):
|
2019-09-10 11:49:01 +00:00
|
|
|
user = User.query.get_or_404(user_id)
|
2021-09-13 14:36:48 +00:00
|
|
|
form = EditGeneralSettingsAdminForm(user)
|
2020-10-27 13:23:23 +00:00
|
|
|
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)
|
2019-09-10 11:49:01 +00:00
|
|
|
db.session.commit()
|
2020-11-13 09:01:51 +00:00
|
|
|
flash('Settings have been updated.')
|
2021-01-13 10:57:46 +00:00
|
|
|
return redirect(url_for('.edit_user', user_id=user.id))
|
2020-10-27 13:23:23 +00:00
|
|
|
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
|
2021-01-13 10:57:46 +00:00
|
|
|
return render_template('admin/edit_user.html.j2', form=form,
|
|
|
|
title='Edit user', user=user)
|