diff --git a/PruebaFoto/app.py b/PruebaFoto/app.py new file mode 100644 index 0000000..17dc82e --- /dev/null +++ b/PruebaFoto/app.py @@ -0,0 +1,107 @@ +import os +from flask import Flask, render_template, request, redirect, url_for, flash +from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user +from flask_sqlalchemy import SQLAlchemy +from werkzeug.security import generate_password_hash, check_password_hash +from werkzeug.utils import secure_filename + +app = Flask(__name__) +app.secret_key = 'your_secret_key' # Cambia esto por una clave secreta segura + +# Configuración de la base de datos +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +# Configuración para subir archivos +app.config['UPLOAD_FOLDER'] = 'uploads' +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB + +# Asegúrate de que el directorio de carga existe +os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) + +# Inicializa la base de datos +db = SQLAlchemy(app) + +# Inicializa Flask-Login +login_manager = LoginManager() +login_manager.init_app(app) +login_manager.login_view = 'login' + +# Modelo de usuario +class User(UserMixin, db.Model): + id = db.Column(db.Integer, primary_key=True) + email = db.Column(db.String(150), unique=True, nullable=False) + password = db.Column(db.String(150), nullable=False) + photo = db.Column(db.String(150), nullable=True) + +@login_manager.user_loader +def load_user(user_id): + return User.query.get(int(user_id)) + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + email = request.form['email'] + password = request.form['password'] + user = User.query.filter_by(email=email).first() + if user and check_password_hash(user.password, password): + login_user(user) + flash('Logged in successfully.') + return redirect(url_for('profile')) + else: + flash('Invalid email or password.') + return render_template('login.html') + +@app.route('/logout') +@login_required +def logout(): + logout_user() + flash('Logged out successfully.') + return redirect(url_for('index')) + +@app.route('/profile') +@login_required +def profile(): + photo_url = url_for('static', filename='uploads/' + current_user.photo) if current_user.photo else None + return render_template('profile.html', email=current_user.email, photo_url=photo_url) + +@app.route('/register', methods=['GET', 'POST']) +def register(): + if request.method == 'POST': + email = request.form['email'] + password = request.form['password'] + confirm_password = request.form['confirm_password'] + photo = request.files['photo'] + + if password != confirm_password: + flash('Passwords do not match.') + return redirect(url_for('register')) + + hashed_password = generate_password_hash(password) + + if photo: + photo_filename = secure_filename(photo.filename) + photo.save(os.path.join(app.config['UPLOAD_FOLDER'], photo_filename)) + else: + photo_filename = None + + new_user = User(email=email, password=hashed_password, photo=photo_filename) + try: + db.session.add(new_user) + db.session.commit() + flash('Registration successful.') + return redirect(url_for('login')) + except: + flash('Email address already exists.') + return redirect(url_for('register')) + + return render_template('register.html') + +if __name__ == '__main__': + with app.app_context(): + db.create_all() + app.run(debug=True) diff --git a/PruebaFoto/instance/users.db b/PruebaFoto/instance/users.db new file mode 100644 index 0000000..bbacd32 Binary files /dev/null and b/PruebaFoto/instance/users.db differ diff --git a/PruebaFoto/static/uploads/daniel.jpg b/PruebaFoto/static/uploads/daniel.jpg new file mode 100644 index 0000000..d7775bd Binary files /dev/null and b/PruebaFoto/static/uploads/daniel.jpg differ diff --git a/PruebaFoto/static/uploads/theriver.jpg b/PruebaFoto/static/uploads/theriver.jpg new file mode 100644 index 0000000..3caa7cd Binary files /dev/null and b/PruebaFoto/static/uploads/theriver.jpg differ diff --git a/PruebaFoto/templates/index.html b/PruebaFoto/templates/index.html new file mode 100644 index 0000000..16613ca --- /dev/null +++ b/PruebaFoto/templates/index.html @@ -0,0 +1,18 @@ + + + + + Example App + + +

