from flask import abort, flash, g, 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, User from ..utils import user_endpoint_arguments_constructor as user_eac from . import bp from .forms import ( UpdateAvatarForm, UpdatePasswordForm, UpdateNotificationsForm, UpdateAccountInformationForm, UpdateProfileInformationForm ) @bp.route('//settings', methods=['GET', 'POST']) @register_breadcrumb(bp, '.entity.settings', 'settingsSettings', endpoint_arguments_constructor=user_eac) @login_required def settings(user_id): user = User.query.get_or_404(user_id) if not (user == current_user or current_user.is_administrator()): abort(403) redirect_location_on_post = g.pop( '_nopaque_redirect_location_on_post', url_for('.settings', user_id=user_id) ) update_account_information_form = UpdateAccountInformationForm(user) update_profile_information_form = UpdateProfileInformationForm(user) update_avatar_form = UpdateAvatarForm() update_password_form = UpdatePasswordForm(user) update_notifications_form = UpdateNotificationsForm(user) # 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() flash('Your changes have been saved') return redirect(redirect_location_on_post) # 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() flash('Your changes have been saved') return redirect(redirect_location_on_post) # 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(redirect_location_on_post) # 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') return redirect(redirect_location_on_post) # endregion handle update password form # region handle update notifications form if update_notifications_form.submit.data and update_notifications_form.validate(): user.setting_job_status_mail_notification_level = \ update_notifications_form.job_status_mail_notification_level.data db.session.commit() flash('Your changes have been saved') return redirect(redirect_location_on_post) # endregion handle update notifications form return render_template( 'users/settings/settings.html.j2', title='Settings', 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, user=user )