mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-03 20:02:47 +00:00 
			
		
		
		
	Add function to change user email
This commit is contained in:
		@@ -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!')
 | 
			
		||||
 
 | 
			
		||||
@@ -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'
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user