From e09c4abf8cf9fb5912f290ccd5b908813334b9de Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Thu, 18 May 2017 18:25:54 +0200 Subject: [PATCH] Update texts and Fixbugs --- app.py | 37 ++++++----- database.sqlite | Bin 20480 -> 20480 bytes envExample | 7 ++ forms.py | 89 ++++++++++++++++++++++---- models.py | 15 ++--- templates/emails/activate.html | 10 +-- templates/emails/activate.txt | 7 +- templates/emails/forgot_password.html | 10 +-- templates/emails/forgot_password.txt | 6 +- templates/helpers/_forms.html | 19 ++++++ templates/layouts/master.html | 5 +- templates/web/forgot_password.html | 24 ++----- templates/web/login.html | 24 ++----- templates/web/signup.html | 20 +----- templates/web/update_password.html | 24 ++----- 15 files changed, 166 insertions(+), 131 deletions(-) create mode 100644 envExample create mode 100644 templates/helpers/_forms.html diff --git a/app.py b/app.py index b509307..99ba32f 100644 --- a/app.py +++ b/app.py @@ -4,8 +4,8 @@ from functools import wraps from forms import LoginForm, SignupForm, EmailResetPasswordForm, ResetPasswordForm from models import db, User from flask_mail import Mail, Message -import uuid -import crypt +from uuid import uuid4 +from crypt import crypt, mksalt, METHOD_SHA512 # CONFIGURATIONS # Flask @@ -59,9 +59,13 @@ def signup(): ''' form = SignupForm() if form.validate_on_submit(): - if User.query.filter_by(email=form.email.data).all(): + if not User.query.filter_by(email=form.email.data).all(): my_user = User() form.populate_obj(my_user) + # Encrypt password + my_user.password = crypt( + form.password.data, mksalt(METHOD_SHA512) + ) db.session.add(my_user) # Prepare the account activation email msg = Message( @@ -69,15 +73,15 @@ def signup(): sender='no-repy@' + getenv('DOMAIN'), recipients=[my_user.email] ) - link = 'http://' + getenv('DOMAIN') + url_for('activate_account') + link = 'http://' + getenv('DOMAIN') + url_for('activate_account', token=my_user.token) msg.body = render_template( 'emails/activate.txt', username=my_user.username, - token=link + my_user.token + token=link ) msg.html = render_template( 'emails/activate.html', username=my_user.username, - token=link + my_user.token + token=link ) try: # Save new User @@ -128,13 +132,16 @@ def forgot_password(): my_user = User.query.filter_by(email=form.email.data).first() if my_user: # Generate new token - token = str(uuid.uuid4()).replace('-', '') + token = str(uuid4()).replace('-', '') # Update user token my_user.token = token db.session.add(my_user) db.session.commit() # Send email with token - link = 'http://' + getenv('DOMAIN') + url_for('update_password') + link = 'http://' + getenv('DOMAIN') + url_for( + 'update_password', + email=my_user.email, token=token + ) msg = Message( 'Recover password', sender='no-repy@' + getenv('DOMAIN'), @@ -142,12 +149,12 @@ def forgot_password(): ) msg.body = render_template( 'emails/forgot_password.txt', username=my_user.username, - token=link + my_user.token + token=link ) msg.html = render_template( 'emails/forgot_password.html', username=my_user.username, - token=link + my_user.token + token=link ) mail.send(msg) flash(''' @@ -172,8 +179,8 @@ def update_password(email, token): if my_user: if form.validate_on_submit(): # Encrypt password - my_user.password = crypt.crypt( - form.password.data, crypt.mksalt(crypt.METHOD_SHA512) + my_user.password = crypt( + form.password.data, mksalt(METHOD_SHA512) ) # Update password db.session.add(my_user) @@ -194,9 +201,9 @@ def login(): if form.validate_on_submit(): # Validate email and password email = form.email.data - password = crypt.crypt( - form.password.data, crypt.mksalt(crypt.METHOD_SHA512) - ) + password = crypt( + form.password.data, mksalt(METHOD_SHA512) + ) my_user = User.query.filter_by(email=email, password=password).first() if my_user: # Login de usuario diff --git a/database.sqlite b/database.sqlite index 1dd77fa0e960d1d691bb36d368202eda2e669dcd..836ff47eecb13a8567f6c1009b085b83e3c6f099 100644 GIT binary patch delta 156 zcmZozz}T>Wae_3Xz(g5mMuCk9OZ3?o`PVVWae_1>^F$eEM&^wPOZ1r-1U3r_+~=P-L7I(=0SMq+03Or}_y7O^ diff --git a/envExample b/envExample new file mode 100644 index 0000000..bbaf36d --- /dev/null +++ b/envExample @@ -0,0 +1,7 @@ +export DOMAIN='example.com' +export SECRET_KEY='my secret' +export DEBUG=True +export SQLALCHEMY_DATABASE_URI='sqlite:///database.sqlite' +export MAIL_SERVER='' +export MAIL_USERNAME='' +export MAIL_PASSWORD='' diff --git a/forms.py b/forms.py index b9102e5..f09107e 100644 --- a/forms.py +++ b/forms.py @@ -2,24 +2,91 @@ from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, BooleanField from wtforms.validators import DataRequired, Email, Length, EqualTo + class LoginForm(FlaskForm): - email = StringField('E-mail', validators=[DataRequired('Necesito un E-mail'), Email('Debe tener un formato válido')]) - password = PasswordField('Contraseña', validators=[DataRequired('No me has indicado una contraseña')]) + ''' + Form Login + ''' + email = StringField( + 'Email', + validators=[ + DataRequired(), + Email() + ] + ) + password = PasswordField( + 'Password', + validators=[ + DataRequired() + ] + ) class SignupForm(FlaskForm): - username = StringField('Nombre de usuario', validators=[DataRequired('Debes indicarnos un nombre de usuario'), Length(5, 30, 'Debe estar entre 5 y 30 carácteres')]) - email = StringField('E-mail', validators=[DataRequired('Necesito un E-mail'), Email('Debe tener un formato válido'), Length(1, 254, 'Es demasiado largo')]) - password = PasswordField('Contraseña', validators=[DataRequired('No me has indicado una contraseña'), EqualTo('password_confirm', 'No coinciden las contraseñas')]) - password_confirm = PasswordField('Repetir contraseña') - accept_tos = BooleanField('Aceptar condiciones', validators=[DataRequired('Necesito que aceptes mis condiciones. Aqui mando yo.')]) + ''' + Form signup + ''' + username = StringField( + 'Username', + validators=[ + DataRequired(), + Length(5, 30, ''' + You can not have less than 5 characters or more 30. + ''') + ] + ) + email = StringField( + 'Email', + validators=[ + DataRequired(), + Email(), + Length(1, 254, 'Too long.') + ] + ) + password = PasswordField( + 'Password', + validators=[ + DataRequired(), + EqualTo( + 'password_confirm', + 'Passwords are not the same.' + ) + ] + ) + password_confirm = PasswordField('Repeat password') + accept_tos = BooleanField( + 'I accept the terms and conditions.', + validators=[ + DataRequired('Please accept the terms and conditions.') + ] + ) class EmailResetPasswordForm(FlaskForm): - email = StringField('E-mail', validators=[DataRequired('Necesito un E-mail'), Email('Debe tener un formato válido')]) + ''' + Form send email reset password + ''' + email = StringField( + 'Email', + validators=[ + DataRequired(), + Email() + ] + ) class ResetPasswordForm(FlaskForm): - password = PasswordField('Contraseña', validators=[DataRequired('No me has indicado una contraseña'), EqualTo('password_confirm', 'No coinciden las contraseñas')]) - password_confirm = PasswordField('Repetir contraseña') - + ''' + Form update password + ''' + password = PasswordField( + 'Password', + validators=[ + DataRequired(), + EqualTo( + 'password_confirm', + 'Passwords are not the same.' + ) + ] + ) + password_confirm = PasswordField('Repeat password') diff --git a/models.py b/models.py index dc241ec..9bef0ba 100644 --- a/models.py +++ b/models.py @@ -3,8 +3,7 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_script import Manager from flask_migrate import Migrate, MigrateCommand -import crypt -import uuid +from uuid import uuid4 app = Flask(__name__) @@ -18,7 +17,9 @@ manager.add_command('db', MigrateCommand) class User(db.Model): - + ''' + Table user + ''' __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) @@ -28,15 +29,13 @@ class User(db.Model): is_active = db.Column(db.Boolean) token = db.Column(db.String(32), nullable=False, unique=False) - def __init__(self, username, email, password): - self.username = username - self.email = email - self.password = crypt.crypt(password, crypt.mksalt(crypt.METHOD_SHA512)) + def __init__(self): self.is_active = False - self.token = str(uuid.uuid4()).replace('-', '') + self.token = str(uuid4()).replace('-', '') def __repr__(self): return '' % self.username + if __name__ == '__main__': manager.run() diff --git a/templates/emails/activate.html b/templates/emails/activate.html index b5e980d..f23e87a 100644 --- a/templates/emails/activate.html +++ b/templates/emails/activate.html @@ -3,7 +3,7 @@ - Activar cuenta + Activate account