-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (52 loc) · 2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from dotenv import load_dotenv
load_dotenv()
from flask import Flask, render_template, request, redirect, flash, session
from utils.db import db
from utils.hash import hash_pass
from sqlalchemy import select
# Models
from models.user import user_account
# Routes
from routes.admin import admin
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("DB_URI")
# TODO Change this to .env with another key.
app.config['SECRET_KEY'] = "4e041161ab1f2548591d829ecdeb58bd3921a59462c714e2ccddbe02b69216d4"
db.init_app(app)
app.register_blueprint(admin, url_prefix="/admin")
@app.route('/')
def index():
return redirect("/login")
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
#quit session
session.pop("user_name", None)
session.pop("user_id", None)
user = request.form['username']
password = request.form['password']
hashed_pass = hash_pass(password)
query = select(user_account).where(user_account.c.user_name == user) \
.where(user_account.c.user_password == hashed_pass)
print(query)
result = db.session.execute(query).first()
if result is None:
flash("Usuario y/o contrasena incorrectos", "error")
return render_template("login.html")
elif result.user_state != 0:
flash("Usuario desactivado", "error")
return render_template("login.html")
else:
session["user_name"] = result.user_name
session["user_id"] = result.user_id
return redirect("/admin")
else:
return render_template("login.html")
@app.route('/logout', methods=['GET'])
def logout():
session.pop("user_name", None)
session.pop("user_id", None)
return redirect("/")
return app