Welcome to the Example App

+ {% if current_user.is_authenticated %} +

Welcome, {{ current_user.email }}!

+ Profile + Logout + {% else %} + Login + Register + {% endif %} + + diff --git a/PruebaFoto/templates/login.html b/PruebaFoto/templates/login.html new file mode 100644 index 0000000..5e91082 --- /dev/null +++ b/PruebaFoto/templates/login.html @@ -0,0 +1,17 @@ + + + + + Login + + +

Login

+
+ + + + + +
+ + diff --git a/PruebaFoto/templates/profile.html b/PruebaFoto/templates/profile.html new file mode 100644 index 0000000..199b9cf --- /dev/null +++ b/PruebaFoto/templates/profile.html @@ -0,0 +1,15 @@ + + + + + Profile + + +

Profile

+

Email: {{ email }}

+ {% if photo_url %} + Profile Photo + {% endif %} + Logout + + diff --git a/PruebaFoto/templates/register.html b/PruebaFoto/templates/register.html new file mode 100644 index 0000000..74b1d02 --- /dev/null +++ b/PruebaFoto/templates/register.html @@ -0,0 +1,21 @@ + + + + + Register + + +

Register

+
+ + + + + + + + + +
+ + diff --git a/ReymotaPy/servicios/instance/reymotapy.db b/ReymotaPy/servicios/instance/reymotapy.db index 2608bf3..47f9b06 100644 Binary files a/ReymotaPy/servicios/instance/reymotapy.db and b/ReymotaPy/servicios/instance/reymotapy.db differ diff --git a/ReymotaPy/servicios/instance/uploads/nebraska.jpg b/ReymotaPy/servicios/instance/uploads/nebraska.jpg new file mode 100644 index 0000000..eebc1a5 Binary files /dev/null and b/ReymotaPy/servicios/instance/uploads/nebraska.jpg differ diff --git a/ReymotaPy/servicios/reymotapy/auth.py b/ReymotaPy/servicios/reymotapy/auth.py index 7304b12..2f4d498 100644 --- a/ReymotaPy/servicios/reymotapy/auth.py +++ b/ReymotaPy/servicios/reymotapy/auth.py @@ -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.utils import secure_filename from flask_login import login_user, logout_user, login_required @@ -16,14 +16,12 @@ def login(): @bp.route('/login', methods=['POST']) def login_post(): - print("Entrando en sesión (o no)") username = request.form.get('username') password = request.form.get('password') remember = True if request.form.get('remember') else False user = User.query.filter_by(username=username).first() - print("Usuario: "+user.username) # check if the user actually exists # 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): @@ -41,12 +39,11 @@ def signup(): @bp.route('/signup', methods=['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: 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 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')) - if photo: photo_filename = secure_filename(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: - 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) # 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') diff --git a/ReymotaPy/servicios/reymotapy/models.py b/ReymotaPy/servicios/reymotapy/models.py index c73438d..c6569b6 100644 --- a/ReymotaPy/servicios/reymotapy/models.py +++ b/ReymotaPy/servicios/reymotapy/models.py @@ -8,5 +8,5 @@ class User(UserMixin, db.Model): email = db.Column(db.String(150), unique=True, nullable=False) username = db.Column(db.String(150), unique=True, nullable=False) password = db.Column(db.String(150), nullable=False) - photo = db.Column(db.String(150), nullable=True) + photo = db.Column(db.String(150), nullable=False) diff --git a/ReymotaPy/servicios/reymotapy/templates/_cabecera.html b/ReymotaPy/servicios/reymotapy/templates/_cabecera.html index e75ad2a..e8ba45d 100644 --- a/ReymotaPy/servicios/reymotapy/templates/_cabecera.html +++ b/ReymotaPy/servicios/reymotapy/templates/_cabecera.html @@ -119,7 +119,7 @@