Add function to change user email

This commit is contained in:
Stephan Porada 2019-09-09 16:17:59 +02:00
parent 97517339ff
commit 8786defa01
7 changed files with 103 additions and 55 deletions

View File

@ -1,5 +1,5 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, ValidationError
from wtforms import StringField, PasswordField, BooleanField, SubmitField, ValidationError, TextAreaField
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
from ..models import User
@ -76,3 +76,18 @@ class ChangePasswordForm(FlaskForm):
]
)
submit = SubmitField('Update Password')
class EditProfileForm(FlaskForm):
email = StringField('Change Email', validators=[Length(0, 64),
DataRequired()])
submit = SubmitField('Change Email')
def __init__(self, user, *args, **kwargs):
super(EditProfileForm, self).__init__(*args, **kwargs)
self.user = user
def validate_email(self, field):
if field.data != self.user.email and \
User.query.filter_by(email=field.data).first():
raise ValidationError('Email already registered!')

View File

@ -2,7 +2,7 @@ from flask import flash, redirect, render_template, request, url_for
from flask_login import current_user, login_required, login_user, logout_user
from . import auth
from .. import db
from .forms import ChangePasswordForm, LoginForm, PasswordResetForm, PasswordResetRequestForm, RegistrationForm
from .forms import ChangePasswordForm, LoginForm, PasswordResetForm, PasswordResetRequestForm, RegistrationForm, EditProfileForm
from ..email import send_email
from ..models import User
@ -131,9 +131,9 @@ def password_reset(token):
title='Password Reset')
@auth.route('/settings', methods=['GET', 'POST'])
@auth.route('/edit_profile', methods=['GET', 'POST'])
@login_required
def settings():
def edit_profile():
"""
View where loged in User can change own User information like Password etc.
"""
@ -144,11 +144,19 @@ def settings():
db.session.add(current_user)
db.session.commit()
flash('Your password has been updated.')
return redirect(url_for('auth.settings'))
return redirect(url_for('auth.edit_profile'))
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
return render_template(
'auth/settings.html.j2',
form=change_password_form,
title='Settings'
'auth/edit_profile.html.j2',
change_password_form=change_password_form,
change_profile_form=change_profile_form,
title='Edit Profile'
)

View File

@ -216,6 +216,7 @@ class User(UserMixin, db.Model):
jobs[str(job.id)] = job.to_dict()
return jobs
class AnonymousUser(AnonymousUserMixin):
"""
Model replaces the default AnonymousUser.

View File

@ -58,7 +58,7 @@
<span class="card-title">Administration actions</span>
<!-- Confirm deletion of selected user with modal dialogue
Modal Trigger-->
<a href="#modal-confirm-delete" class="waves-effect waves-light btn modal-trigger"><i class="material-icons left">delete</i>Delete User</a>
<a href="#modal-confirm-delete" class="waves-effect waves-light btn red modal-trigger"><i class="material-icons left">delete</i>Delete User</a>
<!-- Modal Strucutre -->
<div id="modal-confirm-delete" class="modal">
<div class="modal-content">

View File

@ -0,0 +1,69 @@
{% extends "base.html.j2" %}
{% block page_content %}
<div class="col s12 m4">
<h3>Change password</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,</p>
</div>
<div class="col s12 m8">
<div class="card">
<form method="POST">
<div class="card-content">
{{ change_password_form.hidden_tag() }}
<div class="input-field ">
<i class="material-icons prefix">vpn_key</i>
{{ change_password_form.old_password() }}
{{ change_password_form.old_password.label }}
{% for error in change_password_form.old_password.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<div class="input-field">
<i class="material-icons prefix">vpn_key</i>
{{ change_password_form.new_password() }}
{{ change_password_form.new_password.label }}
{% for error in change_password_form.new_password.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<div class="input-field">
<i class="material-icons prefix">vpn_key</i>
{{ change_password_form.new_password2() }}
{{ change_password_form.new_password2.label }}
{% for error in change_password_form.new_password2.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
<div class="card-action right-align">
{{ change_password_form.submit(class='btn') }}
</div>
</form>
</div>
</div>
<div class="col s12 m4">
<h3>Change email</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,</p>
</div>
<div class="col s12 m8">
<div class="card">
<form method="POST">
<div class="card-content">
{{ change_profile_form.hidden_tag() }}
<div class="input-field ">
<i class="material-icons prefix">mail</i>
{{ change_profile_form.email() }}
{{ change_profile_form.email.label }}
{% for error in change_profile_form.email.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
<div class="card-action right-align">
{{ change_profile_form.submit(class='btn') }}
</div>
</form>
</div>
</div>
{% endblock %}

View File

@ -1,45 +0,0 @@
{% extends "base.html.j2" %}
{% block page_content %}
<div class="col s12 m4">
<h3>Change password</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,</p>
</div>
<div class="col s12 m8">
<div class="card">
<form method="POST">
<div class="card-content">
{{ form.hidden_tag() }}
<div class="input-field ">
<i class="material-icons prefix">vpn_key</i>
{{ form.old_password() }}
{{ form.old_password.label }}
{% for error in form.old_password.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<div class="input-field">
<i class="material-icons prefix">vpn_key</i>
{{ form.new_password() }}
{{ form.new_password.label }}
{% for error in form.new_password.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<div class="input-field">
<i class="material-icons prefix">vpn_key</i>
{{ form.new_password2() }}
{{ form.new_password2.label }}
{% for error in form.new_password2.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
<div class="card-action right-align">
{{ form.submit(class='btn') }}
</div>
</form>
</div>
</div>
{% endblock %}

View File

@ -72,7 +72,7 @@
</div>
<ul id="nav-account-dropdown" class="dropdown-content">
{% if current_user.is_authenticated %}
<li><a href="{{ url_for('auth.settings') }}"><i class="material-icons">settings</i>Settings</a></li>
<li><a href="{{ url_for('auth.edit_profile') }}"><i class="material-icons">edit_profile</i>Edit Profile</a></li>
<li><a href="{{ url_for('auth.logout') }}"><i class="material-icons">chevron_left</i>Log out</a></li>
{% else %}
<li><a href="{{ url_for('auth.login') }}"><i class="material-icons">chevron_right</i>Log in</a></li>