nopaque/app/profile/views.py

70 lines
2.8 KiB
Python
Raw Normal View History

2020-02-20 09:03:42 +00:00
from app import db, logger
2020-02-19 13:49:52 +00:00
from flask import current_app, flash, redirect, render_template, url_for
from flask_login import current_user, login_required, logout_user
2019-11-15 11:51:53 +00:00
from threading import Thread
from . import profile
2019-11-14 12:40:05 +00:00
from .background_functions import delete_user_
2020-02-19 13:49:52 +00:00
from .forms import EditEmailForm, EditGeneralSettingsForm, EditPasswordForm
2020-02-19 13:49:52 +00:00
@profile.route('/settings', methods=['GET', 'POST'])
@login_required
2020-02-19 13:49:52 +00:00
def settings():
edit_email_form = EditEmailForm(prefix='edit-email-form')
edit_general_settings_form = EditGeneralSettingsForm(
2020-02-20 09:03:42 +00:00
prefix='edit-general-settings-form'
2020-02-19 13:49:52 +00:00
)
edit_password_form = EditPasswordForm(prefix='edit-password-form',
user=current_user)
# Check if edit_email_form is submitted and valid
if (edit_email_form.save_email.data
and edit_email_form.validate_on_submit()):
2020-02-20 09:03:42 +00:00
logger.warning('edit_email_form submitted')
2020-02-19 13:49:52 +00:00
db.session.add(current_user)
2019-10-31 12:19:18 +00:00
db.session.commit()
2020-02-19 13:49:52 +00:00
flash('Your email address has been updated.')
return redirect(url_for('profile.settings'))
# Check if edit_settings_form is submitted and valid
if (edit_general_settings_form.save_settings.data
and edit_general_settings_form.validate_on_submit()):
2020-02-20 09:03:42 +00:00
logger.warning('edit_general_settings_form submitted')
2020-02-19 13:49:52 +00:00
current_user.is_dark = edit_general_settings_form.dark_mode.data
db.session.add(current_user)
db.session.commit()
flash('Your settings have been updated.')
return redirect(url_for('profile.settings'))
# Check if edit_password_form is submitted and valid
if (edit_password_form.save_password.data
and edit_password_form.validate_on_submit()):
2020-02-20 09:03:42 +00:00
logger.warning('edit_password_form submitted')
2020-02-19 13:49:52 +00:00
current_user.password = edit_password_form.password.data
db.session.add(current_user)
db.session.commit()
flash('Your password has been updated.')
return redirect(url_for('profile.settings'))
# If no form is submitted or valid, fill out fields with current values
2020-02-20 09:03:42 +00:00
logger.warning('nothing submitted')
2020-02-19 13:49:52 +00:00
edit_email_form.email.data = current_user.email
edit_general_settings_form.dark_mode.data = current_user.is_dark
return render_template(
'profile/settings.html.j2',
edit_email_form=edit_email_form,
edit_password_form=edit_password_form,
edit_general_settings_form=edit_general_settings_form,
title='Settings'
)
2020-02-19 13:49:52 +00:00
@profile.route('/delete', methods=['GET', 'POST'])
@login_required
2020-02-19 13:49:52 +00:00
def delete():
2019-10-09 14:10:30 +00:00
"""
View to delete yourslef and all associated data.
2019-10-09 14:10:30 +00:00
"""
2019-11-15 11:51:53 +00:00
thread = Thread(target=delete_user_,
args=(current_app._get_current_object(), current_user.id))
thread.start()
2020-02-25 09:03:31 +00:00
logout_user()
flash('Your account has been deleted!')
return redirect(url_for('main.index'))