Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Create user and generate flash messages
src/__init__.py | 2 +- src/blueprints/auth.py | 8 ++++---- src/types/user.py | 19 ++++++++++++++++++-
diff --git a/src/__init__.py b/src/__init__.py index bea7a030950589d5fcc8cb20efe6a5a4faff47af..c20b176d5ac18ce2f41bb93e3bd1b137eadf7c55 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -5,7 +5,7 @@ def create_app(): app = Flask(__name__) # Database - app.config['SECRET'] = '123456asckjnsac' + app.config['SECRET_KEY'] = '123456asckjnsac' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/ciclos_dev' from .types import db diff --git a/src/blueprints/auth.py b/src/blueprints/auth.py index 5e39bf462bd4cead168db77f7234f15f2ee0cf0f..7ce6e6d21dcdad0ba2a8f0c33354bf68f8d012c3 100644 --- a/src/blueprints/auth.py +++ b/src/blueprints/auth.py @@ -1,4 +1,4 @@ -from flask import Blueprint, render_template, url_for, redirect +from flask import Blueprint, render_template, url_for, redirect, request, flash from ..types.user import User from werkzeug.security import generate_password_hash @@ -20,7 +20,7 @@ email = request.form.get('email') user = User.query.filter_by(email = email).first() if user: - # flash('You already have an account, perhaps log in?') + flash('You already have an account, perhaps log in?') return redirect(url_for('auth.login')) else: username = request.form.get('username') @@ -29,15 +29,15 @@ first_name = request.form.get('first_name') last_name = request.form.get('last_name') new_user = User( - username, email, generate_password_hash(password, method = 'sha256'), + username, first_name, last_name ) new_user.create() - # flash('Account created successfully') + flash('Account created successfully') return redirect(url_for('auth.login')) diff --git a/src/types/user.py b/src/types/user.py index 67b6198629dd331d79b169de4da8a89fc34dba2d..2f41189b3f11125987dd77139ada034f0b4b69ea 100644 --- a/src/types/user.py +++ b/src/types/user.py @@ -1,10 +1,27 @@ from . import db +from datetime import datetime as dt class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(80), unique = True, nullable = False) - email = db.Column(db.String(120), unique = True, nullable = False) + email = db.Column(db.String(220), unique = True, nullable = False) + password = db.Column(db.String(96), nullable = False) + first_name = db.Column(db.String(250), nullable = False) + last_name = db.Column(db.String(250)) + created_at = db.Column(db.DateTime, default = dt.utcnow) + updated_at = db.Column(db.DateTime, default = dt.utcnow) + + def __init__(self, email, password, username, first_name, last_name): + self.email = email + self.password = password + self.username = username + self.first_name = first_name + self.last_name = last_name + + def create(self): + db.session.add(self) + db.session.commit() def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns}