diff --git a/app/auth/forms.py b/app/auth/forms.py index 606c069f..4a524cfa 100644 --- a/app/auth/forms.py +++ b/app/auth/forms.py @@ -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!') diff --git a/app/auth/views.py b/app/auth/views.py index 7e963e36..d3535ac2 100644 --- a/app/auth/views.py +++ b/app/auth/views.py @@ -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' ) diff --git a/app/models.py b/app/models.py index bf62399c..0f28882f 100644 --- a/app/models.py +++ b/app/models.py @@ -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. diff --git a/app/templates/admin/admin_user_page.html.j2 b/app/templates/admin/admin_user_page.html.j2 index 49b1a35d..6f892a0a 100644 --- a/app/templates/admin/admin_user_page.html.j2 +++ b/app/templates/admin/admin_user_page.html.j2 @@ -58,7 +58,7 @@ Administration actions - deleteDelete User + deleteDelete User