From f840bd4b20e76084a43244d8809187e48e12113c Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sat, 26 Jun 2021 01:59:21 +0200 Subject: [PATCH] Transfer part experiments and assistants for new semesters --- advlabdb/modelViews.py | 51 ++++++++++++++++++++++++++++++------------ advlabdb/routes.py | 4 ++-- advlabdb/utils.py | 2 +- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/advlabdb/modelViews.py b/advlabdb/modelViews.py index 0a26024..cfbe271 100644 --- a/advlabdb/modelViews.py +++ b/advlabdb/modelViews.py @@ -3,7 +3,7 @@ from flask_admin.contrib.sqla.filters import BaseSQLAFilter from flask_admin.menu import MenuLink from flask_security import current_user, hash_password from sqlalchemy import func -from wtforms import BooleanField, SelectField, TextField +from wtforms import BooleanField, SelectField, TextField, RadioField from wtforms.validators import DataRequired, Email from advlabdb import admin, app, db, user_datastore @@ -27,7 +27,7 @@ from advlabdb.models import ( from advlabdb.utils import ( partFromLabelInUserActiveSemester, randomPassword, - setActiveSemester, + setUserActiveSemester, userActiveSemester, ) @@ -100,15 +100,21 @@ class SemesterModelView(SecureModelView): can_edit = False column_list = ["label", "parts"] - form_columns = ["semester_label", "year", "create_parts"] + form_columns = ["semester_label", "year", "create_parts", "transfer_assistants"] semesterLabels = ["WS", "SS"] form_extra_fields = { - "semester_label": SelectField( + "semester_label": RadioField( "Semester", choices=list(zip(semesterLabels, semesterLabels)), validators=[DataRequired()] ), "year": TextField("Year", validators=[DataRequired()]), - "create_parts": BooleanField("Create parts:" + ", ".join(getConfig("partLabels")) + ".", default=True), + "create_parts": BooleanField( + "Create parts:" + + ", ".join(getConfig("partLabels")) + + " and transfer part experiments from your current active semester:", + default=True, + ), + "transfer_assistants": BooleanField("Transfer Assistants from your current active semester:", default=False), } def create_model(self, form): @@ -125,18 +131,35 @@ class SemesterModelView(SecureModelView): return model def after_model_change(self, form, model, is_created): + admin.add_link( + MenuLink( + name=model.label, + url=url_for("set_semester") + "?semester_id=" + str(model.id), + category="Active semester", + ) + ) + + oldSemesterParts = userActiveSemester().parts + + setUserActiveSemester(model.id) + if form.create_parts.data: model.createParts() - 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) + try: + for part in oldSemesterParts: + for partExperiment in part.part_experiments: + newPartExperiment = PartExperiment( + experiment=partExperiment.experiment, part=partFromLabelInUserActiveSemester(part.label) + ) + if form.transfer_assistants.data: + newPartExperiment.assistants = partExperiment.assistants + self.session.add(newPartExperiment) + self.session.commit() + except Exception as ex: + flash(ex, "error") + + self.session.rollback() class PartModelView(SecureModelView): diff --git a/advlabdb/routes.py b/advlabdb/routes.py index 3792669..a23b695 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -3,7 +3,7 @@ from flask_security import auth_required, current_user from advlabdb import app from advlabdb.models import Semester -from advlabdb.utils import setActiveSemester, titleToTemplate, userActiveSemester +from advlabdb.utils import setUserActiveSemester, titleToTemplate, userActiveSemester @app.context_processor @@ -73,7 +73,7 @@ def index(): @app.route("/set_semester") @auth_required() def set_semester(): - setActiveSemester(request.args.get("semester_id")) + setUserActiveSemester(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 6acdb12..2a78e4d 100644 --- a/advlabdb/utils.py +++ b/advlabdb/utils.py @@ -101,7 +101,7 @@ def partFromLabelInUserActiveSemester(partLabel): return part -def setActiveSemester(semesterId): +def setUserActiveSemester(semesterId): try: current_user.active_semester_id = Semester.query.get(int(semesterId)).id except Exception as err: