2019-11-05 15:22:36 +00:00
|
|
|
from app import db
|
|
|
|
from app.decorators import admin_required
|
|
|
|
from app.models import Role, User
|
|
|
|
from app.tables import AdminUserItem, AdminUserTable
|
2019-09-23 14:25:26 +00:00
|
|
|
from app.utils import background_delete_user
|
|
|
|
from flask import current_app, flash, redirect, render_template, url_for
|
|
|
|
from flask_login import login_required
|
2019-08-15 13:56:53 +00:00
|
|
|
from . import admin
|
2019-09-23 14:25:26 +00:00
|
|
|
from .forms import EditProfileAdminForm
|
2019-09-17 14:31:41 +00:00
|
|
|
import threading
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admin.route('/overview', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def for_admins_only():
|
|
|
|
users = User.query.order_by(User.username).all()
|
2019-09-23 14:25:26 +00:00
|
|
|
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')
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2019-09-23 14:25:26 +00:00
|
|
|
@admin.route('/overview/admin_user_page/<int:user_id>',
|
|
|
|
methods=['GET', 'POST'])
|
2019-08-15 13:56:53 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def admin_user_page(user_id):
|
|
|
|
selected_user = User.query.filter_by(id=user_id).first()
|
2019-09-23 14:25:26 +00:00
|
|
|
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'
|
|
|
|
)
|
2019-08-15 13:56:53 +00:00
|
|
|
return render_template('admin/admin_user_page.html.j2',
|
2019-09-23 14:25:26 +00:00
|
|
|
registration_date=registration_date,
|
|
|
|
selected_user=selected_user,
|
|
|
|
title=title)
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2019-09-23 14:25:26 +00:00
|
|
|
@admin.route('/overview/admin_user_page/delete/<int:user_id>',
|
|
|
|
methods=['GET', 'POST'])
|
2019-08-15 13:56:53 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def admin_delete_user(user_id):
|
2019-09-23 14:25:26 +00:00
|
|
|
delete_thread = threading.Thread(
|
|
|
|
target=background_delete_user,
|
|
|
|
args=(current_app._get_current_object(), user_id)
|
|
|
|
)
|
2019-09-17 14:31:41 +00:00
|
|
|
delete_thread.start()
|
2019-08-15 13:56:53 +00:00
|
|
|
flash('User {} has been deleted!'.format(user_id))
|
|
|
|
return redirect(url_for('admin.for_admins_only'))
|
2019-09-10 11:49:01 +00:00
|
|
|
|
|
|
|
|
2019-09-23 14:25:26 +00:00
|
|
|
@admin.route('/overview/admin_user_page/edit_profile_admin/<int:user_id>',
|
|
|
|
methods=['GET', 'POST'])
|
2019-09-10 11:49:01 +00:00
|
|
|
@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
|
2019-09-23 14:25:26 +00:00
|
|
|
title = 'Edit profile of user {} with ID {}'.format(
|
|
|
|
user.username,
|
|
|
|
user.id
|
|
|
|
)
|
2019-09-10 11:49:01 +00:00
|
|
|
return render_template('admin/edit_profile_admin.html.j2',
|
|
|
|
form=form,
|
2019-09-23 14:25:26 +00:00
|
|
|
title=title,
|
|
|
|
user=user)
|