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 from app.models import Avatar from . import bp from .forms import ( ChangePasswordForm, EditNotificationsForm, EditAccountForm, EditProfileForm ) @bp.route('', methods=['GET', 'POST']) @register_breadcrumb(bp, '.', 'settingsSettings') @login_required def settings(): user = current_user._get_current_object() # region forms edit_account_form = EditAccountForm() edit_profile_form = EditProfileForm() change_password_form = ChangePasswordForm() edit_notifications_form = EditNotificationsForm() # 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('.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.show_email.data: user.add_profile_privacy_setting('SHOW_EMAIL') else: user.remove_profile_privacy_setting('SHOW_EMAIL') if edit_profile_form.show_last_seen.data: user.add_profile_privacy_setting('SHOW_LAST_SEEN') else: 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: user.remove_profile_privacy_setting('SHOW_MEMBER_SINCE') 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('.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('.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('.settings')) # endregion handle edit_notification_settings_form POST return render_template( 'settings/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 )