Skip to content
Snippets Groups Projects
Verified Commit f24a99fa authored by magicfelix's avatar magicfelix Committed by Jonathan Weth
Browse files

Optimize and drop unnecessary coursebook queries

parent dd2685c6
No related branches found
No related tags found
1 merge request!429Resolve "Adapt to active school term"
...@@ -139,7 +139,6 @@ class Documentation(CalendarEvent): ...@@ -139,7 +139,6 @@ class Documentation(CalendarEvent):
events: list, events: list,
incomplete: Optional[bool] = False, incomplete: Optional[bool] = False,
absences_exist: Optional[bool] = False, absences_exist: Optional[bool] = False,
request: Optional[HttpRequest] = None,
) -> tuple: ) -> tuple:
"""Get all the documentations for the events. """Get all the documentations for the events.
Create dummy documentations if none exist. Create dummy documentations if none exist.
...@@ -149,11 +148,15 @@ class Documentation(CalendarEvent): ...@@ -149,11 +148,15 @@ class Documentation(CalendarEvent):
dummies = [] dummies = []
# Prefetch existing documentations to speed things up # Prefetch existing documentations to speed things up
existing_documentations = Documentation.objects.filter( existing_documentations = (
datetime_start__lte=datetime_end, Documentation.objects.filter(
datetime_end__gte=datetime_start, datetime_start__lte=datetime_end,
amends__in=[e["REFERENCE_OBJECT"] for e in events], datetime_end__gte=datetime_start,
).prefetch_related("participations") amends__in=[e["REFERENCE_OBJECT"] for e in events],
)
.prefetch_related("participations", "teachers")
.select_related("course", "subject")
)
for event in events: for event in events:
if incomplete and event["STATUS"] == "CANCELLED": if incomplete and event["STATUS"] == "CANCELLED":
...@@ -214,7 +217,6 @@ class Documentation(CalendarEvent): ...@@ -214,7 +217,6 @@ class Documentation(CalendarEvent):
start: datetime, start: datetime,
end: datetime, end: datetime,
incomplete: Optional[bool] = False, incomplete: Optional[bool] = False,
request: Optional[HttpRequest] = None,
) -> tuple: ) -> tuple:
"""Get all the documentations for the person from start to end datetime. """Get all the documentations for the person from start to end datetime.
Create dummy documentations if none exist. Create dummy documentations if none exist.
...@@ -233,7 +235,7 @@ class Documentation(CalendarEvent): ...@@ -233,7 +235,7 @@ class Documentation(CalendarEvent):
with_reference_object=True, with_reference_object=True,
) )
return Documentation.get_documentations_for_events(start, end, events, incomplete, request) return Documentation.get_documentations_for_events(start, end, events, incomplete)
@classmethod @classmethod
def parse_dummy( def parse_dummy(
......
...@@ -4,7 +4,6 @@ from django.db.models import BooleanField, ExpressionWrapper, Q ...@@ -4,7 +4,6 @@ from django.db.models import BooleanField, ExpressionWrapper, Q
import graphene import graphene
import graphene_django_optimizer import graphene_django_optimizer
from guardian.shortcuts import get_objects_for_user
from aleksis.apps.chronos.models import LessonEvent from aleksis.apps.chronos.models import LessonEvent
from aleksis.apps.cursus.models import Course from aleksis.apps.cursus.models import Course
...@@ -54,7 +53,6 @@ from .statistics import StatisticsByPersonType ...@@ -54,7 +53,6 @@ from .statistics import StatisticsByPersonType
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
documentations = FilterOrderList(DocumentationType)
documentations_by_course_id = FilterOrderList( documentations_by_course_id = FilterOrderList(
DocumentationType, course_id=graphene.ID(required=True) DocumentationType, course_id=graphene.ID(required=True)
) )
...@@ -101,19 +99,6 @@ class Query(graphene.ObjectType): ...@@ -101,19 +99,6 @@ class Query(graphene.ObjectType):
group=graphene.ID(required=True), group=graphene.ID(required=True),
) )
@staticmethod
def resolve_documentations(root, info, **kwargs):
qs = filter_active_school_term(
info.context,
Documentation.objects.all(),
school_term_field="course__groups__school_term",
)
if not info.context.user.has_perm("alsijil.view_documentation_rule"):
return graphene_django_optimizer.query(
get_objects_for_user(info.context.user, "alsijil.view_documetation_rule", qs), info
)
return graphene_django_optimizer.query(qs, info)
def resolve_documentations_by_course_id(root, info, course_id, **kwargs): def resolve_documentations_by_course_id(root, info, course_id, **kwargs):
documentations = Documentation.objects.filter( documentations = Documentation.objects.filter(
pk__in=Documentation.objects.filter(course_id=course_id) pk__in=Documentation.objects.filter(course_id=course_id)
...@@ -173,23 +158,15 @@ class Query(graphene.ObjectType): ...@@ -173,23 +158,15 @@ class Query(graphene.ObjectType):
) )
school_term = get_active_school_term(info.context) school_term = get_active_school_term(info.context)
q = Q( date_start = date_start if date_start > school_term.date_start else school_term.date_start
pk__in=LessonEvent.objects.filter(groups__school_term=school_term) date_end = date_end if date_end < school_term.date_end else school_term.date_end
.values_list("id", flat=True)
.union(
LessonEvent.objects.filter(course__groups__school_term=school_term).values_list(
"id", flat=True
)
)
)
qs = LessonEvent.objects.filter(q)
events = LessonEvent.get_single_events( events = LessonEvent.get_single_events(
datetime.combine(date_start, datetime.min.time()), datetime.combine(date_start, datetime.min.time()),
datetime.combine(date_end, datetime.max.time()), datetime.combine(date_end, datetime.max.time()),
info.context, info.context,
event_params, event_params,
with_reference_object=True, with_reference_object=True,
queryset=qs,
) )
# Lookup or create documentations and return them all. # Lookup or create documentations and return them all.
...@@ -199,7 +176,6 @@ class Query(graphene.ObjectType): ...@@ -199,7 +176,6 @@ class Query(graphene.ObjectType):
events, events,
incomplete, incomplete,
absences_exist, absences_exist,
info.context,
) )
return docs + dummies return docs + dummies
...@@ -294,7 +270,6 @@ class Query(graphene.ObjectType): ...@@ -294,7 +270,6 @@ class Query(graphene.ObjectType):
person, person,
start, start,
end, end,
info.context,
) )
lessons_for_person.append(LessonsForPersonType(id=person, lessons=docs + dummies)) lessons_for_person.append(LessonsForPersonType(id=person, lessons=docs + dummies))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment