1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-12 21:40:41 +00:00
AdvLabDB/advlabdb/routes.py

62 lines
1.7 KiB
Python
Raw Normal View History

2022-09-19 17:14:45 +00:00
from flask import Blueprint, redirect, url_for
2022-08-15 20:22:36 +00:00
from flask_login import current_user
from flask_security.decorators import auth_required
2021-04-03 00:11:26 +00:00
2022-11-19 18:52:29 +00:00
from . import security, settings
2022-06-01 20:53:23 +00:00
from .model_dependent_funs import active_semester_str
2021-03-18 13:53:55 +00:00
2022-08-09 12:46:48 +00:00
bp = Blueprint("main", __name__, root_path="/", template_folder="templates")
2021-03-31 23:04:21 +00:00
2022-08-09 12:46:48 +00:00
@bp.app_context_processor
2021-03-20 14:41:22 +00:00
def util_processor():
2022-10-09 01:02:35 +00:00
repo_url = settings["repo_url"]
2023-11-01 22:24:47 +00:00
footer = f"AdvLabDB - <a href={repo_url} target='_blank' rel='noopener noreferrer'>Source code</a>"
2022-08-09 12:46:48 +00:00
2023-11-02 18:01:07 +00:00
return {"active_semester_str": active_semester_str, "current_user": current_user, "footer": footer}
2021-03-20 14:41:22 +00:00
2021-03-31 23:04:21 +00:00
2022-11-10 12:20:18 +00:00
@security.login_context_processor
def login_context_processor():
2023-11-02 18:01:07 +00:00
return {"message": settings.get("login_view_message")}
2022-11-10 12:20:18 +00:00
2022-08-09 12:46:48 +00:00
@bp.route("/")
2021-03-18 13:53:55 +00:00
def index():
2021-05-17 20:15:50 +00:00
if current_user.has_role("admin"):
2022-06-28 14:57:55 +00:00
endpoint_base = "admin"
2021-05-17 20:15:50 +00:00
elif current_user.has_role("assistant"):
2022-06-28 14:57:55 +00:00
endpoint_base = "assistant"
2021-05-17 20:15:50 +00:00
else:
2021-09-11 19:20:36 +00:00
return redirect(url_for("security.login"))
2022-09-17 13:55:09 +00:00
url = url_for(endpoint_base + ".index")
return redirect(url)
@bp.route("/post-login")
@auth_required()
def post_login():
current_active_semester = current_user.active_semester
if current_active_semester.done:
2022-09-19 13:19:25 +00:00
current_user.set_last_semester_as_active()
2022-09-17 13:55:09 +00:00
2023-11-02 18:38:09 +00:00
endpoint_base = "admin" if current_user.has_role("admin") else "assistant"
2022-09-17 13:55:09 +00:00
2023-11-02 18:38:09 +00:00
url = url_for(endpoint_base + "_docs.index") if current_user.login_count == 1 else url_for(endpoint_base + ".index")
2022-06-28 14:57:55 +00:00
return redirect(url)
2022-09-21 14:52:04 +00:00
@bp.route("/user-settings")
@auth_required()
def user_settings():
2023-11-02 18:38:09 +00:00
role = "admin" if current_user.has_role("admin") else "assistant"
2022-09-21 14:52:04 +00:00
url = url_for("main.index") + role + "/user/edit/?id=" + str(current_user.id)
return redirect(url)