Skip to content
Snippets Groups Projects
Verified Commit 5b17b9a9 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Optimize operations/queries in full register printout view to make it faster

parent b2e79686
No related branches found
No related tags found
1 merge request!285Resolve "Full register printout doesn't load in time"
Pipeline #73434 passed
...@@ -21,6 +21,7 @@ Fixed ...@@ -21,6 +21,7 @@ Fixed
~~~~~ ~~~~~
* The week overview page was not refreshed when a new week was selected in the dropdown. * The week overview page was not refreshed when a new week was selected in the dropdown.
* Make generation of full register printout faster.
`2.0.1`_ - 2022-02-12 `2.0.1`_ - 2022-02-12
--------------------- ---------------------
......
...@@ -118,6 +118,14 @@ class RegisterObjectRelatedMixin(WeekRelatedMixin): ...@@ -118,6 +118,14 @@ class RegisterObjectRelatedMixin(WeekRelatedMixin):
else: else:
return self.extra_lesson return self.extra_lesson
@property
def register_object_key(self: Union["LessonDocumentation", "PersonalNote"]) -> str:
"""Get a unique reference to the related object related."""
if self.week and self.year:
return f"{self.register_object.pk}_{self.week}_{self.year}"
else:
return self.register_object.pk
@property @property
def calendar_week(self: Union["LessonDocumentation", "PersonalNote"]) -> CalendarWeek: def calendar_week(self: Union["LessonDocumentation", "PersonalNote"]) -> CalendarWeek:
"""Get the calendar week of this lesson documentation or personal note. """Get the calendar week of this lesson documentation or personal note.
......
...@@ -180,7 +180,7 @@ ...@@ -180,7 +180,7 @@
<div class="page-break">&nbsp;</div> <div class="page-break">&nbsp;</div>
{% if group.lessons.all %} {% if lessons %}
<h4>{% trans 'Teachers and lessons in group' %} {{ group.name }}</h4> <h4>{% trans 'Teachers and lessons in group' %} {{ group.name }}</h4>
<table id="lessons"> <table id="lessons">
...@@ -210,7 +210,7 @@ ...@@ -210,7 +210,7 @@
{% endif %} {% endif %}
{% if group.child_groups.all %} {% if child_groups %}
<h4>{% trans 'Teachers and lessons in child groups' %}</h4> <h4>{% trans 'Teachers and lessons in child groups' %}</h4>
<table id="lessons"> <table id="lessons">
...@@ -427,7 +427,7 @@ ...@@ -427,7 +427,7 @@
<tbody> <tbody>
{% for day in week %} {% for day in week %}
{% with register_objects_by_day|get_dict:day as register_objects %} {% with register_objects_by_day|get_dict:day as register_objects %}
{% for register_object, documentations, notes, substitution in register_objects %} {% for register_object, documentation, notes, substitution in register_objects %}
<tr class=" <tr class="
{% if substitution %} {% if substitution %}
{% if substitution.cancelled %} {% if substitution.cancelled %}
...@@ -461,47 +461,45 @@ ...@@ -461,47 +461,45 @@
</td> </td>
<td class="lesson-topic"> <td class="lesson-topic">
{% if register_object.label_ == "event" %} {% if register_object.label_ == "event" %}
{{ register_object.title }}: {{ documentations.0.topic }} {{ register_object.title }}: {{ documentation.topic }}
{% elif substitution.cancelled %} {% elif substitution.cancelled %}
{% trans 'Lesson cancelled' %} {% trans 'Lesson cancelled' %}
{% else %} {% else %}
{{ documentations.0.topic }} {{ documentation.topic }}
{% endif %} {% endif %}
</td> </td>
<td class="lesson-homework">{{ documentations.0.homework }}</td> <td class="lesson-homework">{{ documentation.homework }}</td>
<td class="lesson-notes"> <td class="lesson-notes">
{{ documentations.0.group_note }} {{ documentation.group_note }}
{% for note in notes %} {% for note in notes %}
{% if group in note.groups_of_person.all %} {% if note.absent %}
{% if note.absent %} <span class="lesson-note-absent">
<span class="lesson-note-absent"> {{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}.
{{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}. {% if note.excused %}
{% if note.excused %} <span class="lesson-note-excused">
<span class="lesson-note-excused"> {% if note.excuse_type %}
{% if note.excuse_type %} ({{ note.excuse_type.short_name }})
({{ note.excuse_type.short_name }}) {% else %}
{% else %} ({% trans 'e' %})
({% trans 'e' %}) {% endif %}
{% endif %} </span>
</span> {% endif %}
{% endif %} </span>
</span> {% endif %}
{% endif %} {% if note.late %}
{% if note.late %} <span class="lesson-note-late">
<span class="lesson-note-late"> {{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}.
{{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}. ({{ note.late }}′)
({{ note.late }}′) {% if note.excused %}
{% if note.excused %} <span class="lesson-note-excused">
<span class="lesson-note-excused"> {% if note.excuse_type %}
{% if note.excuse_type %} ({{ note.excuse_type.short_name }})
({{ note.excuse_type.short_name }}) {% else %}
{% else %} ({% trans 'e' %})
({% trans 'e' %}) {% endif %}
{% endif %} </span>
</span> {% endif %}
{% endif %} </span>
</span>
{% endif %}
{% endif %} {% endif %}
{% for extra_mark in note.extra_marks.all %} {% for extra_mark in note.extra_marks.all %}
<span> <span>
...@@ -512,7 +510,7 @@ ...@@ -512,7 +510,7 @@
{% endfor %} {% endfor %}
</td> </td>
<td class="lesson-te"> <td class="lesson-te">
{% if documentations.0.topic %} {% if documentation.topic %}
{{ register_object.get_teachers.first.short_name }} {{ register_object.get_teachers.first.short_name }}
{% endif %} {% endif %}
</td> </td>
......
...@@ -622,16 +622,27 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -622,16 +622,27 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
| Q(event__groups__parent_groups=group) | Q(event__groups__parent_groups=group)
) )
personal_notes = ( personal_notes = (
PersonalNote.objects.select_related("lesson_period") PersonalNote.objects.prefetch_related(
.prefetch_related( "lesson_period__substitutions", "lesson_period__lesson__teachers"
"lesson_period__substitutions", "lesson_period__lesson__teachers", "groups_of_person"
) )
.not_empty() .not_empty()
.filter(groups_q) .filter(groups_q)
.filter(groups_of_person=group)
) )
documentations = ( documentations = LessonDocumentation.objects.not_empty().filter(groups_q)
LessonDocumentation.objects.select_related("lesson_period").not_empty().filter(groups_q)
) sorted_documentations = {"extra_lesson": {}, "event": {}, "lesson_period": {}}
sorted_personal_notes = {"extra_lesson": {}, "event": {}, "lesson_period": {}, "person": {}}
for documentation in documentations:
key = documentation.register_object.label_
sorted_documentations[key][documentation.register_object_key] = documentation
for note in personal_notes:
key = note.register_object.label_
sorted_personal_notes[key].setdefault(note.register_object_key, [])
sorted_personal_notes[key][note.register_object_key].append(note)
sorted_personal_notes["person"].setdefault(note.person.pk, [])
sorted_personal_notes["person"][note.person.pk].append(note)
# Get all lesson periods for the selected group # Get all lesson periods for the selected group
lesson_periods = LessonPeriod.objects.filter_group(group).distinct() lesson_periods = LessonPeriod.objects.filter_group(group).distinct()
...@@ -645,8 +656,8 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -645,8 +656,8 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
register_objects_by_day.setdefault(day, []).append( register_objects_by_day.setdefault(day, []).append(
( (
extra_lesson, extra_lesson,
list(filter(lambda d: d.extra_lesson == extra_lesson, documentations)), sorted_documentations["extra_lesson"].get(extra_lesson.pk),
list(filter(lambda d: d.extra_lesson == extra_lesson, personal_notes)), sorted_personal_notes["extra_lesson"].get(extra_lesson.pk, []),
None, None,
) )
) )
...@@ -660,8 +671,8 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -660,8 +671,8 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
register_objects_by_day.setdefault(day, []).append( register_objects_by_day.setdefault(day, []).append(
( (
event_copy, event_copy,
list(filter(lambda d: d.event == event, documentations)), sorted_documentations["event"].get(event.pk),
list(filter(lambda d: d.event == event, personal_notes)), sorted_personal_notes["event"].get(event.pk, []),
None, None,
) )
) )
...@@ -680,26 +691,17 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -680,26 +691,17 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
<= day <= day
<= lesson_period.lesson.validity.date_end <= lesson_period.lesson.validity.date_end
): ):
filtered_documentations = list( filtered_documentation = sorted_documentations["lesson_period"].get(
filter( f"{lesson_period.pk}_{week.week}_{week.year}"
lambda d: d.week == week.week
and d.year == week.year
and d.lesson_period == lesson_period,
documentations,
)
) )
filtered_personal_notes = list( filtered_personal_notes = sorted_personal_notes["lesson_period"].get(
filter( f"{lesson_period.pk}_{week.week}_{week.year}", []
lambda d: d.week == week.week
and d.year == week.year
and d.lesson_period == lesson_period,
personal_notes,
)
) )
substitution = lesson_period.get_substitution(week) substitution = lesson_period.get_substitution(week)
register_objects_by_day.setdefault(day, []).append( register_objects_by_day.setdefault(day, []).append(
(lesson_period, filtered_documentations, filtered_personal_notes, substitution) (lesson_period, filtered_documentation, filtered_personal_notes, substitution)
) )
persons = group.members.prefetch_related(None).select_related(None) persons = group.members.prefetch_related(None).select_related(None)
...@@ -707,7 +709,7 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -707,7 +709,7 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
prefetched_persons = [] prefetched_persons = []
for person in persons: for person in persons:
person.filtered_notes = list(filter(lambda d: d.person == person, personal_notes)) person.filtered_notes = sorted_personal_notes["person"][person.pk]
prefetched_persons.append(person) prefetched_persons.append(person)
context["school_term"] = group.school_term context["school_term"] = group.school_term
...@@ -722,15 +724,22 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -722,15 +724,22 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
context["today"] = date.today() context["today"] = date.today()
context["lessons"] = ( context["lessons"] = (
group.lessons.all() group.lessons.all()
.select_related(None)
.prefetch_related(None)
.select_related("validity", "subject") .select_related("validity", "subject")
.prefetch_related("teachers", "lesson_periods") .prefetch_related("teachers", "lesson_periods")
) )
context["child_groups"] = group.child_groups.all().prefetch_related( context["child_groups"] = (
"lessons", group.child_groups.all()
"lessons__validity", .select_related(None)
"lessons__subject", .prefetch_related(None)
"lessons__teachers", .prefetch_related(
"lessons__lesson_periods", "lessons",
"lessons__validity",
"lessons__subject",
"lessons__teachers",
"lessons__lesson_periods",
)
) )
return render_pdf(request, "alsijil/print/full_register.html", context) return render_pdf(request, "alsijil/print/full_register.html", context)
......
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