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

Improve performance by removing userActiveSemester()

This commit is contained in:
Mo 2022-05-17 00:42:49 +02:00
parent b2428d29f3
commit 6618e72843
21 changed files with 84 additions and 95 deletions

View file

@ -42,9 +42,9 @@ from .database_import import importFromFile
from .exceptions import DataBaseException, ModelViewException from .exceptions import DataBaseException, ModelViewException
from .model_dependent_funs import ( from .model_dependent_funs import (
initActiveSemesterMenuLinks, initActiveSemesterMenuLinks,
lastSemester,
setUserActiveSemester, setUserActiveSemester,
sortedSemestersStartingWithNewest, sortedSemestersStartingWithNewest,
userActiveSemester,
) )
from .models import ( from .models import (
MAX_MARK, MAX_MARK,
@ -70,7 +70,7 @@ from .models import (
def semesterExperimentQueryFactory(): def semesterExperimentQueryFactory():
return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester()) return SemesterExperiment.query.filter(SemesterExperiment.semester == current_user.active_semester)
class SemesterRowFilter(FilterEqual): class SemesterRowFilter(FilterEqual):
@ -150,7 +150,7 @@ class UserView(SecureAdminModelView):
query_factory=semesterQueryFactory, query_factory=semesterQueryFactory,
allow_blank=True, allow_blank=True,
blank_text="-", blank_text="-",
default=userActiveSemester, default=lastSemester,
description="Not fixed and users (including assistants) can change it.", description="Not fixed and users (including assistants) can change it.",
) )
@ -289,13 +289,13 @@ class RoleView(SecureAdminModelView):
class SemesterView(SecureAdminModelView): class SemesterView(SecureAdminModelView):
class CreateForm(Form): class CreateForm(Form):
def defaultFormLabel(): def defaultFormLabel():
if userActiveSemester().label == "WS": if current_user.active_semester.label == "WS":
return "SS" return "SS"
else: else:
return "WS" return "WS"
def defaultFormYear(): def defaultFormYear():
activeSemester = userActiveSemester() activeSemester = current_user.active_semester
if activeSemester.label == "WS": if activeSemester.label == "WS":
return activeSemester.year + 1 return activeSemester.year + 1
else: else:
@ -351,7 +351,7 @@ class SemesterView(SecureAdminModelView):
return Semester.initFromOldSemester( return Semester.initFromOldSemester(
label=form.label.data, label=form.label.data,
year=form.year.data, year=form.year.data,
oldSemester=userActiveSemester(), oldSemester=current_user.active_semester,
transferParts=form.transfer_parts.data, transferParts=form.transfer_parts.data,
transferAssistants=form.transfer_assistants.data, transferAssistants=form.transfer_assistants.data,
) )
@ -370,7 +370,7 @@ class SemesterView(SecureAdminModelView):
category._children.insert(0, link) category._children.insert(0, link)
def after_model_change(self, form, model, is_created): def after_model_change(self, form, model, is_created):
setUserActiveSemester(model.id) setUserActiveSemester(model)
SemesterView.addMenuLink(adminSpace, model) SemesterView.addMenuLink(adminSpace, model)
SemesterView.addMenuLink(assistantSpace, model) SemesterView.addMenuLink(assistantSpace, model)
@ -412,10 +412,10 @@ class PartView(SecureAdminModelView):
column_searchable_list = ["program.label", "number"] column_searchable_list = ["program.label", "number"]
def queryFilter(self): def queryFilter(self):
return Part.semester == userActiveSemester() return Part.semester == current_user.active_semester
def customCreateModel(self, form): 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): class StudentView(SecureAdminModelView):
@ -469,11 +469,11 @@ class StudentView(SecureAdminModelView):
def partQueryFactory(): def partQueryFactory():
return Part.query.filter(Part.semester == userActiveSemester()) return Part.query.filter(Part.semester == current_user.active_semester)
def groupQueryFactory(): def groupQueryFactory():
return Group.query.filter(Group.semester == userActiveSemester()) return Group.query.filter(Group.semester == current_user.active_semester)
class PartRowFilter(FilterEqual): class PartRowFilter(FilterEqual):
@ -481,7 +481,7 @@ class PartRowFilter(FilterEqual):
if not has_request_context(): if not has_request_context():
return tuple() 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) return tuple((part.id, f"{part.program.repr()}{part.number}") for part in parts)
@ -568,14 +568,14 @@ class PartStudentView(SecureAdminModelView):
] ]
def queryFilter(self): 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): def on_model_change(self, form, model, is_created):
PartStudent.check(model.part, model.group) PartStudent.check(model.part, model.group)
def partStudentQueryFactory(): 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): class ProgramRowFilter(FilterEqual):
@ -635,7 +635,7 @@ class GroupView(SecureAdminModelView):
refreshFiltersCache = True refreshFiltersCache = True
def queryFilter(self): def queryFilter(self):
return Group.semester == userActiveSemester() return Group.semester == current_user.active_semester
def customCreateModel(self, form): def customCreateModel(self, form):
return Group.customInit(form.part_students.data) return Group.customInit(form.part_students.data)
@ -796,11 +796,11 @@ class SemesterExperimentView(SecureAdminModelView):
] ]
def queryFilter(self): def queryFilter(self):
return SemesterExperiment.semester == userActiveSemester() return SemesterExperiment.semester == current_user.active_semester
def customCreateModel(self, form): def customCreateModel(self, form):
return SemesterExperiment( return SemesterExperiment(
semester=userActiveSemester(), semester=current_user.active_semester,
oral_weighting=form.oral_weighting.data, oral_weighting=form.oral_weighting.data,
protocol_weighting=form.protocol_weighting.data, protocol_weighting=form.protocol_weighting.data,
final_weighting=form.final_weighting.data, final_weighting=form.final_weighting.data,
@ -1052,7 +1052,7 @@ class GroupExperimentView(SecureAdminModelView):
] ]
def queryFilter(self): def queryFilter(self):
return GroupExperiment.group.has(Group.semester == userActiveSemester()) return GroupExperiment.group.has(Group.semester == current_user.active_semester)
def customCreateModel(self, form): def customCreateModel(self, form):
return GroupExperiment(semester_experiment=form.semester_experiment.data, group=form.group.data) return GroupExperiment(semester_experiment=form.semester_experiment.data, group=form.group.data)
@ -1076,7 +1076,7 @@ class GroupExperimentView(SecureAdminModelView):
def groupExperimentQueryFactory(): def groupExperimentQueryFactory():
return GroupExperiment.query.filter( 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): def queryFilter(self):
return Appointment.group_experiment.has( 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): def customCreateModel(self, form):
@ -1338,7 +1338,7 @@ class ExperimentMarkView(SecureAdminModelView):
# Deactivated for the experiments history of a student. # Deactivated for the experiments history of a student.
def queryFilter(self): def queryFilter(self):
return ExperimentMark.group_experiment.has( 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.validate_on_submit():
if form.manualUpdateFinalExperimentAndPartMarksSubmit.data: if form.manualUpdateFinalExperimentAndPartMarksSubmit.data:
for semesterExperiment in userActiveSemester().semester_experiments: for semesterExperiment in current_user.active_semester.semester_experiments:
semesterExperiment.updateFinalExperimentAndPartMarks() semesterExperiment.updateFinalExperimentAndPartMarks()
flash("Manually updated all final experiment and part marks", "success") flash("Manually updated all final experiment and part marks", "success")
@ -1519,7 +1519,7 @@ class AnalysisView(SecureAdminBaseView):
) )
if form.finalPartMarksSubmit.data: if form.finalPartMarksSubmit.data:
parts = userActiveSemester().parts parts = current_user.active_semester.parts
activeSemesterFinalPartMarksHists = [ activeSemesterFinalPartMarksHists = [
AnalysisView.markHist( AnalysisView.markHist(
data=np.array([partStudent.final_part_mark for partStudent in part.part_students]), data=np.array([partStudent.final_part_mark for partStudent in part.part_students]),

View file

@ -11,7 +11,7 @@ from . import assistantSpace, db
from .advlabdb_independent_funs import flashRandomPassword, randomPassword from .advlabdb_independent_funs import flashRandomPassword, randomPassword
from .customClasses import SecureAssistantBaseView, SecureAssistantModelView from .customClasses import SecureAssistantBaseView, SecureAssistantModelView
from .exceptions import DataBaseException, ModelViewException from .exceptions import DataBaseException, ModelViewException
from .model_dependent_funs import initActiveSemesterMenuLinks, userActiveSemester from .model_dependent_funs import initActiveSemesterMenuLinks
from .models import ( from .models import (
MAX_MARK, MAX_MARK,
MIN_MARK, MIN_MARK,
@ -53,13 +53,15 @@ class AssistantAppointmentView(SecureAssistantModelView):
] ]
form_columns = column_editable_list form_columns = column_editable_list
def queryFilter(self): def query_modifier(self, query):
return and_( return (
Appointment.group_experiment.has( query.join(GroupExperiment)
GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) .join(SemesterExperiment)
), .where(
SemesterExperiment.semester == current_user.active_semester,
Appointment.assistant == current_user.assistant, Appointment.assistant == current_user.assistant,
) )
)
class AssistantExperimentMarkView(SecureAssistantModelView): class AssistantExperimentMarkView(SecureAssistantModelView):
@ -114,7 +116,7 @@ class AssistantExperimentMarkView(SecureAssistantModelView):
return ExperimentMark.group_experiment.has( return ExperimentMark.group_experiment.has(
GroupExperiment.semester_experiment.has( GroupExperiment.semester_experiment.has(
and_( and_(
SemesterExperiment.semester == userActiveSemester(), SemesterExperiment.semester == current_user.active_semester,
SemesterExperiment.assistants.any(Assistant.user == current_user), SemesterExperiment.assistants.any(Assistant.user == current_user),
) )
) )

View file

@ -9,7 +9,6 @@ from sqlalchemy import and_, select
from . import db from . import db
from .exceptions import DataBaseException, ModelViewException from .exceptions import DataBaseException, ModelViewException
from .model_dependent_funs import userActiveSemester
from .model_independent_funs import get_count, reportBadAttempt from .model_independent_funs import get_count, reportBadAttempt
from .models import ( from .models import (
Assistant, Assistant,
@ -45,7 +44,7 @@ class SecureAdminIndexView(CustomIndexView):
select(ExperimentMark.final_experiment_mark) select(ExperimentMark.final_experiment_mark)
.join(PartStudent) .join(PartStudent)
.join(Part) .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) 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) select(ExperimentMark.final_experiment_mark)
.join(GroupExperiment) .join(GroupExperiment)
.join(SemesterExperiment) .join(SemesterExperiment)
.where(SemesterExperiment.semester == userActiveSemester()) .where(SemesterExperiment.semester == current_user.active_semester)
.join(SemesterExperiment.assistants) .join(SemesterExperiment.assistants)
.where(Assistant.user == current_user) .where(Assistant.user == current_user)
) )

View file

@ -24,30 +24,14 @@ def lastSemester():
return sortedSemestersStartingWithNewest(limit=1).first() return sortedSemestersStartingWithNewest(limit=1).first()
def userActiveSemester(flashWarning=False): def setUserActiveSemester(semester):
lastSemesterId = lastSemester().id if semester is None:
if current_user.active_semester_id is None: flash(f"Can not change the active semester to None!", "error")
current_user.active_semester_id = lastSemesterId return
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
db.session.commit()
flash("Semester changed!", "warning")
return db.session.get(Semester, current_user.active_semester_id) if current_user.active_semester != semester:
def setUserActiveSemester(semesterId):
if current_user.active_semester_id != semesterId:
semester = db.session.get(Semester, semesterId)
if semester is not None:
try: try:
current_user.active_semester_id = semesterId current_user.active_semester = semester
db.session.commit() db.session.commit()
except Exception as ex: except Exception as ex:
flash(str(ex), "error") flash(str(ex), "error")
@ -55,8 +39,6 @@ def setUserActiveSemester(semesterId):
db.session.rollback() db.session.rollback()
else: else:
flash(f"Active semester changed to {semester.repr()}!", "warning") flash(f"Active semester changed to {semester.repr()}!", "warning")
else:
flash(f"No semester with ID {semesterId}!", "error")
def initActiveSemesterMenuLinks(space): def initActiveSemesterMenuLinks(space):

View file

@ -1,13 +1,18 @@
from flask import flash, redirect, render_template, request, url_for from flask import flash, redirect, render_template, request, url_for
from flask_security import auth_required, current_user, roles_accepted, roles_required from flask_security import auth_required, current_user, roles_accepted, roles_required
from . import app from . import app, db
from .model_dependent_funs import setUserActiveSemester, userActiveSemester from .model_dependent_funs import lastSemester, setUserActiveSemester
from .models import Semester
@app.context_processor @app.context_processor
def util_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("/") @app.route("/")
@ -28,7 +33,8 @@ def set_semester():
except Exception as ex: except Exception as ex:
flash(str(ex), "error") flash(str(ex), "error")
else: else:
setUserActiveSemester(semesterId) semester = db.session.get(Semester, semesterId)
setUserActiveSemester(semester)
red = request.referrer or url_for("index") red = request.referrer or url_for("index")
return redirect(red) return redirect(red)

View file

@ -2,7 +2,7 @@
{% extends "admin/master.html" %} {% extends "admin/master.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
<hr> <hr>

View file

@ -2,6 +2,6 @@
{% extends "admin/model/create.html" %} {% extends "admin/model/create.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,6 +2,6 @@
{% extends "admin/model/details.html" %} {% extends "admin/model/details.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,6 +2,6 @@
{% extends "admin/model/edit.html" %} {% extends "admin/model/edit.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,7 +2,7 @@
{% extends "admin/index.html" %} {% extends "admin/index.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
<hr> <hr>

View file

@ -2,6 +2,6 @@
{% extends "admin/model/list.html" %} {% extends "admin/model/list.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,7 +2,7 @@
{% extends "admin/master.html" %} {% extends "admin/master.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
<hr> <hr>

View file

@ -2,7 +2,7 @@
{% extends "admin/master.html" %} {% extends "admin/master.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
<hr> <hr>

View file

@ -2,7 +2,7 @@
{% extends "admin/master.html" %} {% extends "admin/master.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
<hr> <hr>

View file

@ -2,6 +2,6 @@
{% extends "admin/model/create.html" %} {% extends "admin/model/create.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="assistant")}} {{information(current_user, active_semester, role="assistant")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,6 +2,6 @@
{% extends "admin/model/details.html" %} {% extends "admin/model/details.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="assistant")}} {{information(current_user, active_semester, role="assistant")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,6 +2,6 @@
{% extends "admin/model/edit.html" %} {% extends "admin/model/edit.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="assistant")}} {{information(current_user, active_semester, role="assistant")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,7 +2,7 @@
{% extends "admin/index.html" %} {% extends "admin/index.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="assistant")}} {{information(current_user, active_semester, role="assistant")}}
<hr> <hr>

View file

@ -2,6 +2,6 @@
{% extends "admin/model/list.html" %} {% extends "admin/model/list.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="assistant")}} {{information(current_user, active_semster, role="assistant")}}
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -2,7 +2,7 @@
{% extends "admin/master.html" %} {% extends "admin/master.html" %}
{% block body %} {% block body %}
{{information(current_user, userActiveSemester, role="admin")}} {{information(current_user, active_semester, role="admin")}}
<hr> <hr>

View file

@ -1,7 +1,7 @@
{% macro information(current_user, userActiveSemester, role) %} {% macro information(current_user, active_semester, role) %}
User: <a href="{{ url_for('index') }}{{ role }}/user/details/?id={{ current_user.id }}">{{ current_user.repr() }}</a> User: <a href="{{ url_for('index') }}{{ role }}/user/details/?id={{ current_user.id }}">{{ current_user.repr() }}</a>
| Active semester: {{ userActiveSemester(flashWarning=True).repr() }} | Active semester: {{ active_semester.repr() }}
{% if (role == "admin") and (current_user.has_role("assistant")) %} {% if (role == "admin") and (current_user.has_role("assistant")) %}
| <a href="{{ url_for('index') }}assistant">Assistant space</a>. | <a href="{{ url_for('index') }}assistant">Assistant space</a>.