2020-04-23 06:24:11 +00:00
|
|
|
from flask import flash, redirect, render_template, url_for
|
2020-10-27 13:23:23 +00:00
|
|
|
from flask_login import current_user, login_required
|
2019-08-15 13:56:53 +00:00
|
|
|
from . import admin
|
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
|
|
|
|
|
|
|
|
2020-10-23 08:17:14 +00:00
|
|
|
@admin.route('/users')
|
2019-08-15 13:56:53 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-10-23 08:17:14 +00:00
|
|
|
def users():
|
2019-11-15 10:45:04 +00:00
|
|
|
users = User.query.all()
|
2020-07-07 14:06:09 +00:00
|
|
|
users = [dict(username=u.username,
|
|
|
|
email=u.email,
|
|
|
|
role_id=u.role_id,
|
|
|
|
confirmed=u.confirmed,
|
|
|
|
id=u.id)
|
2019-09-23 14:25:26 +00:00
|
|
|
for u in users]
|
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
|
|
|
|
|
|
|
|
2020-10-23 08:17:14 +00:00
|
|
|
@admin.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
|
|
|
|
|
|
|
|
2020-10-23 08:17:14 +00:00
|
|
|
@admin.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)
|
2019-11-15 10:45:04 +00:00
|
|
|
flash('User has been deleted!')
|
2020-10-28 14:34:12 +00:00
|
|
|
return redirect(url_for('.users'))
|
2019-09-10 11:49:01 +00:00
|
|
|
|
|
|
|
|
2020-10-27 13:23:23 +00:00
|
|
|
@admin.route('/users/<int:user_id>/edit_general_settings',
|
|
|
|
methods=['GET', 'POST'])
|
2019-09-10 11:49:01 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-10-27 13:23:23 +00:00
|
|
|
def edit_general_settings(user_id):
|
2019-09-10 11:49:01 +00:00
|
|
|
user = User.query.get_or_404(user_id)
|
2020-10-27 13:23:23 +00:00
|
|
|
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)
|
2019-09-10 11:49:01 +00:00
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
flash('The profile has been updated.')
|
2020-10-27 13:23:23 +00:00
|
|
|
return redirect(url_for('admin.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',
|
2020-10-23 08:17:14 +00:00
|
|
|
user=user)
|