mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-01-23 00:00:35 +00:00
Codestyle for auth package.
This commit is contained in:
parent
5749c94bca
commit
4858f36d76
@ -1,7 +1,7 @@
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import (StringField, PasswordField, BooleanField, SubmitField,
|
from wtforms import (BooleanField, PasswordField, StringField, SubmitField,
|
||||||
ValidationError)
|
ValidationError)
|
||||||
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
|
from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
|
||||||
from ..models import User
|
from ..models import User
|
||||||
|
|
||||||
|
|
||||||
@ -12,6 +12,25 @@ class LoginForm(FlaskForm):
|
|||||||
submit = SubmitField('Log In')
|
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):
|
class RegistrationForm(FlaskForm):
|
||||||
email = StringField('Email', validators=[DataRequired(), Email()])
|
email = StringField('Email', validators=[DataRequired(), Email()])
|
||||||
username = StringField(
|
username = StringField(
|
||||||
@ -38,26 +57,3 @@ class RegistrationForm(FlaskForm):
|
|||||||
def validate_username(self, field):
|
def validate_username(self, field):
|
||||||
if User.query.filter_by(username=field.data).first():
|
if User.query.filter_by(username=field.data).first():
|
||||||
raise ValidationError('Username already in use.')
|
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')
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
from flask import flash, redirect, render_template, request, url_for
|
from flask import flash, redirect, render_template, request, url_for
|
||||||
from flask_login import current_user, login_required, login_user, logout_user
|
from flask_login import current_user, login_required, login_user, logout_user
|
||||||
from . import auth
|
from . import auth
|
||||||
from .. import db
|
|
||||||
from .forms import (LoginForm, PasswordResetForm, PasswordResetRequestForm,
|
from .forms import (LoginForm, PasswordResetForm, PasswordResetRequestForm,
|
||||||
RegistrationForm)
|
RegistrationForm)
|
||||||
|
from .. import db
|
||||||
from ..email import send_email
|
from ..email import send_email
|
||||||
from ..models import User
|
from ..models import User
|
||||||
|
|
||||||
@ -42,16 +42,21 @@ def register():
|
|||||||
form = RegistrationForm()
|
form = RegistrationForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
user = User(email=form.email.data.lower(),
|
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.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
token = user.generate_confirmation_token()
|
token = user.generate_confirmation_token()
|
||||||
send_email(user.email, 'Confirm Your Account',
|
send_email(user.email,
|
||||||
'auth/email/confirm', user=user, token=token)
|
'Confirm Your Account',
|
||||||
|
'auth/email/confirm',
|
||||||
|
token=token,
|
||||||
|
user=user)
|
||||||
flash('A confirmation email has been sent to you by email.')
|
flash('A confirmation email has been sent to you by email.')
|
||||||
return redirect(url_for('auth.login'))
|
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>')
|
@auth.route('/confirm/<token>')
|
||||||
@ -73,10 +78,10 @@ def before_request():
|
|||||||
Checks if a user is unconfirmed when visiting specific sites. Redirects to
|
Checks if a user is unconfirmed when visiting specific sites. Redirects to
|
||||||
unconfirmed view if user is unconfirmed.
|
unconfirmed view if user is unconfirmed.
|
||||||
"""
|
"""
|
||||||
if current_user.is_authenticated \
|
if (current_user.is_authenticated
|
||||||
and not current_user.confirmed \
|
and not current_user.confirmed
|
||||||
and request.blueprint != 'auth' \
|
and request.blueprint != 'auth'
|
||||||
and request.endpoint != 'static':
|
and request.endpoint != 'static'):
|
||||||
return redirect(url_for('auth.unconfirmed'))
|
return redirect(url_for('auth.unconfirmed'))
|
||||||
|
|
||||||
|
|
||||||
@ -91,8 +96,11 @@ def unconfirmed():
|
|||||||
@login_required
|
@login_required
|
||||||
def resend_confirmation():
|
def resend_confirmation():
|
||||||
token = current_user.generate_confirmation_token()
|
token = current_user.generate_confirmation_token()
|
||||||
send_email(current_user.email, 'Confirm Your Account', 'auth/email/confirm',
|
send_email(current_user.email,
|
||||||
user=current_user, token=token)
|
'Confirm Your Account',
|
||||||
|
'auth/email/confirm',
|
||||||
|
token=token,
|
||||||
|
user=current_user)
|
||||||
flash('A new confirmation email has benn sent to you by email.')
|
flash('A new confirmation email has benn sent to you by email.')
|
||||||
return redirect(url_for('main.dashboard'))
|
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()
|
user = User.query.filter_by(email=form.email.data.lower()).first()
|
||||||
if user:
|
if user:
|
||||||
token = user.generate_reset_token()
|
token = user.generate_reset_token()
|
||||||
send_email(user.email, 'Reset Your Password',
|
send_email(user.email,
|
||||||
|
'Reset Your Password',
|
||||||
'auth/email/reset_password',
|
'auth/email/reset_password',
|
||||||
user=user, token=token)
|
token=token,
|
||||||
|
user=user)
|
||||||
flash('An email with instructions to reset your password has been '
|
flash('An email with instructions to reset your password has been '
|
||||||
'sent to you.')
|
'sent to you.')
|
||||||
return redirect(url_for('auth.login'))
|
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')
|
title='Password Reset')
|
||||||
|
|
||||||
|
|
||||||
@ -128,5 +139,6 @@ def password_reset(token):
|
|||||||
return redirect(url_for('auth.login'))
|
return redirect(url_for('auth.login'))
|
||||||
else:
|
else:
|
||||||
return redirect(url_for('main.index'))
|
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')
|
title='Password Reset')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user