nopaque/app/settings/routes.py

40 lines
1.7 KiB
Python
Raw Normal View History

2022-12-22 15:02:12 +00:00
from flask import flash, redirect, render_template, url_for
2022-09-02 11:07:30 +00:00
from flask_login import current_user, login_required
from app import db
2022-12-22 15:02:12 +00:00
from app.models import UserSettingJobStatusMailNotificationLevel
2022-09-02 11:07:30 +00:00
from . import bp
2022-12-22 15:02:12 +00:00
from .forms import ChangePasswordForm, EditNotificationSettingsForm
@bp.route('', methods=['GET', 'POST'])
@login_required
2022-09-02 11:07:30 +00:00
def settings():
change_password_form = ChangePasswordForm(
2022-09-02 11:07:30 +00:00
current_user,
prefix='change-password-form'
)
edit_notification_settings_form = EditNotificationSettingsForm(
2022-11-24 11:24:29 +00:00
data=current_user.to_json_serializeable(),
2022-09-02 11:07:30 +00:00
prefix='edit-notification-settings-form'
)
2022-12-22 15:02:12 +00:00
# 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')
2022-12-22 15:02:12 +00:00
return redirect(url_for('.settings'))
# 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')
2022-09-02 11:07:30 +00:00
return redirect(url_for('.settings'))
2022-12-22 15:02:12 +00:00
# endregion handle edit_notification_settings_form POST
return render_template(
2022-09-02 11:07:30 +00:00
'settings/settings.html.j2',
change_password_form=change_password_form,
edit_notification_settings_form=edit_notification_settings_form,
2022-11-30 13:36:42 +00:00
title='Settings'
)