diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index bb8de37..5bc4cba 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -42,9 +42,9 @@ from .database_import import importFromFile from .exceptions import DataBaseException, ModelViewException from .model_dependent_funs import ( initActiveSemesterMenuLinks, + lastSemester, setUserActiveSemester, sortedSemestersStartingWithNewest, - userActiveSemester, ) from .models import ( MAX_MARK, @@ -70,7 +70,7 @@ from .models import ( def semesterExperimentQueryFactory(): - return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester()) + return SemesterExperiment.query.filter(SemesterExperiment.semester == current_user.active_semester) class SemesterRowFilter(FilterEqual): @@ -150,7 +150,7 @@ class UserView(SecureAdminModelView): query_factory=semesterQueryFactory, allow_blank=True, blank_text="-", - default=userActiveSemester, + default=lastSemester, description="Not fixed and users (including assistants) can change it.", ) @@ -289,13 +289,13 @@ class RoleView(SecureAdminModelView): class SemesterView(SecureAdminModelView): class CreateForm(Form): def defaultFormLabel(): - if userActiveSemester().label == "WS": + if current_user.active_semester.label == "WS": return "SS" else: return "WS" def defaultFormYear(): - activeSemester = userActiveSemester() + activeSemester = current_user.active_semester if activeSemester.label == "WS": return activeSemester.year + 1 else: @@ -351,7 +351,7 @@ class SemesterView(SecureAdminModelView): return Semester.initFromOldSemester( label=form.label.data, year=form.year.data, - oldSemester=userActiveSemester(), + oldSemester=current_user.active_semester, transferParts=form.transfer_parts.data, transferAssistants=form.transfer_assistants.data, ) @@ -370,7 +370,7 @@ class SemesterView(SecureAdminModelView): category._children.insert(0, link) def after_model_change(self, form, model, is_created): - setUserActiveSemester(model.id) + setUserActiveSemester(model) SemesterView.addMenuLink(adminSpace, model) SemesterView.addMenuLink(assistantSpace, model) @@ -412,10 +412,10 @@ class PartView(SecureAdminModelView): column_searchable_list = ["program.label", "number"] def queryFilter(self): - return Part.semester == userActiveSemester() + return Part.semester == current_user.active_semester def customCreateModel(self, form): - return Part(program=form.program.data, number=form.number.data, semester=userActiveSemester()) + return Part(program=form.program.data, number=form.number.data, semester=current_user.active_semester) class StudentView(SecureAdminModelView): @@ -469,11 +469,11 @@ class StudentView(SecureAdminModelView): def partQueryFactory(): - return Part.query.filter(Part.semester == userActiveSemester()) + return Part.query.filter(Part.semester == current_user.active_semester) def groupQueryFactory(): - return Group.query.filter(Group.semester == userActiveSemester()) + return Group.query.filter(Group.semester == current_user.active_semester) class PartRowFilter(FilterEqual): @@ -481,7 +481,7 @@ class PartRowFilter(FilterEqual): if not has_request_context(): return tuple() - parts = Part.query.filter(Part.semester == userActiveSemester()) + parts = Part.query.filter(Part.semester == current_user.active_semester) return tuple((part.id, f"{part.program.repr()}{part.number}") for part in parts) @@ -568,14 +568,14 @@ class PartStudentView(SecureAdminModelView): ] def queryFilter(self): - return PartStudent.part.has(Part.semester == userActiveSemester()) + return PartStudent.part.has(Part.semester == current_user.active_semester) def on_model_change(self, form, model, is_created): PartStudent.check(model.part, model.group) def partStudentQueryFactory(): - return PartStudent.query.filter(PartStudent.part.has(Part.semester == userActiveSemester())) + return PartStudent.query.filter(PartStudent.part.has(Part.semester == current_user.active_semester)) class ProgramRowFilter(FilterEqual): @@ -635,7 +635,7 @@ class GroupView(SecureAdminModelView): refreshFiltersCache = True def queryFilter(self): - return Group.semester == userActiveSemester() + return Group.semester == current_user.active_semester def customCreateModel(self, form): return Group.customInit(form.part_students.data) @@ -796,11 +796,11 @@ class SemesterExperimentView(SecureAdminModelView): ] def queryFilter(self): - return SemesterExperiment.semester == userActiveSemester() + return SemesterExperiment.semester == current_user.active_semester def customCreateModel(self, form): return SemesterExperiment( - semester=userActiveSemester(), + semester=current_user.active_semester, oral_weighting=form.oral_weighting.data, protocol_weighting=form.protocol_weighting.data, final_weighting=form.final_weighting.data, @@ -1052,7 +1052,7 @@ class GroupExperimentView(SecureAdminModelView): ] def queryFilter(self): - return GroupExperiment.group.has(Group.semester == userActiveSemester()) + return GroupExperiment.group.has(Group.semester == current_user.active_semester) def customCreateModel(self, form): return GroupExperiment(semester_experiment=form.semester_experiment.data, group=form.group.data) @@ -1076,7 +1076,7 @@ class GroupExperimentView(SecureAdminModelView): def groupExperimentQueryFactory(): return GroupExperiment.query.filter( - GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) + GroupExperiment.semester_experiment.has(SemesterExperiment.semester == current_user.active_semester) ) @@ -1173,7 +1173,7 @@ class AppointmentView(SecureAdminModelView): def queryFilter(self): return Appointment.group_experiment.has( - GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) + GroupExperiment.semester_experiment.has(SemesterExperiment.semester == current_user.active_semester) ) def customCreateModel(self, form): @@ -1338,7 +1338,7 @@ class ExperimentMarkView(SecureAdminModelView): # Deactivated for the experiments history of a student. def queryFilter(self): return ExperimentMark.group_experiment.has( - GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) + GroupExperiment.semester_experiment.has(SemesterExperiment.semester == current_user.active_semester) ) """ @@ -1425,7 +1425,7 @@ class ActionsView(SecureAdminBaseView): if form.validate_on_submit(): if form.manualUpdateFinalExperimentAndPartMarksSubmit.data: - for semesterExperiment in userActiveSemester().semester_experiments: + for semesterExperiment in current_user.active_semester.semester_experiments: semesterExperiment.updateFinalExperimentAndPartMarks() flash("Manually updated all final experiment and part marks", "success") @@ -1519,7 +1519,7 @@ class AnalysisView(SecureAdminBaseView): ) if form.finalPartMarksSubmit.data: - parts = userActiveSemester().parts + parts = current_user.active_semester.parts activeSemesterFinalPartMarksHists = [ AnalysisView.markHist( data=np.array([partStudent.final_part_mark for partStudent in part.part_students]), diff --git a/advlabdb/assistantModelViews.py b/advlabdb/assistantModelViews.py index 1b6af43..627ae77 100644 --- a/advlabdb/assistantModelViews.py +++ b/advlabdb/assistantModelViews.py @@ -11,7 +11,7 @@ from . import assistantSpace, db from .advlabdb_independent_funs import flashRandomPassword, randomPassword from .customClasses import SecureAssistantBaseView, SecureAssistantModelView from .exceptions import DataBaseException, ModelViewException -from .model_dependent_funs import initActiveSemesterMenuLinks, userActiveSemester +from .model_dependent_funs import initActiveSemesterMenuLinks from .models import ( MAX_MARK, MIN_MARK, @@ -53,12 +53,14 @@ class AssistantAppointmentView(SecureAssistantModelView): ] form_columns = column_editable_list - def queryFilter(self): - return and_( - Appointment.group_experiment.has( - GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) - ), - Appointment.assistant == current_user.assistant, + def query_modifier(self, query): + return ( + query.join(GroupExperiment) + .join(SemesterExperiment) + .where( + SemesterExperiment.semester == current_user.active_semester, + Appointment.assistant == current_user.assistant, + ) ) @@ -114,7 +116,7 @@ class AssistantExperimentMarkView(SecureAssistantModelView): return ExperimentMark.group_experiment.has( GroupExperiment.semester_experiment.has( and_( - SemesterExperiment.semester == userActiveSemester(), + SemesterExperiment.semester == current_user.active_semester, SemesterExperiment.assistants.any(Assistant.user == current_user), ) ) diff --git a/advlabdb/customClasses.py b/advlabdb/customClasses.py index e7ae61f..b3a62fa 100644 --- a/advlabdb/customClasses.py +++ b/advlabdb/customClasses.py @@ -9,7 +9,6 @@ from sqlalchemy import and_, select from . import db from .exceptions import DataBaseException, ModelViewException -from .model_dependent_funs import userActiveSemester from .model_independent_funs import get_count, reportBadAttempt from .models import ( Assistant, @@ -45,7 +44,7 @@ class SecureAdminIndexView(CustomIndexView): select(ExperimentMark.final_experiment_mark) .join(PartStudent) .join(Part) - .where(Part.semester == userActiveSemester()) + .where(Part.semester == current_user.active_semester) ) number_of_all_experiment_marks = get_count(active_semester_experiment_mark_ids_stmt) @@ -70,7 +69,7 @@ class SecureAssistantIndexView(CustomIndexView): select(ExperimentMark.final_experiment_mark) .join(GroupExperiment) .join(SemesterExperiment) - .where(SemesterExperiment.semester == userActiveSemester()) + .where(SemesterExperiment.semester == current_user.active_semester) .join(SemesterExperiment.assistants) .where(Assistant.user == current_user) ) diff --git a/advlabdb/model_dependent_funs.py b/advlabdb/model_dependent_funs.py index 68ab657..ff49cc0 100644 --- a/advlabdb/model_dependent_funs.py +++ b/advlabdb/model_dependent_funs.py @@ -24,39 +24,21 @@ def lastSemester(): return sortedSemestersStartingWithNewest(limit=1).first() -def userActiveSemester(flashWarning=False): - lastSemesterId = lastSemester().id - if current_user.active_semester_id is None: - current_user.active_semester_id = lastSemesterId - db.session.commit() - elif current_user.active_semester_id != lastSemesterId: - activeSemester = db.session.get(Semester, current_user.active_semester_id) - if activeSemester is not None: - if flashWarning: - flash(f"You are in the old semester {activeSemester.repr()}!", "warning") - else: - current_user.active_semester_id = lastSemesterId +def setUserActiveSemester(semester): + if semester is None: + flash(f"Can not change the active semester to None!", "error") + return + + if current_user.active_semester != semester: + try: + current_user.active_semester = semester db.session.commit() - flash("Semester changed!", "warning") + except Exception as ex: + flash(str(ex), "error") - return db.session.get(Semester, current_user.active_semester_id) - - -def setUserActiveSemester(semesterId): - if current_user.active_semester_id != semesterId: - semester = db.session.get(Semester, semesterId) - if semester is not None: - try: - current_user.active_semester_id = semesterId - db.session.commit() - except Exception as ex: - flash(str(ex), "error") - - db.session.rollback() - else: - flash(f"Active semester changed to {semester.repr()}!", "warning") + db.session.rollback() else: - flash(f"No semester with ID {semesterId}!", "error") + flash(f"Active semester changed to {semester.repr()}!", "warning") def initActiveSemesterMenuLinks(space): diff --git a/advlabdb/routes.py b/advlabdb/routes.py index aeaaad9..c506978 100644 --- a/advlabdb/routes.py +++ b/advlabdb/routes.py @@ -1,13 +1,18 @@ from flask import flash, redirect, render_template, request, url_for from flask_security import auth_required, current_user, roles_accepted, roles_required -from . import app -from .model_dependent_funs import setUserActiveSemester, userActiveSemester +from . import app, db +from .model_dependent_funs import lastSemester, setUserActiveSemester +from .models import Semester @app.context_processor def util_processor(): - return dict(userActiveSemester=userActiveSemester, current_user=current_user) + active_semester = current_user.active_semester + if active_semester != lastSemester(): + flash(f"You are in the old semester {current_user.active_semester.repr()}!", "warning") + + return dict(active_semester=active_semester, current_user=current_user) @app.route("/") @@ -28,7 +33,8 @@ def set_semester(): except Exception as ex: flash(str(ex), "error") else: - setUserActiveSemester(semesterId) + semester = db.session.get(Semester, semesterId) + setUserActiveSemester(semester) red = request.referrer or url_for("index") return redirect(red) diff --git a/advlabdb/templates/actions.html b/advlabdb/templates/actions.html index 45a1a02..549cccb 100644 --- a/advlabdb/templates/actions.html +++ b/advlabdb/templates/actions.html @@ -2,7 +2,7 @@ {% extends "admin/master.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}}