husstanden/routes.py

148 lines
4.1 KiB
Python
Raw Normal View History

2019-04-26 03:28:27 +02:00
from flask import render_template, url_for, request, redirect, flash, abort
from wtforms import Form, BooleanField, StringField, PasswordField, validators
import flask_login
2019-05-27 16:54:05 +02:00
from forms.login import LoginForm, RegisterForm, BillForm, WarrantyForm, User, register_account
2019-04-26 03:28:27 +02:00
2019-02-12 16:59:43 +01:00
from objects import glob # Global sharing of python objects in a manageable way
2019-05-12 16:04:25 +02:00
from flask_babel import gettext
2019-04-26 03:28:27 +02:00
login_manager = flask_login.LoginManager()
login_manager.init_app(glob.app)
login_manager.login_view = "login"
logged_in_users = []
2019-02-12 17:09:43 +01:00
@glob.app.route("/")
@glob.app.route("/home")
2019-04-25 22:17:26 +02:00
@glob.app.route("/dashboard")
2019-04-26 03:28:27 +02:00
@flask_login.login_required
def dashboard():
2019-04-25 22:17:26 +02:00
return render_template("pages/dashboard.html")
2019-02-25 16:29:35 +01:00
2019-04-26 07:50:49 +02:00
@glob.app.route("/bills", methods = ["GET", "POST"])
2019-04-26 06:22:43 +02:00
@flask_login.login_required
def bills():
2019-04-26 07:50:49 +02:00
form = BillForm(request.form)
2019-04-26 06:52:27 +02:00
conn = glob.make_sql_connection()
2019-04-26 06:22:43 +02:00
cur = conn.cursor()
2019-04-26 07:50:49 +02:00
if request.method == "POST" and form.validate():
cur.execute("""
INSERT
INTO Regninger
VALUES (NULL, %s, %s, %s, %s, %s, 0, 1, %s)
""", (form.payment_to.data, form.description.data, form.kid.data, form.sum.data, form.date_due.data, flask_login.current_user.id))
conn.commit()
return redirect(url_for("bills"))
2019-04-26 06:22:43 +02:00
cur.execute("""
SELECT Betaletil, Regningfor, Regningsum, KID, Betalingsfrist, Betalt
FROM Regninger
WHERE BrukerID = %s
""", (flask_login.current_user.id,))
data = []
for row in cur:
data.append(row)
cur.close()
2019-04-26 06:52:27 +02:00
conn.close()
2019-04-26 07:50:49 +02:00
return render_template("pages/bills.html", data=data, form=form)
2019-04-26 06:22:43 +02:00
2019-05-27 16:54:05 +02:00
@glob.app.route("/warranties", methods = ["GET", "POST"])
2019-04-26 06:22:43 +02:00
@flask_login.login_required
def warranties():
2019-05-27 16:54:05 +02:00
form = WarrantyForm(request.form)
conn = glob.make_sql_connection()
cur = conn.cursor()
if request.method == "POST" and form.validate():
cur.execute("""
INSERT
INTO Garanti
VALUES (NULL, %s, %s, %s, %s, 1, %s)
""", (form.item.data, form.date_from.data, form.date_to.data, form.sum.data, flask_login.current_user.id))
conn.commit()
2019-05-27 16:56:56 +02:00
return redirect(url_for("warranties"))
2019-05-27 16:54:05 +02:00
cur.execute("""
SELECT Vare, Kjøpsdato, Garantitil, Pris
FROM Garanti
WHERE BrukerID = %s
""", (flask_login.current_user.id,))
data = []
for row in cur:
data.append(row)
cur.close()
conn.close()
return render_template("pages/warranties.html", data=data, form=form)
2019-04-26 06:22:43 +02:00
2019-05-27 16:54:05 +02:00
@glob.app.route("/receipts", methods = ["GET", "POST"])
2019-04-26 06:22:43 +02:00
@flask_login.login_required
def receipts():
2019-05-27 16:59:09 +02:00
return render_template("pages/receipts.html")
2019-04-26 06:22:43 +02:00
2019-03-02 23:41:16 +01:00
@glob.app.route("/login", methods = ["GET", "POST"])
2019-04-26 03:28:27 +02:00
def login():
if flask_login.current_user.is_authenticated:
2019-05-12 16:04:25 +02:00
flash(gettext("Already logged in"), "info")
2019-04-26 03:28:27 +02:00
return redirect(url_for("dashboard"))
2019-05-12 19:31:26 +02:00
form_login = LoginForm(request.form)
form_register = RegisterForm(request.form)
if request.method == "POST":
if form_register.validate():
try:
register_account(form_register.email.data, form_register.password.data, form_register.firstname.data, form_register.surname.data)
flash(gettext("User registered"), "success")
except Exception as e:
flash(gettext(str(e)), "danger")
return redirect(url_for("login"))
elif form_login.validate():
try:
user = User((form_login.email.data, form_login.password.data))
flask_login.login_user(user)
logged_in_users.append(user)
flash(gettext("Logged in"), "success")
except Exception as e:
flash(gettext(str(e)), "danger")
return redirect(url_for("login"))
return redirect(url_for("dashboard")) # Valid login > Redirect to dashboard as user is logged in
return render_template("login.html", form = {
"login": form_login,
"register": form_register
})
2019-04-26 03:28:27 +02:00
@glob.app.route("/logout")
@flask_login.login_required
def logout():
flask_login.logout_user()
2019-05-12 16:04:25 +02:00
flash(gettext("Logged out"), "success")
2019-04-26 03:28:27 +02:00
return redirect(url_for("login"))
@glob.app.errorhandler(401)
def unauthorized_handler_err():
2019-05-12 16:04:25 +02:00
flash(gettext("Login is required"), "danger")
2019-04-26 03:28:27 +02:00
unauthorized_handler()
@login_manager.user_loader
def load_user(uuid):
2019-04-26 04:52:54 +02:00
uuid = int(uuid)
lst = [x for x in logged_in_users if x.id == uuid]
return lst[0] if len(lst) > 0 else None
2019-04-26 03:28:27 +02:00
@login_manager.unauthorized_handler
def unauthorized_handler():
return redirect(url_for("login"))