2023-05-15 03:38:16 +02:00
|
|
|
import os
|
2023-05-15 04:28:05 +02:00
|
|
|
from hashlib import sha256
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from flask import render_template, send_from_directory, request, session, redirect
|
|
|
|
|
|
|
|
from .database import Session
|
|
|
|
|
2023-05-17 00:03:11 +02:00
|
|
|
|
2023-05-15 04:28:05 +02:00
|
|
|
def views(app, db):
|
|
|
|
def est_connecte():
|
|
|
|
return session.get("uuid") is not None and db.check_connection(session.get("uuid"))
|
|
|
|
|
|
|
|
def login_required(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return func(*args, **kwargs) if est_connecte() else redirect("/login")
|
|
|
|
return wrapper
|
2023-05-17 00:03:11 +02:00
|
|
|
|
2023-05-15 04:28:05 +02:00
|
|
|
@app.route("/")
|
|
|
|
@login_required
|
|
|
|
def index():
|
|
|
|
return render_template("index.html", s=Session(db, session["uuid"]))
|
|
|
|
|
|
|
|
@app.route("/login/", methods=["GET"])
|
|
|
|
def login_get():
|
|
|
|
return render_template("login.html")
|
|
|
|
|
|
|
|
@app.route("/login/", methods=["POST"])
|
|
|
|
def login_post():
|
|
|
|
session["uuid"] = db.create_session(
|
|
|
|
request.form["username"],
|
|
|
|
sha256(request.form["password"].encode()).hexdigest()
|
|
|
|
)
|
2023-05-15 05:15:34 +02:00
|
|
|
|
|
|
|
return redirect("/") if session["uuid"] else redirect("/login?error=1")
|
2023-05-15 04:28:05 +02:00
|
|
|
|
|
|
|
@app.route("/logout/")
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
db.destroy_session(session["uuid"])
|
|
|
|
session["uuid"] = None
|
2023-05-15 05:15:34 +02:00
|
|
|
return redirect("/login")
|
2023-05-15 04:28:05 +02:00
|
|
|
|
|
|
|
@app.route("/grades/")
|
|
|
|
@login_required
|
|
|
|
def grades():
|
2023-05-15 05:15:34 +02:00
|
|
|
s = Session(db, session["uuid"])
|
2023-05-17 00:03:11 +02:00
|
|
|
g = s.fetch_grades()
|
|
|
|
return render_template("grades.html", s=s, grades=g)
|
2023-05-15 04:28:05 +02:00
|
|
|
|
|
|
|
@app.route("/timetable/")
|
|
|
|
@login_required
|
|
|
|
def timetable():
|
|
|
|
return render_template("timetable.html", s=Session(db, session["uuid"]))
|
|
|
|
|
|
|
|
@app.route('/favicon.ico')
|
|
|
|
def favicon():
|
|
|
|
return send_from_directory(os.path.join(app.root_path, 'static'),
|
|
|
|
'favicon.ico', mimetype='image/vnd.microsoft.icon')
|