2020-04-23 08:24:11 +02:00
|
|
|
from flask import flash, redirect, render_template, url_for
|
2019-09-23 16:25:26 +02:00
|
|
|
from flask_login import login_required
|
2019-08-15 15:56:53 +02:00
|
|
|
from . import admin
|
2019-11-15 14:01:36 +01:00
|
|
|
from .forms import EditUserForm
|
2020-03-27 12:06:11 +01:00
|
|
|
from .. import db
|
|
|
|
from ..decorators import admin_required
|
|
|
|
from ..models import Role, User
|
2020-04-23 08:24:11 +02:00
|
|
|
from ..profile import tasks as profile_tasks
|
2019-08-15 15:56:53 +02:00
|
|
|
|
|
|
|
|
2019-11-15 12:51:53 +01:00
|
|
|
@admin.route('/')
|
2019-08-15 15:56:53 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-11-15 12:51:53 +01:00
|
|
|
def index():
|
2019-11-15 11:45:04 +01:00
|
|
|
users = User.query.all()
|
2020-07-07 16:06:09 +02:00
|
|
|
users = [dict(username=u.username,
|
|
|
|
email=u.email,
|
|
|
|
role_id=u.role_id,
|
|
|
|
confirmed=u.confirmed,
|
|
|
|
id=u.id)
|
2019-09-23 16:25:26 +02:00
|
|
|
for u in users]
|
2020-07-07 16:06:09 +02:00
|
|
|
return render_template('admin/index.html.j2',
|
|
|
|
title='Administration tools',
|
|
|
|
users=users)
|
2019-08-15 15:56:53 +02:00
|
|
|
|
|
|
|
|
2019-11-15 11:45:04 +01:00
|
|
|
@admin.route('/user/<int:user_id>')
|
2019-08-15 15:56:53 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-11-15 11:45:04 +01:00
|
|
|
def user(user_id):
|
|
|
|
user = User.query.get_or_404(user_id)
|
2019-11-15 12:51:53 +01:00
|
|
|
return render_template('admin/user.html.j2', title='Administration: User',
|
2019-11-15 11:45:04 +01:00
|
|
|
user=user)
|
2019-08-15 15:56:53 +02:00
|
|
|
|
|
|
|
|
2019-11-15 11:45:04 +01:00
|
|
|
@admin.route('/user/<int:user_id>/delete')
|
2019-08-15 15:56:53 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-11-15 11:45:04 +01:00
|
|
|
def delete_user(user_id):
|
2020-04-23 08:24:11 +02:00
|
|
|
profile_tasks.delete_user(user_id)
|
2019-11-15 11:45:04 +01:00
|
|
|
flash('User has been deleted!')
|
2019-11-15 12:51:53 +01:00
|
|
|
return redirect(url_for('admin.index'))
|
2019-09-10 13:49:01 +02:00
|
|
|
|
|
|
|
|
2019-11-15 11:45:04 +01:00
|
|
|
@admin.route('/user/<int:user_id>/edit', methods=['GET', 'POST'])
|
2019-09-10 13:49:01 +02:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-11-15 11:45:04 +01:00
|
|
|
def edit_user(user_id):
|
2019-09-10 13:49:01 +02:00
|
|
|
user = User.query.get_or_404(user_id)
|
2019-11-15 14:01:36 +01:00
|
|
|
edit_user_form = EditUserForm(user=user)
|
|
|
|
if edit_user_form.validate_on_submit():
|
|
|
|
user.email = edit_user_form.email.data
|
|
|
|
user.username = edit_user_form.username.data
|
|
|
|
user.confirmed = edit_user_form.confirmed.data
|
|
|
|
user.role = Role.query.get(edit_user_form.role.data)
|
2019-09-10 13:49:01 +02:00
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
flash('The profile has been updated.')
|
2019-11-15 11:45:04 +01:00
|
|
|
return redirect(url_for('admin.edit_user', user_id=user.id))
|
2020-02-18 15:22:48 +01:00
|
|
|
edit_user_form.email.data = user.email
|
|
|
|
edit_user_form.username.data = user.username
|
|
|
|
edit_user_form.confirmed.data = user.confirmed
|
|
|
|
edit_user_form.role.data = user.role_id
|
2019-11-15 14:01:36 +01:00
|
|
|
return render_template('admin/edit_user.html.j2',
|
|
|
|
edit_user_form=edit_user_form,
|
2019-11-15 12:51:53 +01:00
|
|
|
title='Administration: Edit user', user=user)
|