nopaque/app/admin/routes.py

147 lines
5.5 KiB
Python
Raw Normal View History

2023-03-27 08:22:43 +00:00
from flask import abort, flash, redirect, render_template, url_for
2023-03-14 13:24:52 +00:00
from flask_breadcrumbs import register_breadcrumb
2023-03-29 12:32:35 +00:00
from app import db, hashids
from app.models import Avatar, Corpus, Role, User
from app.users.settings.forms import (
2023-03-29 12:32:35 +00:00
UpdateAvatarForm,
UpdatePasswordForm,
UpdateNotificationsForm,
UpdateAccountInformationForm,
UpdateProfileInformationForm
2023-03-27 08:22:43 +00:00
)
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
)
2024-04-10 11:34:48 +00:00
from . import bp
from .forms import UpdateUserForm
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
)
@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(
'admin/users.html.j2',
2023-03-23 16:42:51 +00:00
title='Users',
users=users
)
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-06-07 09:23:59 +00:00
corpora = Corpus.query.filter(Corpus.user == user).all()
2023-03-23 16:42:51 +00:00
return render_template(
'admin/user.html.j2',
title=user.username,
2023-06-07 09:23:59 +00:00
user=user,
corpora=corpora
2023-03-23 16:42:51 +00:00
)
2023-03-27 08:22:43 +00:00
@bp.route('/users/<hashid:user_id>/settings', methods=['GET', 'POST'])
@register_breadcrumb(bp, '.users.entity.settings', '<i class="material-icons left">settings</i>Settings')
def user_settings(user_id):
user = User.query.get_or_404(user_id)
update_account_information_form = UpdateAccountInformationForm(user)
update_profile_information_form = UpdateProfileInformationForm(user)
update_avatar_form = UpdateAvatarForm()
update_password_form = UpdatePasswordForm(user)
update_notifications_form = UpdateNotificationsForm(user)
update_user_form = UpdateUserForm(user)
2023-03-29 12:32:35 +00:00
# region handle update profile information form
if update_profile_information_form.submit.data and update_profile_information_form.validate():
user.about_me = update_profile_information_form.about_me.data
user.location = update_profile_information_form.location.data
user.organization = update_profile_information_form.organization.data
user.website = update_profile_information_form.website.data
user.full_name = update_profile_information_form.full_name.data
2023-03-27 08:22:43 +00:00
db.session.commit()
2023-03-29 12:32:35 +00:00
flash('Your changes have been saved')
return redirect(url_for('.user_settings', user_id=user.id))
# endregion handle update profile information form
# region handle update avatar form
if update_avatar_form.submit.data and update_avatar_form.validate():
try:
Avatar.create(
update_avatar_form.avatar.data,
user=user
)
except (AttributeError, OSError):
abort(500)
2022-09-02 11:08:01 +00:00
db.session.commit()
flash('Your changes have been saved')
2023-03-29 12:32:35 +00:00
return redirect(url_for('.user_settings', user_id=user.id))
# endregion handle update avatar form
# region handle update account information form
if update_account_information_form.submit.data and update_account_information_form.validate():
user.email = update_account_information_form.email.data
user.username = update_account_information_form.username.data
db.session.commit()
flash('Profile settings updated')
return redirect(url_for('.user_settings', user_id=user.id))
# endregion handle update account information form
# region handle update password form
if update_password_form.submit.data and update_password_form.validate():
user.password = update_password_form.new_password.data
db.session.commit()
flash('Your changes have been saved')
2023-03-29 12:32:35 +00:00
return redirect(url_for('.user_settings', user_id=user.id))
# endregion handle update password form
# region handle update notifications form
if update_notifications_form.submit.data and update_notifications_form.validate():
user.setting_job_status_mail_notification_level = \
2023-03-29 12:32:35 +00:00
update_notifications_form.job_status_mail_notification_level.data
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.user_settings', user_id=user.id))
# endregion handle update notifications form
# region handle update user form
if update_user_form.submit.data and update_user_form.validate():
role_id = hashids.decode(update_user_form.role.data)
user.role = Role.query.get(role_id)
db.session.commit()
flash('Your changes have been saved')
2023-03-29 12:32:35 +00:00
return redirect(url_for('.user_settings', user_id=user.id))
# endregion handle update user form
return render_template(
2023-03-27 08:22:43 +00:00
'admin/user_settings.html.j2',
title='Settings',
2023-03-29 12:32:35 +00:00
update_account_information_form=update_account_information_form,
update_avatar_form=update_avatar_form,
update_notifications_form=update_notifications_form,
update_password_form=update_password_form,
update_profile_information_form=update_profile_information_form,
update_user_form=update_user_form,
user=user
)