mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 09:15:41 +00:00
90 lines
3.1 KiB
Python
90 lines
3.1 KiB
Python
from app.utils import background_delete_user
|
|
from flask import current_app, flash, redirect, render_template, url_for
|
|
from flask_login import login_required
|
|
from . import admin
|
|
from .forms import EditProfileAdminForm
|
|
from .. import db
|
|
from ..decorators import admin_required
|
|
from ..models import Role, User
|
|
from ..tables import AdminUserTable, AdminUserItem
|
|
import threading
|
|
|
|
|
|
@admin.route('/overview', methods=['GET', 'POST'])
|
|
@login_required
|
|
@admin_required
|
|
def for_admins_only():
|
|
users = User.query.order_by(User.username).all()
|
|
items = [AdminUserItem(u.username, u.email, u.role_id, u.confirmed, u.id)
|
|
for u in users]
|
|
# Convert table object to html string
|
|
table = AdminUserTable(items).__html__()
|
|
# Add class "list" to tbody element. Needed for "List.js"
|
|
table = table.replace('tbody', 'tbody class="list"', 1)
|
|
return render_template('admin/admin.html.j2',
|
|
table=table,
|
|
title='Administration tools')
|
|
|
|
|
|
@admin.route('/overview/admin_user_page/<int:user_id>',
|
|
methods=['GET', 'POST'])
|
|
@login_required
|
|
@admin_required
|
|
def admin_user_page(user_id):
|
|
selected_user = User.query.filter_by(id=user_id).first()
|
|
title = 'Administration of user {} with ID: {}'.format(
|
|
selected_user.username,
|
|
selected_user.id
|
|
)
|
|
registration_date = selected_user.registration_date.strftime(
|
|
'%A, %e %B %H:%M'
|
|
)
|
|
return render_template('admin/admin_user_page.html.j2',
|
|
registration_date=registration_date,
|
|
selected_user=selected_user,
|
|
title=title)
|
|
|
|
|
|
@admin.route('/overview/admin_user_page/delete/<int:user_id>',
|
|
methods=['GET', 'POST'])
|
|
@login_required
|
|
@admin_required
|
|
def admin_delete_user(user_id):
|
|
delete_thread = threading.Thread(
|
|
target=background_delete_user,
|
|
args=(current_app._get_current_object(), user_id)
|
|
)
|
|
delete_thread.start()
|
|
flash('User {} has been deleted!'.format(user_id))
|
|
return redirect(url_for('admin.for_admins_only'))
|
|
|
|
|
|
@admin.route('/overview/admin_user_page/edit_profile_admin/<int:user_id>',
|
|
methods=['GET', 'POST'])
|
|
@login_required
|
|
@admin_required
|
|
def edit_profile_admin(user_id):
|
|
user = User.query.get_or_404(user_id)
|
|
form = EditProfileAdminForm(user=user)
|
|
if form.validate_on_submit():
|
|
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.add(user)
|
|
db.session.commit()
|
|
flash('The profile has been updated.')
|
|
return redirect(url_for('admin.edit_profile_admin', user_id=user.id))
|
|
form.email.data = user.email
|
|
form.username.data = user.username
|
|
form.confirmed.data = user.confirmed
|
|
form.role.data = user.role_id
|
|
title = 'Edit profile of user {} with ID {}'.format(
|
|
user.username,
|
|
user.id
|
|
)
|
|
return render_template('admin/edit_profile_admin.html.j2',
|
|
form=form,
|
|
title=title,
|
|
user=user)
|