nopaque/app/profile/views.py
2019-10-09 16:10:30 +02:00

66 lines
2.4 KiB
Python

from app.utils import background_delete_user
from flask import current_app, flash, redirect, render_template, url_for
from flask_login import current_user, login_required, logout_user
from . import profile
from .forms import ChangePasswordForm, EditProfileForm, EditUserSettingsForm
from .. import db
import threading
import logging
logger = logging.getLogger(__name__)
@profile.route('/', methods=['GET', 'POST'])
@login_required
def index():
"""
View where loged in User can change own User information like Password etc.
"""
change_password_form = ChangePasswordForm()
if change_password_form.validate_on_submit():
if current_user.verify_password(change_password_form.old_password.data):
current_user.password = change_password_form.new_password.data
db.session.add(current_user)
db.session.commit()
flash('Your password has been updated.')
return redirect(url_for('profile.index'))
else:
flash('Invalid password.')
change_profile_form = EditProfileForm(user=current_user)
if change_profile_form.validate_on_submit():
current_user.email = change_profile_form.email.data
db.session.add(current_user._get_current_object())
db.session.commit()
flash('Your email has been updated.')
change_profile_form.email.data = current_user.email
edit_user_settings_form = EditUserSettingsForm()
if edit_user_settings_form.validate_on_submit():
current_user.is_dark = edit_user_settings_form.is_dark.data
logger.warning('Form data: {}'.format(current_user.is_dark))
db.session.add(current_user)
db.session.commit()
return render_template('profile/index.html.j2',
change_password_form=change_password_form,
change_profile_form=change_profile_form,
edit_user_settings_form=edit_user_settings_form,
title='Profile')
@profile.route('/delete_self', methods=['GET', 'POST'])
@login_required
def delete_self():
"""
Vie to delete yourslef and all associated data.
"""
delete_thread = threading.Thread(
target=background_delete_user,
args=(current_app._get_current_object(), current_user.id)
)
delete_thread.start()
logout_user()
flash('Your account has been deleted!')
return redirect(url_for('main.index'))