From 868c1e2d4dabf7ed090ea487a28642169943dc8a Mon Sep 17 00:00:00 2001 From: Mo8it Date: Mon, 17 May 2021 16:55:04 +0200 Subject: [PATCH] Added changing active semester from admin view --- advlabdb/customClasses.py | 23 ++++++++++++++++++++--- advlabdb/modelViews.py | 9 ++++++--- advlabdb/routes.py | 3 ++- advlabdb/utils.py | 5 +++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/advlabdb/customClasses.py b/advlabdb/customClasses.py index a040325..6f3bb01 100644 --- a/advlabdb/customClasses.py +++ b/advlabdb/customClasses.py @@ -1,11 +1,24 @@ from flask_admin import AdminIndexView -from flask_admin.contrib.sqla import ModelView from flask_security import current_user +from flask import flash +from flask_admin.contrib.sqla import ModelView +from advlabdb.utils import userActiveSemester + + +def adminViewIsAccessible(): + return current_user.has_role("admin") + +def handleAdminView(): + flash("User active semester: " + userActiveSemester(flashWarning=True).label) class SecureAdminIndexView(AdminIndexView): def is_accessible(self): - return current_user.has_role("admin") + return adminViewIsAccessible() + + def _handle_view(self, *args, **kwargs): + handleAdminView() + return super()._handle_view(*args, **kwargs) class SecureModelView(ModelView): @@ -13,4 +26,8 @@ class SecureModelView(ModelView): can_set_page_size = True def is_accessible(self): - return current_user.has_role("admin") + return adminViewIsAccessible() + + def _handle_view(self, *args, **kwargs): + handleAdminView() + return super()._handle_view(*args, **kwargs) diff --git a/advlabdb/modelViews.py b/advlabdb/modelViews.py index c1917f7..fa4e30e 100644 --- a/advlabdb/modelViews.py +++ b/advlabdb/modelViews.py @@ -1,5 +1,4 @@ from flask import url_for, render_template, flash, request -from advlabdb.customClasses import SecureModelView from flask_admin.menu import MenuLink from flask_admin.contrib.sqla.filters import BaseSQLAFilter from flask_security import hash_password @@ -9,7 +8,7 @@ from wtforms.validators import DataRequired, Email from advlabdb import admin, app, user_datastore, db from advlabdb.models import * from advlabdb.utils import randomPassword, userActiveSemester, partFromLabelInUserActiveSemester - +from advlabdb.customClasses import SecureModelView partsLabels = ["A/1", "A/2", "A/m", "B/1", "B/2"] @@ -168,4 +167,8 @@ admin.add_view(UserModelView(User, db.session)) admin.add_view(RoleModelView(Role, db.session)) with app.app_context(): - admin.add_link(MenuLink(name="Home", url=url_for("index"), category="Links")) + semesters = Semester.query.all() + for semester in semesters: + admin.add_link(MenuLink(name=semester.label, + url=url_for("set_semester") + "?semester_id=" + str(semester.id), + category="Active semester")) diff --git a/advlabdb/routes.py b/advlabdb/routes.py index 3103027..4a5e935 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -164,4 +164,5 @@ def deactivate_users(): def set_semester(): current_user.active_semester_id = int(request.args.get("semester_id")) db.session.commit() - return redirect(url_for("index")) + flash("Active semester changed!", "warning") + return redirect(request.referrer) diff --git a/advlabdb/utils.py b/advlabdb/utils.py index 01d0f87..b3a348a 100644 --- a/advlabdb/utils.py +++ b/advlabdb/utils.py @@ -72,7 +72,7 @@ def titleToTemplate(page): return page.lower().replace(" ", "_") -def userActiveSemester(): +def userActiveSemester(flashWarning=False): lastSemesterId = Semester.query.all()[-1].id if current_user.active_semester_id is None: current_user.active_semester_id = lastSemesterId @@ -80,7 +80,8 @@ def userActiveSemester(): elif current_user.active_semester_id != lastSemesterId: activeSemester = Semester.query.get(current_user.active_semester_id) if activeSemester: - flash(f"You are in the old semester {activeSemester.label}!", "warning") + if flashWarning: + flash(f"You are in the old semester {activeSemester.label}!", "warning") else: current_user.active_semester_id = lastSemesterId db.session.commit()