2023-03-21 09:50:29 +00:00
|
|
|
from flask import flash, redirect, render_template, request, url_for
|
2023-03-14 13:24:52 +00:00
|
|
|
from flask_breadcrumbs import register_breadcrumb
|
2022-02-08 11:26:20 +00:00
|
|
|
from app import db, hashids
|
2023-03-21 09:50:29 +00:00
|
|
|
from app.models import Corpus, Role, User, UserSettingJobStatusMailNotificationLevel
|
2023-03-23 16:42:51 +00:00
|
|
|
from app.settings.forms import EditNotificationsForm
|
|
|
|
from app.settings.forms import EditAccountForm
|
2021-09-13 09:45:43 +00:00
|
|
|
from . import bp
|
2022-02-08 11:26:20 +00:00
|
|
|
from .forms import AdminEditUserForm
|
2023-03-14 13:24:52 +00:00
|
|
|
from app.users.utils import (
|
|
|
|
user_endpoint_arguments_constructor as user_eac,
|
|
|
|
user_dynamic_list_constructor as user_dlc
|
|
|
|
)
|
2021-12-13 11:20:32 +00:00
|
|
|
|
|
|
|
|
2022-09-02 11:08:01 +00:00
|
|
|
@bp.route('')
|
2023-03-14 13:24:52 +00:00
|
|
|
@register_breadcrumb(bp, '.', '<i class="material-icons left">admin_panel_settings</i>Administration')
|
2023-03-22 11:06:33 +00:00
|
|
|
def admin():
|
2023-03-21 09:50:29 +00:00
|
|
|
return render_template(
|
|
|
|
'admin/admin.html.j2',
|
|
|
|
title='Administration'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/corpora')
|
|
|
|
@register_breadcrumb(bp, '.corpora', 'Corpora')
|
|
|
|
def corpora():
|
2023-03-23 16:42:51 +00:00
|
|
|
corpora = Corpus.query.all()
|
2023-03-21 09:50:29 +00:00
|
|
|
return render_template(
|
|
|
|
'admin/corpora.html.j2',
|
2023-03-23 16:42:51 +00:00
|
|
|
title='Corpora',
|
|
|
|
corpora=corpora
|
2023-03-21 09:50:29 +00:00
|
|
|
)
|
2021-01-13 10:57:46 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/users')
|
2023-03-14 13:24:52 +00:00
|
|
|
@register_breadcrumb(bp, '.users', '<i class="material-icons left">group</i>Users')
|
2020-10-23 08:17:14 +00:00
|
|
|
def users():
|
2023-03-23 16:42:51 +00:00
|
|
|
users = User.query.all()
|
2021-11-30 15:22:16 +00:00
|
|
|
return render_template(
|
2022-02-08 11:26:20 +00:00
|
|
|
'admin/users.html.j2',
|
2023-03-23 16:42:51 +00:00
|
|
|
title='Users',
|
|
|
|
users=users
|
2022-02-08 11:26:20 +00:00
|
|
|
)
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/users/<hashid:user_id>')
|
2023-03-14 13:24:52 +00:00
|
|
|
@register_breadcrumb(bp, '.users.entity', '', dynamic_list_constructor=user_dlc)
|
2019-11-15 10:45:04 +00:00
|
|
|
def user(user_id):
|
|
|
|
user = User.query.get_or_404(user_id)
|
2023-03-23 16:42:51 +00:00
|
|
|
return render_template(
|
|
|
|
'admin/user.html.j2',
|
|
|
|
title=user.username,
|
|
|
|
user=user
|
|
|
|
)
|
2019-08-15 13:56:53 +00:00
|
|
|
|
|
|
|
|
2022-02-08 11:26:20 +00:00
|
|
|
@bp.route('/users/<hashid:user_id>/edit', methods=['GET', 'POST'])
|
2023-03-14 13:24:52 +00:00
|
|
|
@register_breadcrumb(bp, '.users.entity.edit', 'Edit', endpoint_arguments_constructor=user_eac)
|
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)
|
2022-02-08 11:26:20 +00:00
|
|
|
admin_edit_user_form = AdminEditUserForm(
|
2022-11-24 11:24:29 +00:00
|
|
|
data={'confirmed': user.confirmed, 'role': user.role.hashid},
|
2022-09-02 11:08:01 +00:00
|
|
|
prefix='admin-edit-user-form'
|
2022-02-08 11:26:20 +00:00
|
|
|
)
|
2023-03-23 16:42:51 +00:00
|
|
|
edit_profile_settings_form = EditAccountForm(
|
2022-02-08 11:26:20 +00:00
|
|
|
user,
|
2022-11-24 11:24:29 +00:00
|
|
|
data=user.to_json_serializeable(),
|
2022-11-30 13:36:42 +00:00
|
|
|
prefix='edit-profile-settings-form'
|
2022-02-08 11:26:20 +00:00
|
|
|
)
|
2023-03-23 16:42:51 +00:00
|
|
|
edit_notification_settings_form = EditNotificationsForm(
|
2022-11-24 11:24:29 +00:00
|
|
|
data=user.to_json_serializeable(),
|
2022-09-02 11:08:01 +00:00
|
|
|
prefix='edit-notification-settings-form'
|
2022-02-08 11:26:20 +00:00
|
|
|
)
|
2022-09-02 11:08:01 +00:00
|
|
|
if (admin_edit_user_form.submit.data
|
|
|
|
and admin_edit_user_form.validate()):
|
2022-02-08 11:26:20 +00:00
|
|
|
user.confirmed = admin_edit_user_form.confirmed.data
|
|
|
|
role_id = hashids.decode(admin_edit_user_form.role.data)
|
|
|
|
user.role = Role.query.get(role_id)
|
2022-09-02 11:08:01 +00:00
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Your changes have been saved')
|
|
|
|
return redirect(url_for('.edit_user', user_id=user.id))
|
2022-11-30 13:36:42 +00:00
|
|
|
if (edit_profile_settings_form.submit.data
|
|
|
|
and edit_profile_settings_form.validate()):
|
|
|
|
user.email = edit_profile_settings_form.email.data
|
|
|
|
user.username = edit_profile_settings_form.username.data
|
2022-02-08 11:26:20 +00:00
|
|
|
db.session.commit()
|
|
|
|
flash('Your changes have been saved')
|
|
|
|
return redirect(url_for('.edit_user', user_id=user.id))
|
2022-09-02 11:08:01 +00:00
|
|
|
if (edit_notification_settings_form.submit.data
|
|
|
|
and edit_notification_settings_form.validate()):
|
2022-02-08 11:26:20 +00:00
|
|
|
user.setting_job_status_mail_notification_level = \
|
2022-02-09 15:02:37 +00:00
|
|
|
UserSettingJobStatusMailNotificationLevel[
|
2022-02-08 11:26:20 +00:00
|
|
|
edit_notification_settings_form.job_status_mail_notification_level.data # noqa
|
|
|
|
]
|
2019-09-10 11:49:01 +00:00
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Your changes have been saved')
|
2021-01-13 10:57:46 +00:00
|
|
|
return redirect(url_for('.edit_user', user_id=user.id))
|
2021-12-13 11:20:32 +00:00
|
|
|
return render_template(
|
2022-02-08 11:26:20 +00:00
|
|
|
'admin/edit_user.html.j2',
|
|
|
|
admin_edit_user_form=admin_edit_user_form,
|
2022-11-30 13:36:42 +00:00
|
|
|
edit_profile_settings_form=edit_profile_settings_form,
|
2022-02-08 11:26:20 +00:00
|
|
|
edit_notification_settings_form=edit_notification_settings_form,
|
|
|
|
title='Edit user',
|
|
|
|
user=user
|
|
|
|
)
|