nopaque/app/settings/routes.py

89 lines
3.6 KiB
Python
Raw Normal View History

2023-03-23 16:42:51 +00:00
from flask import abort, flash, redirect, render_template, url_for
from flask_breadcrumbs import register_breadcrumb
from flask_login import current_user, login_required
from app import db
2023-03-23 16:42:51 +00:00
from app.models import Avatar
from . import bp
from .forms import (
2023-03-29 12:32:35 +00:00
UpdateAvatarForm,
UpdatePasswordForm,
UpdateNotificationsForm,
UpdateAccountInformationForm,
UpdateProfileInformationForm
)
2023-03-20 15:00:25 +00:00
@bp.route('', methods=['GET', 'POST'])
@register_breadcrumb(bp, '.', '<i class="material-icons left">settings</i>Settings')
@login_required
2023-03-20 15:00:25 +00:00
def settings():
2023-03-29 12:32:35 +00:00
user = current_user
update_account_information_form = UpdateAccountInformationForm()
update_profile_information_form = UpdateProfileInformationForm()
update_avatar_form = UpdateAvatarForm()
update_password_form = UpdatePasswordForm()
update_notifications_form = UpdateNotificationsForm()
# 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
db.session.commit()
2023-03-29 12:32:35 +00:00
flash('Your changes have been saved')
2023-03-20 15:00:25 +00:00
return redirect(url_for('.settings'))
2023-03-29 12:32:35 +00:00
# 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)
db.session.commit()
2023-03-20 15:00:25 +00:00
flash('Your changes have been saved')
return redirect(url_for('.settings'))
2023-03-29 12:32:35 +00:00
# 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('.settings'))
# 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-20 15:00:25 +00:00
return redirect(url_for('.settings'))
2023-03-29 12:32:35 +00:00
# endregion handle update password form
# region handle update notifications form
if update_notifications_form.submit.data and update_notifications_form.validate():
2023-03-23 16:42:51 +00:00
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')
2023-03-20 15:00:25 +00:00
return redirect(url_for('.settings'))
2023-03-29 12:32:35 +00:00
# endregion handle update notifications form
return render_template(
2023-03-20 15:00:25 +00:00
'settings/settings.html.j2',
2023-03-23 16:42:51 +00:00
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,
2023-03-23 16:42:51 +00:00
user=user
)