nopaque/app/admin/routes.py
2023-03-27 10:22:43 +02:00

174 lines
6.5 KiB
Python

from flask import abort, flash, redirect, render_template, url_for
from flask_breadcrumbs import register_breadcrumb
from app import db
from app.models import Avatar, Corpus, User
from app.settings.forms import (
ChangePasswordForm,
EditNotificationsForm,
EditAccountForm,
EditProfileForm
)
from . import bp
from .forms import AdminEditUserForm
from app.users.utils import (
user_endpoint_arguments_constructor as user_eac,
user_dynamic_list_constructor as user_dlc
)
@bp.route('')
@register_breadcrumb(bp, '.', '<i class="material-icons left">admin_panel_settings</i>Administration')
def admin():
return render_template(
'admin/admin.html.j2',
title='Administration'
)
@bp.route('/corpora')
@register_breadcrumb(bp, '.corpora', 'Corpora')
def corpora():
corpora = Corpus.query.all()
return render_template(
'admin/corpora.html.j2',
title='Corpora',
corpora=corpora
)
@bp.route('/users')
@register_breadcrumb(bp, '.users', '<i class="material-icons left">group</i>Users')
def users():
users = User.query.all()
return render_template(
'admin/users.html.j2',
title='Users',
users=users
)
@bp.route('/users/<hashid:user_id>')
@register_breadcrumb(bp, '.users.entity', '', dynamic_list_constructor=user_dlc)
def user(user_id):
user = User.query.get_or_404(user_id)
return render_template(
'admin/user.html.j2',
title=user.username,
user=user
)
@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)
# region forms
edit_account_form = EditAccountForm(user=user)
edit_profile_form = EditProfileForm(user=user)
change_password_form = ChangePasswordForm(user=user)
edit_notifications_form = EditNotificationsForm(user=user)
# endregion forms
# region handle edit profile settings form
if edit_account_form.validate_on_submit():
user.email = edit_account_form.email.data
user.username = edit_account_form.username.data
db.session.commit()
flash('Profile settings updated')
return redirect(url_for('.user_settings'))
# endregion handle edit profile settings forms
# region handle edit public profile information form
if edit_profile_form.validate_on_submit():
if edit_profile_form.avatar.data:
try:
Avatar.create(
edit_profile_form.avatar.data,
user=user
)
except (AttributeError, OSError):
abort(500)
user.about_me = edit_profile_form.about_me.data
user.location = edit_profile_form.location.data
user.organization = edit_profile_form.organization.data
user.website = edit_profile_form.website.data
user.full_name = edit_profile_form.full_name.data
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.user_settings'))
# endregion handle edit public profile information form
# region handle change_password_form POST
if change_password_form.validate_on_submit():
user.password = change_password_form.new_password.data
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.user_settings'))
# endregion handle change_password_form POST
# region handle edit_notification_settings_form POST
if edit_notifications_form.validate_on_submit():
user.setting_job_status_mail_notification_level = \
edit_notifications_form.job_status_mail_notification_level.data
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.user_settings'))
# endregion handle edit_notification_settings_form POST
return render_template(
'admin/user_settings.html.j2',
title='Settings',
change_password_form=change_password_form,
edit_account_form=edit_account_form,
edit_notifications_form=edit_notifications_form,
edit_profile_form=edit_profile_form,
user=user
)
# @bp.route('/users/<hashid:user_id>/edit', methods=['GET', 'POST'])
# @register_breadcrumb(bp, '.users.entity.edit', 'Edit', endpoint_arguments_constructor=user_eac)
# def edit_user(user_id):
# user = User.query.get_or_404(user_id)
# admin_edit_user_form = AdminEditUserForm(
# data={'confirmed': user.confirmed, 'role': user.role.hashid},
# prefix='admin-edit-user-form'
# )
# edit_profile_settings_form = EditAccountForm(
# user,
# data=user.to_json_serializeable(),
# prefix='edit-profile-settings-form'
# )
# edit_notification_settings_form = EditNotificationsForm(
# data=user.to_json_serializeable(),
# prefix='edit-notification-settings-form'
# )
# if (admin_edit_user_form.submit.data
# and admin_edit_user_form.validate()):
# 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)
# db.session.commit()
# flash('Your changes have been saved')
# return redirect(url_for('.edit_user', user_id=user.id))
# 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
# db.session.commit()
# flash('Your changes have been saved')
# return redirect(url_for('.edit_user', user_id=user.id))
# if (edit_notification_settings_form.submit.data
# and edit_notification_settings_form.validate()):
# user.setting_job_status_mail_notification_level = \
# UserSettingJobStatusMailNotificationLevel[
# edit_notification_settings_form.job_status_mail_notification_level.data # noqa
# ]
# db.session.commit()
# flash('Your changes have been saved')
# return redirect(url_for('.edit_user', user_id=user.id))
# return render_template(
# 'admin/edit_user.html.j2',
# admin_edit_user_form=admin_edit_user_form,
# edit_profile_settings_form=edit_profile_settings_form,
# edit_notification_settings_form=edit_notification_settings_form,
# title='Edit user',
# user=user
# )