diff --git a/evap/contributor/tests/test_views.py b/evap/contributor/tests/test_views.py index 4db2451b10..833e8341e4 100644 --- a/evap/contributor/tests/test_views.py +++ b/evap/contributor/tests/test_views.py @@ -6,6 +6,7 @@ from evap.evaluation.models import Contribution, Course, Evaluation, Questionnaire, UserProfile from evap.evaluation.tests.tools import ( + FuzzyInt, WebTest, WebTestWith200Check, create_evaluation_with_responsible_and_editor, @@ -82,8 +83,31 @@ class TestContributorView(WebTestWith200Check): @classmethod def setUpTestData(cls): - users = create_evaluation_with_responsible_and_editor() - cls.test_users = [users["editor"], users["responsible"]] + result = create_evaluation_with_responsible_and_editor() + cls.responsible = result["responsible"] + cls.test_users = [result["editor"], cls.responsible] + + def test_num_queries_is_not_constant(self): + """Current defect, see https://github.com/e-valuation/EvaP/issues/1229#issuecomment-4239035495.""" + represented = baker.make(UserProfile, email="represented@example.com") + self.responsible.represented_users.add(represented) + evaluations = baker.make( + Evaluation, + name_en=iter(range(10)), + name_de=iter(range(10)), + state=Evaluation.State.PUBLISHED, + course__responsibles=[represented], + _quantity=10, + _bulk_create=True, + ) + baker.make( + Contribution, + evaluation=iter(evaluations), + _quantity=10, + _bulk_create=True, + ) + with self.assertNumQueries(FuzzyInt(80, 100)): + self.app.get(self.url, user=self.responsible) class TestContributorEvaluationView(WebTestWith200Check): diff --git a/evap/contributor/views.py b/evap/contributor/views.py index aa62b74576..71a86fdca2 100644 --- a/evap/contributor/views.py +++ b/evap/contributor/views.py @@ -58,9 +58,10 @@ def index(request): ) own_evaluations = ( - Evaluation.objects.filter(course__in=own_courses) + Evaluation.annotate_with_participant_and_voter_counts(Evaluation.objects.filter(course__in=own_courses)) .annotate(contributes_to=Exists(Evaluation.objects.filter(id=OuterRef("id"), contributions__contributor=user))) - .prefetch_related("course", "course__evaluations", "course__programs", "course__type", "course__semester") + .select_related("course", "course__type", "course__semester") + .prefetch_related("course__evaluations", "course__programs") ) own_evaluations = [evaluation for evaluation in own_evaluations if evaluation.can_be_seen_by(user)] @@ -77,9 +78,14 @@ def index(request): ) ) ) - delegated_evaluations = Evaluation.objects.filter(course__in=delegated_courses).prefetch_related( - "course", "course__evaluations", "course__programs", "course__type", "course__semester" + delegated_evaluations = ( + Evaluation.annotate_with_participant_and_voter_counts( + Evaluation.objects.filter(course__in=delegated_courses) + ) + .select_related("course", "course__type", "course__semester") + .prefetch_related("course__evaluations", "course__programs", "course__responsibles") ) + delegated_evaluations = [evaluation for evaluation in delegated_evaluations if evaluation.can_be_seen_by(user)] for evaluation in delegated_evaluations: evaluation.delegated_evaluation = True