From 82ee852469e14073a59a6e46ac328fd644bb31ca Mon Sep 17 00:00:00 2001 From: Mo8it Date: Tue, 18 May 2021 14:54:30 +0200 Subject: [PATCH] Added setting new semester as active semester --- advlabdb/modelViews.py | 8 +++++++- advlabdb/routes.py | 10 +++++----- advlabdb/utils.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/advlabdb/modelViews.py b/advlabdb/modelViews.py index a13dc6d..78d764e 100644 --- a/advlabdb/modelViews.py +++ b/advlabdb/modelViews.py @@ -6,7 +6,7 @@ from wtforms import BooleanField, SelectField, TextField from wtforms.validators import DataRequired, Email from advlabdb import admin, app, user_datastore, db -from advlabdb.utils import randomPassword, userActiveSemester, partFromLabelInUserActiveSemester +from advlabdb.utils import randomPassword, userActiveSemester, partFromLabelInUserActiveSemester, setActiveSemester from advlabdb.customClasses import SecureModelView from advlabdb.models import User, Role, Semester, Part, Student, PartStudent, Group, GroupExperiment, Experiment, Assistant, Appointment, PartExperiment, ExperimentMark @@ -64,6 +64,12 @@ class SemesterModelView(SecureModelView): db.session.add(Part(label=partLabel, semester=model)) db.session.commit() + if is_created: + admin.add_link(MenuLink(name=model.label, + url=url_for("set_semester") + "?semester_id=" + str(model.id), + category="Active semester")) + setActiveSemester(model.id) + class PartModelView(SecureModelView): can_view_details = True diff --git a/advlabdb/routes.py b/advlabdb/routes.py index 965ae40..8593b54 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -2,7 +2,7 @@ from advlabdb import app, db from flask import render_template, request, url_for, flash, redirect from flask_security import auth_required, current_user -from advlabdb.utils import titleToTemplate, userActiveSemester +from advlabdb.utils import titleToTemplate, userActiveSemester, setActiveSemester from advlabdb.models import Semester @@ -166,7 +166,7 @@ def deactivate_users(): @app.route("/set_semester", methods=["GET"]) @auth_required() def set_semester(): - current_user.active_semester_id = int(request.args.get("semester_id")) - db.session.commit() - flash("Active semester changed!", "warning") - return redirect(request.referrer) + setActiveSemester(request.args.get("semester_id")) + ref = request.referrer + red = ref if ref else url_for("index") + return redirect(red) diff --git a/advlabdb/utils.py b/advlabdb/utils.py index e4fa335..5af0f62 100644 --- a/advlabdb/utils.py +++ b/advlabdb/utils.py @@ -95,3 +95,15 @@ def partFromLabelInUserActiveSemester(partLabel): if part.label == partLabel: return part return False + + +def setActiveSemester(semesterId): + try: + current_user.active_semester_id = Semester.query.get(int(semesterId)).id + print(Semester.query.get(int(semesterId))) + except Exception as err: + flash(str(err)) + return False + db.session.commit() + flash(f"Active semester changed to {Semester.query.get(semesterId).label}!", "warning") + return True