1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Added changing active semester from admin view

This commit is contained in:
Mo 2021-05-17 16:55:04 +02:00
parent 6d32b1840d
commit 868c1e2d4d
4 changed files with 31 additions and 9 deletions

View file

@ -1,11 +1,24 @@
from flask_admin import AdminIndexView from flask_admin import AdminIndexView
from flask_admin.contrib.sqla import ModelView
from flask_security import current_user 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): class SecureAdminIndexView(AdminIndexView):
def is_accessible(self): 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): class SecureModelView(ModelView):
@ -13,4 +26,8 @@ class SecureModelView(ModelView):
can_set_page_size = True can_set_page_size = True
def is_accessible(self): 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)

View file

@ -1,5 +1,4 @@
from flask import url_for, render_template, flash, request from flask import url_for, render_template, flash, request
from advlabdb.customClasses import SecureModelView
from flask_admin.menu import MenuLink from flask_admin.menu import MenuLink
from flask_admin.contrib.sqla.filters import BaseSQLAFilter from flask_admin.contrib.sqla.filters import BaseSQLAFilter
from flask_security import hash_password 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 import admin, app, user_datastore, db
from advlabdb.models import * from advlabdb.models import *
from advlabdb.utils import randomPassword, userActiveSemester, partFromLabelInUserActiveSemester from advlabdb.utils import randomPassword, userActiveSemester, partFromLabelInUserActiveSemester
from advlabdb.customClasses import SecureModelView
partsLabels = ["A/1", "A/2", "A/m", "B/1", "B/2"] 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)) admin.add_view(RoleModelView(Role, db.session))
with app.app_context(): 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"))

View file

@ -164,4 +164,5 @@ def deactivate_users():
def set_semester(): def set_semester():
current_user.active_semester_id = int(request.args.get("semester_id")) current_user.active_semester_id = int(request.args.get("semester_id"))
db.session.commit() db.session.commit()
return redirect(url_for("index")) flash("Active semester changed!", "warning")
return redirect(request.referrer)

View file

@ -72,7 +72,7 @@ def titleToTemplate(page):
return page.lower().replace(" ", "_") return page.lower().replace(" ", "_")
def userActiveSemester(): def userActiveSemester(flashWarning=False):
lastSemesterId = Semester.query.all()[-1].id lastSemesterId = Semester.query.all()[-1].id
if current_user.active_semester_id is None: if current_user.active_semester_id is None:
current_user.active_semester_id = lastSemesterId current_user.active_semester_id = lastSemesterId
@ -80,6 +80,7 @@ def userActiveSemester():
elif current_user.active_semester_id != lastSemesterId: elif current_user.active_semester_id != lastSemesterId:
activeSemester = Semester.query.get(current_user.active_semester_id) activeSemester = Semester.query.get(current_user.active_semester_id)
if activeSemester: if activeSemester:
if flashWarning:
flash(f"You are in the old semester {activeSemester.label}!", "warning") flash(f"You are in the old semester {activeSemester.label}!", "warning")
else: else:
current_user.active_semester_id = lastSemesterId current_user.active_semester_id = lastSemesterId