|
|
@ -1,4 +1,4 @@ |
|
|
from flask import Blueprint, render_template, redirect, url_for, request, flash |
|
|
|
|
|
|
|
|
from flask import Blueprint, render_template, redirect, url_for, request, flash, current_app |
|
|
from werkzeug.security import generate_password_hash, check_password_hash |
|
|
from werkzeug.security import generate_password_hash, check_password_hash |
|
|
from werkzeug.utils import secure_filename |
|
|
from werkzeug.utils import secure_filename |
|
|
from flask_login import login_user, logout_user, login_required |
|
|
from flask_login import login_user, logout_user, login_required |
|
|
@ -16,14 +16,12 @@ def login(): |
|
|
|
|
|
|
|
|
@bp.route('/login', methods=['POST']) |
|
|
@bp.route('/login', methods=['POST']) |
|
|
def login_post(): |
|
|
def login_post(): |
|
|
print("Entrando en sesión (o no)") |
|
|
|
|
|
username = request.form.get('username') |
|
|
username = request.form.get('username') |
|
|
password = request.form.get('password') |
|
|
password = request.form.get('password') |
|
|
remember = True if request.form.get('remember') else False |
|
|
remember = True if request.form.get('remember') else False |
|
|
|
|
|
|
|
|
user = User.query.filter_by(username=username).first() |
|
|
user = User.query.filter_by(username=username).first() |
|
|
|
|
|
|
|
|
print("Usuario: "+user.username) |
|
|
|
|
|
# check if the user actually exists |
|
|
# check if the user actually exists |
|
|
# take the user-supplied password, hash it, and compare it to the hashed password in the database |
|
|
# take the user-supplied password, hash it, and compare it to the hashed password in the database |
|
|
if not user or not check_password_hash(user.password, password): |
|
|
if not user or not check_password_hash(user.password, password): |
|
|
@ -41,12 +39,11 @@ def signup(): |
|
|
|
|
|
|
|
|
@bp.route('/signup', methods=['POST']) |
|
|
@bp.route('/signup', methods=['POST']) |
|
|
def signup_post(): |
|
|
def signup_post(): |
|
|
print("Creando usuario") |
|
|
|
|
|
email = request.form.get('email') |
|
|
|
|
|
username = request.form.get('username') |
|
|
|
|
|
password = request.form.get('password') |
|
|
|
|
|
confirm_password = request.form.get('confirm_password') |
|
|
|
|
|
photo = request.files['photo'] |
|
|
|
|
|
|
|
|
username = request.form['username'] |
|
|
|
|
|
email = request.form['email'] |
|
|
|
|
|
password = request.form['password'] |
|
|
|
|
|
confirm_password = request.form['confirm_password'] |
|
|
|
|
|
photo = request.files['fotoperfil'] |
|
|
|
|
|
|
|
|
if password != confirm_password: |
|
|
if password != confirm_password: |
|
|
flash('Passwords do not match.') |
|
|
flash('Passwords do not match.') |
|
|
@ -55,29 +52,29 @@ def signup_post(): |
|
|
user = User.query.filter_by(username=username).first() # if this returns a user, then the user already exists in database |
|
|
user = User.query.filter_by(username=username).first() # if this returns a user, then the user already exists in database |
|
|
|
|
|
|
|
|
if user: # if a user is found, we want to redirect back to signup page so user can try again |
|
|
if user: # if a user is found, we want to redirect back to signup page so user can try again |
|
|
flash('La dirección de correo ya existe') |
|
|
|
|
|
|
|
|
flash('Ese usuario ya existe') |
|
|
return redirect(url_for('auth.signup')) |
|
|
return redirect(url_for('auth.signup')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if photo: |
|
|
if photo: |
|
|
photo_filename = secure_filename(photo.filename) |
|
|
photo_filename = secure_filename(photo.filename) |
|
|
print("Foto: ", photo_filename) |
|
|
print("Foto: ", photo_filename) |
|
|
photo.save(os.path.join(bp.config['UPLOAD_FOLDER'], photo_filename)) |
|
|
|
|
|
|
|
|
photo.save(os.path.join(current_app.config['UPLOAD_FOLDER'], photo_filename)) |
|
|
else: |
|
|
else: |
|
|
photo_filename = None |
|
|
|
|
|
|
|
|
print("No hay foto") |
|
|
|
|
|
photo_filename = "" |
|
|
|
|
|
|
|
|
# create a new user with the form data. Hash the password so the plaintext version isn't saved. |
|
|
|
|
|
|
|
|
# create a new user with the form data. Hash the password so the plaintext version isn't saved. |
|
|
new_user = User(email=email, username=username, password=generate_password_hash(password, method='pbkdf2:sha256'), photo=photo_filename) |
|
|
new_user = User(email=email, username=username, password=generate_password_hash(password, method='pbkdf2:sha256'), photo=photo_filename) |
|
|
|
|
|
|
|
|
# add the new user to the database |
|
|
# add the new user to the database |
|
|
try: |
|
|
|
|
|
db.session.add(new_user) |
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
flash('Registration successful.') |
|
|
|
|
|
return redirect(url_for('auth.login')) |
|
|
|
|
|
except: |
|
|
|
|
|
flash('Email address already exists.') |
|
|
|
|
|
return redirect(url_for('auth.login')) |
|
|
|
|
|
|
|
|
# try: |
|
|
|
|
|
db.session.add(new_user) |
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
flash('Registration successful.') |
|
|
|
|
|
return redirect(url_for('auth.login')) |
|
|
|
|
|
# except: |
|
|
|
|
|
# flash('Email address already exists.') |
|
|
|
|
|
# return redirect(url_for('auth.signup')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/logout') |
|
|
@bp.route('/logout') |
|
|
|