from flask import render_template, url_for, request, redirect, flash, abort from wtforms import Form, BooleanField, StringField, PasswordField, validators import flask_login from forms.login import LoginForm, RegisterForm, BillForm, User, register_account from objects import glob # Global sharing of python objects in a manageable way from flask_babel import gettext login_manager = flask_login.LoginManager() login_manager.init_app(glob.app) login_manager.login_view = "login" logged_in_users = [] @glob.app.route("/") @glob.app.route("/home") @glob.app.route("/dashboard") @flask_login.login_required def dashboard(): return render_template("pages/dashboard.html") @glob.app.route("/bills", methods = ["GET", "POST"]) @flask_login.login_required def bills(): form = BillForm(request.form) conn = glob.make_sql_connection() cur = conn.cursor() 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")) 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() conn.close() return render_template("pages/bills.html", data=data, form=form) @glob.app.route("/warranties") @flask_login.login_required def warranties(): return "Unimplemented" @glob.app.route("/receipts") @flask_login.login_required def receipts(): return "Unimplemented" @glob.app.route("/login", methods = ["GET", "POST"]) def login(): if flask_login.current_user.is_authenticated: flash(gettext("Already logged in"), "info") return redirect(url_for("dashboard")) 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 }) @glob.app.route("/logout") @flask_login.login_required def logout(): flask_login.logout_user() flash(gettext("Logged out"), "success") return redirect(url_for("login")) @glob.app.errorhandler(401) def unauthorized_handler_err(): flash(gettext("Login is required"), "danger") unauthorized_handler() @login_manager.user_loader def load_user(uuid): uuid = int(uuid) lst = [x for x in logged_in_users if x.id == uuid] return lst[0] if len(lst) > 0 else None @login_manager.unauthorized_handler def unauthorized_handler(): return redirect(url_for("login"))