From 6618e72843ca4a4b8a2e7b8c9a88107651af672f Mon Sep 17 00:00:00 2001 From: Mo8it Date: Tue, 17 May 2022 00:42:49 +0200 Subject: [PATCH] Improve performance by removing userActiveSemester() --- advlabdb/adminModelViews.py | 46 +++++++++---------- advlabdb/assistantModelViews.py | 18 ++++---- advlabdb/customClasses.py | 5 +- advlabdb/model_dependent_funs.py | 42 +++++------------ advlabdb/routes.py | 14 ++++-- advlabdb/templates/actions.html | 2 +- advlabdb/templates/admin_create.html | 4 +- advlabdb/templates/admin_details.html | 4 +- advlabdb/templates/admin_edit.html | 4 +- advlabdb/templates/admin_index.html | 4 +- advlabdb/templates/admin_list.html | 4 +- advlabdb/templates/analysis/analysis.html | 2 +- .../templates/analysis/assistant_marks.html | 2 +- .../templates/analysis/final_part_marks.html | 2 +- advlabdb/templates/assistant_create.html | 2 +- advlabdb/templates/assistant_details.html | 4 +- advlabdb/templates/assistant_edit.html | 4 +- advlabdb/templates/assistant_index.html | 4 +- advlabdb/templates/assistant_list.html | 4 +- advlabdb/templates/import.html | 2 +- advlabdb/templates/macros.html | 6 +-- 21 files changed, 84 insertions(+), 95 deletions(-) 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")}}
diff --git a/advlabdb/templates/admin_create.html b/advlabdb/templates/admin_create.html index 2d9ac8f..0e6dd50 100644 --- a/advlabdb/templates/admin_create.html +++ b/advlabdb/templates/admin_create.html @@ -2,6 +2,6 @@ {% extends "admin/model/create.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/admin_details.html b/advlabdb/templates/admin_details.html index 3119844..f89e673 100644 --- a/advlabdb/templates/admin_details.html +++ b/advlabdb/templates/admin_details.html @@ -2,6 +2,6 @@ {% extends "admin/model/details.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/admin_edit.html b/advlabdb/templates/admin_edit.html index 44b0d74..b0314d4 100644 --- a/advlabdb/templates/admin_edit.html +++ b/advlabdb/templates/admin_edit.html @@ -2,6 +2,6 @@ {% extends "admin/model/edit.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/admin_index.html b/advlabdb/templates/admin_index.html index 3e04a7c..f5accb6 100644 --- a/advlabdb/templates/admin_index.html +++ b/advlabdb/templates/admin_index.html @@ -2,11 +2,11 @@ {% extends "admin/index.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}}
{{missing_final_experiment_marks(number_of_missing_final_experiment_marks, number_of_all_experiment_marks)}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/admin_list.html b/advlabdb/templates/admin_list.html index fc60f63..a0f9662 100644 --- a/advlabdb/templates/admin_list.html +++ b/advlabdb/templates/admin_list.html @@ -2,6 +2,6 @@ {% extends "admin/model/list.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/analysis/analysis.html b/advlabdb/templates/analysis/analysis.html index ff3014a..8ba4ff5 100644 --- a/advlabdb/templates/analysis/analysis.html +++ b/advlabdb/templates/analysis/analysis.html @@ -2,7 +2,7 @@ {% extends "admin/master.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}}
diff --git a/advlabdb/templates/analysis/assistant_marks.html b/advlabdb/templates/analysis/assistant_marks.html index 0d3cbfc..8753375 100644 --- a/advlabdb/templates/analysis/assistant_marks.html +++ b/advlabdb/templates/analysis/assistant_marks.html @@ -2,7 +2,7 @@ {% extends "admin/master.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}}
diff --git a/advlabdb/templates/analysis/final_part_marks.html b/advlabdb/templates/analysis/final_part_marks.html index 9f20ec3..3692077 100644 --- a/advlabdb/templates/analysis/final_part_marks.html +++ b/advlabdb/templates/analysis/final_part_marks.html @@ -2,7 +2,7 @@ {% extends "admin/master.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}}
diff --git a/advlabdb/templates/assistant_create.html b/advlabdb/templates/assistant_create.html index 458f081..d16cf7e 100644 --- a/advlabdb/templates/assistant_create.html +++ b/advlabdb/templates/assistant_create.html @@ -2,6 +2,6 @@ {% extends "admin/model/create.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="assistant")}} + {{information(current_user, active_semester, role="assistant")}} {{ super() }} {% endblock %} diff --git a/advlabdb/templates/assistant_details.html b/advlabdb/templates/assistant_details.html index 68a4f72..e2bca28 100644 --- a/advlabdb/templates/assistant_details.html +++ b/advlabdb/templates/assistant_details.html @@ -2,6 +2,6 @@ {% extends "admin/model/details.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="assistant")}} + {{information(current_user, active_semester, role="assistant")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/assistant_edit.html b/advlabdb/templates/assistant_edit.html index ad9da0b..c1435ad 100644 --- a/advlabdb/templates/assistant_edit.html +++ b/advlabdb/templates/assistant_edit.html @@ -2,6 +2,6 @@ {% extends "admin/model/edit.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="assistant")}} + {{information(current_user, active_semester, role="assistant")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/assistant_index.html b/advlabdb/templates/assistant_index.html index c83ee0d..9ef0e7b 100644 --- a/advlabdb/templates/assistant_index.html +++ b/advlabdb/templates/assistant_index.html @@ -2,11 +2,11 @@ {% extends "admin/index.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="assistant")}} + {{information(current_user, active_semester, role="assistant")}}
{{missing_final_experiment_marks(number_of_missing_final_experiment_marks, number_of_all_experiment_marks)}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/assistant_list.html b/advlabdb/templates/assistant_list.html index 8538642..36d767e 100644 --- a/advlabdb/templates/assistant_list.html +++ b/advlabdb/templates/assistant_list.html @@ -2,6 +2,6 @@ {% extends "admin/model/list.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="assistant")}} + {{information(current_user, active_semster, role="assistant")}} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/advlabdb/templates/import.html b/advlabdb/templates/import.html index 359331f..69a8205 100644 --- a/advlabdb/templates/import.html +++ b/advlabdb/templates/import.html @@ -2,7 +2,7 @@ {% extends "admin/master.html" %} {% block body %} - {{information(current_user, userActiveSemester, role="admin")}} + {{information(current_user, active_semester, role="admin")}}
diff --git a/advlabdb/templates/macros.html b/advlabdb/templates/macros.html index 9a8c87a..71ca8c4 100644 --- a/advlabdb/templates/macros.html +++ b/advlabdb/templates/macros.html @@ -1,7 +1,7 @@ -{% macro information(current_user, userActiveSemester, role) %} +{% macro information(current_user, active_semester, role) %} User: {{ current_user.repr() }} - | Active semester: {{ userActiveSemester(flashWarning=True).repr() }} + | Active semester: {{ active_semester.repr() }} {% if (role == "admin") and (current_user.has_role("assistant")) %} | Assistant space. @@ -15,4 +15,4 @@ Number of missing final experiment marks: {{ number_of_missing_final_experiment_marks }} / {{ number_of_all_experiment_marks }}

-{% endmacro %} \ No newline at end of file +{% endmacro %}