nopaque/app/profile/views.py

105 lines
4.3 KiB
Python
Raw Normal View History

2019-11-12 12:36:22 +00:00
from app import db, logger
from app.utils import background_delete_user
from flask import abort, current_app, flash, redirect, render_template, url_for
from flask_login import current_user, login_required, logout_user
from . import profile
2019-10-09 14:10:30 +00:00
from .forms import ChangePasswordForm, EditProfileForm, EditUserSettingsForm
import threading
@profile.route('/', methods=['GET', 'POST'])
@login_required
def index():
"""
View where loged in User can change own User information like Password etc.
"""
edit_user_info_form = EditProfileForm(user=current_user)
edit_user_info_form.email.data = current_user.email
return render_template('profile/index.html.j2',
change_password_form=ChangePasswordForm(),
edit_user_info_form=edit_user_info_form,
edit_user_settings_form=EditUserSettingsForm(),
title='Profile')
2019-10-09 14:10:30 +00:00
@profile.route('/change_password', methods=['POST'])
@login_required
def profile_change_password():
edit_user_info_form = EditProfileForm(user=current_user)
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 render_template('profile/index.html.j2',
change_password_form=change_password_form,
edit_user_info_form=edit_user_info_form,
edit_user_settings_form=EditUserSettingsForm(),
title='Profile')
else:
flash('Invalid password.')
return render_template('profile/index.html.j2',
change_password_form=change_password_form,
edit_user_info_form=edit_user_info_form,
edit_user_settings_form=EditUserSettingsForm(),
title='Profile')
2019-10-09 14:10:30 +00:00
@profile.route('/edit_user_info', methods=['POST'])
@login_required
def profile_edit_user_info():
edit_user_info_form = EditProfileForm(user=current_user)
2019-10-31 12:19:18 +00:00
if edit_user_info_form.validate_on_submit():
current_user.email = edit_user_info_form.email.data
db.session.add(current_user._get_current_object())
db.session.commit()
flash('Your email has been updated.')
else:
logger.warning('Form: {}'.format(edit_user_info_form.errors))
return render_template('profile/index.html.j2',
change_password_form=ChangePasswordForm(),
edit_user_info_form=edit_user_info_form,
edit_user_settings_form=EditUserSettingsForm(),
title='Profile')
edit_user_info_form.email.data = current_user.email
2019-10-31 12:19:18 +00:00
return render_template('profile/index.html.j2',
change_password_form=ChangePasswordForm(),
edit_user_info_form=EditProfileForm(user=current_user),
edit_user_settings_form=EditUserSettingsForm(),
title='Profile')
@profile.route('/edit_user_settings', methods=['POST'])
@login_required
def profile_edit_user_settings():
edit_user_settings_form = EditUserSettingsForm()
if not edit_user_settings_form.validate_on_submit():
abort(400)
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()
if current_user.is_dark is True:
flash('Dark mode has been activated!')
else:
flash('Dark mode has been deactivated!')
return redirect(url_for('profile.index'))
@profile.route('/delete_self', methods=['GET', 'POST'])
@login_required
def delete_self():
2019-10-09 14:10:30 +00:00
"""
View to delete yourslef and all associated data.
2019-10-09 14:10:30 +00:00
"""
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'))