nopaque/app/settings/routes.py

89 lines
3.5 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 (
ChangePasswordForm,
2023-03-23 16:42:51 +00:00
EditNotificationsForm,
EditAccountForm,
EditProfileForm
)
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-23 16:42:51 +00:00
user = current_user._get_current_object()
# region forms
2023-03-23 16:42:51 +00:00
edit_account_form = EditAccountForm()
edit_profile_form = EditProfileForm()
change_password_form = ChangePasswordForm()
edit_notifications_form = EditNotificationsForm()
# endregion forms
# region handle edit profile settings form
2023-03-23 16:42:51 +00:00
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')
2023-03-20 15:00:25 +00:00
return redirect(url_for('.settings'))
# endregion handle edit profile settings forms
# region handle edit public profile information form
2023-03-23 16:42:51 +00:00
if edit_profile_form.validate_on_submit():
if edit_profile_form.show_email.data:
user.add_profile_privacy_setting('SHOW_EMAIL')
else:
2023-03-23 16:42:51 +00:00
user.remove_profile_privacy_setting('SHOW_EMAIL')
if edit_profile_form.show_last_seen.data:
user.add_profile_privacy_setting('SHOW_LAST_SEEN')
else:
2023-03-23 16:42:51 +00:00
user.remove_profile_privacy_setting('SHOW_LAST_SEEN')
if edit_profile_form.show_member_since.data:
user.add_profile_privacy_setting('SHOW_MEMBER_SINCE')
else:
2023-03-23 16:42:51 +00:00
user.remove_profile_privacy_setting('SHOW_MEMBER_SINCE')
if edit_profile_form.avatar.data:
try:
2023-03-23 16:42:51 +00:00
Avatar.create(
edit_profile_form.avatar.data,
user=user
)
except (AttributeError, OSError):
abort(500)
2023-03-23 16:42:51 +00:00
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()
2023-03-20 15:00:25 +00:00
flash('Your changes have been saved')
return redirect(url_for('.settings'))
# endregion handle edit public profile information form
# region handle change_password_form POST
2023-03-23 16:42:51 +00:00
if change_password_form.validate_on_submit():
user.password = change_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'))
# endregion handle change_password_form POST
# region handle edit_notification_settings_form POST
2023-03-23 16:42:51 +00:00
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')
2023-03-20 15:00:25 +00:00
return redirect(url_for('.settings'))
# endregion handle edit_notification_settings_form POST
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',
change_password_form=change_password_form,
2023-03-23 16:42:51 +00:00
edit_account_form=edit_account_form,
edit_notifications_form=edit_notifications_form,
edit_profile_form=edit_profile_form,
user=user
)