2023-03-17 14:56:37 +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-20 15:00:25 +00:00
|
|
|
from app.models import Avatar, ProfilePrivacySettings
|
2023-03-17 14:56:37 +00:00
|
|
|
from . import bp
|
|
|
|
from .forms import (
|
|
|
|
ChangePasswordForm,
|
|
|
|
EditNotificationSettingsForm,
|
|
|
|
EditProfileSettingsForm,
|
|
|
|
EditPublicProfileInformationForm
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-20 15:00:25 +00:00
|
|
|
@bp.route('', methods=['GET', 'POST'])
|
|
|
|
@register_breadcrumb(bp, '.', '<i class="material-icons left">settings</i>Settings')
|
2023-03-17 14:56:37 +00:00
|
|
|
@login_required
|
2023-03-20 15:00:25 +00:00
|
|
|
def settings():
|
2023-03-17 14:56:37 +00:00
|
|
|
# region forms
|
|
|
|
edit_profile_settings_form = EditProfileSettingsForm(
|
|
|
|
current_user,
|
|
|
|
data=current_user.to_json_serializeable(),
|
|
|
|
prefix='edit-profile-settings-form'
|
|
|
|
)
|
|
|
|
edit_public_profile_information_form = EditPublicProfileInformationForm(
|
|
|
|
data=current_user.to_json_serializeable(),
|
|
|
|
prefix='edit-public-profile-information-form'
|
|
|
|
)
|
|
|
|
change_password_form = ChangePasswordForm(
|
|
|
|
current_user,
|
|
|
|
prefix='change-password-form'
|
|
|
|
)
|
|
|
|
edit_notification_settings_form = EditNotificationSettingsForm(
|
|
|
|
data=current_user.to_json_serializeable(),
|
|
|
|
prefix='edit-notification-settings-form'
|
|
|
|
)
|
|
|
|
# endregion forms
|
|
|
|
# region handle edit profile settings form
|
|
|
|
if edit_profile_settings_form.validate_on_submit():
|
|
|
|
current_user.email = edit_profile_settings_form.email.data
|
|
|
|
current_user.username = edit_profile_settings_form.username.data
|
|
|
|
db.session.commit()
|
|
|
|
flash('Profile settings updated')
|
2023-03-20 15:00:25 +00:00
|
|
|
return redirect(url_for('.settings'))
|
2023-03-17 14:56:37 +00:00
|
|
|
# endregion handle edit profile settings forms
|
|
|
|
# region handle edit public profile information form
|
|
|
|
if edit_public_profile_information_form.submit.data and edit_public_profile_information_form.validate():
|
|
|
|
print(edit_public_profile_information_form.show_email.data)
|
|
|
|
if edit_public_profile_information_form.show_email.data:
|
|
|
|
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL)
|
|
|
|
else:
|
|
|
|
current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL)
|
|
|
|
if edit_public_profile_information_form.show_last_seen.data:
|
|
|
|
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN)
|
|
|
|
else:
|
|
|
|
current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN)
|
|
|
|
if edit_public_profile_information_form.show_member_since.data:
|
|
|
|
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
|
|
|
|
else:
|
|
|
|
current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
|
|
|
|
if edit_public_profile_information_form.avatar.data:
|
|
|
|
try:
|
|
|
|
Avatar.create(edit_public_profile_information_form.avatar.data, user=current_user)
|
|
|
|
except (AttributeError, OSError):
|
|
|
|
abort(500)
|
|
|
|
current_user.about_me = edit_public_profile_information_form.about_me.data
|
|
|
|
current_user.location = edit_public_profile_information_form.location.data
|
|
|
|
current_user.organization = edit_public_profile_information_form.organization.data
|
|
|
|
current_user.website = edit_public_profile_information_form.website.data
|
|
|
|
current_user.full_name = edit_public_profile_information_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'))
|
2023-03-17 14:56:37 +00:00
|
|
|
# endregion handle edit public profile information form
|
|
|
|
# region handle change_password_form POST
|
|
|
|
if change_password_form.submit.data and change_password_form.validate():
|
|
|
|
current_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'))
|
2023-03-17 14:56:37 +00:00
|
|
|
# endregion handle change_password_form POST
|
|
|
|
# region handle edit_notification_settings_form POST
|
|
|
|
if edit_notification_settings_form.submit and edit_notification_settings_form.validate():
|
|
|
|
current_user.setting_job_status_mail_notification_level = edit_notification_settings_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-17 14:56:37 +00:00
|
|
|
# endregion handle edit_notification_settings_form POST
|
|
|
|
return render_template(
|
2023-03-20 15:00:25 +00:00
|
|
|
'settings/settings.html.j2',
|
2023-03-17 14:56:37 +00:00
|
|
|
edit_profile_settings_form=edit_profile_settings_form,
|
|
|
|
edit_public_profile_information_form=edit_public_profile_information_form,
|
|
|
|
change_password_form=change_password_form,
|
|
|
|
edit_notification_settings_form=edit_notification_settings_form,
|
|
|
|
title='Settings'
|
|
|
|
)
|