Codestyle for auth package.

This commit is contained in:
Patrick Jentsch 2019-09-23 16:39:36 +02:00
parent 5749c94bca
commit 4858f36d76
2 changed files with 49 additions and 41 deletions

View File

@ -1,7 +1,7 @@
from flask_wtf import FlaskForm
from wtforms import (StringField, PasswordField, BooleanField, SubmitField,
from wtforms import (BooleanField, PasswordField, StringField, SubmitField,
ValidationError)
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
from ..models import User
@ -12,6 +12,25 @@ class LoginForm(FlaskForm):
submit = SubmitField('Log In')
class PasswordResetForm(FlaskForm):
password = PasswordField(
'New Password',
validators=[DataRequired(),
EqualTo('password2', message='Passwords must match')]
)
password2 = PasswordField(
'Confirm password',
validators=[DataRequired(),
EqualTo('password', message='Passwords must match.')]
)
submit = SubmitField('Reset Password')
class PasswordResetRequestForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Reset Password')
class RegistrationForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
username = StringField(
@ -38,26 +57,3 @@ class RegistrationForm(FlaskForm):
def validate_username(self, field):
if User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')
class PasswordResetForm(FlaskForm):
password = PasswordField(
'New Password',
validators=[
DataRequired(),
EqualTo('password2', message='Passwords must match')
]
)
password2 = PasswordField(
'Confirm password',
validators=[
DataRequired(),
EqualTo('password', message='Passwords must match.')
]
)
submit = SubmitField('Reset Password')
class PasswordResetRequestForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Reset Password')

View File

@ -1,9 +1,9 @@
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 (LoginForm, PasswordResetForm, PasswordResetRequestForm,
RegistrationForm)
from .. import db
from ..email import send_email
from ..models import User
@ -42,16 +42,21 @@ def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(email=form.email.data.lower(),
username=form.username.data,
password=form.password.data)
password=form.password.data,
username=form.username.data)
db.session.add(user)
db.session.commit()
token = user.generate_confirmation_token()
send_email(user.email, 'Confirm Your Account',
'auth/email/confirm', user=user, token=token)
send_email(user.email,
'Confirm Your Account',
'auth/email/confirm',
token=token,
user=user)
flash('A confirmation email has been sent to you by email.')
return redirect(url_for('auth.login'))
return render_template('auth/register.html.j2', form=form, title='Register')
return render_template('auth/register.html.j2',
form=form,
title='Register')
@auth.route('/confirm/<token>')
@ -73,10 +78,10 @@ def before_request():
Checks if a user is unconfirmed when visiting specific sites. Redirects to
unconfirmed view if user is unconfirmed.
"""
if current_user.is_authenticated \
and not current_user.confirmed \
and request.blueprint != 'auth' \
and request.endpoint != 'static':
if (current_user.is_authenticated
and not current_user.confirmed
and request.blueprint != 'auth'
and request.endpoint != 'static'):
return redirect(url_for('auth.unconfirmed'))
@ -91,8 +96,11 @@ def unconfirmed():
@login_required
def resend_confirmation():
token = current_user.generate_confirmation_token()
send_email(current_user.email, 'Confirm Your Account', 'auth/email/confirm',
user=current_user, token=token)
send_email(current_user.email,
'Confirm Your Account',
'auth/email/confirm',
token=token,
user=current_user)
flash('A new confirmation email has benn sent to you by email.')
return redirect(url_for('main.dashboard'))
@ -106,13 +114,16 @@ def password_reset_request():
user = User.query.filter_by(email=form.email.data.lower()).first()
if user:
token = user.generate_reset_token()
send_email(user.email, 'Reset Your Password',
send_email(user.email,
'Reset Your Password',
'auth/email/reset_password',
user=user, token=token)
token=token,
user=user)
flash('An email with instructions to reset your password has been '
'sent to you.')
return redirect(url_for('auth.login'))
return render_template('auth/reset_password_request.html.j2', form=form,
return render_template('auth/reset_password_request.html.j2',
form=form,
title='Password Reset')
@ -128,5 +139,6 @@ def password_reset(token):
return redirect(url_for('auth.login'))
else:
return redirect(url_for('main.index'))
return render_template('auth/reset_password.html.j2', form=form,
return render_template('auth/reset_password.html.j2',
form=form,
title='Password Reset')