From 2c09920de31cf7f341bf3d1a9d47afb63f6a0e42 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sun, 14 Feb 2021 17:19:45 +0100
Subject: [PATCH] Support events and extra lessons in full register printout

---
 .../alsijil/print/full_register.html          | 50 +++++++++++-----
 aleksis/apps/alsijil/views.py                 | 59 +++++++++++++++++--
 2 files changed, 90 insertions(+), 19 deletions(-)

diff --git a/aleksis/apps/alsijil/templates/alsijil/print/full_register.html b/aleksis/apps/alsijil/templates/alsijil/print/full_register.html
index 85ac0fbca..4458204ed 100644
--- a/aleksis/apps/alsijil/templates/alsijil/print/full_register.html
+++ b/aleksis/apps/alsijil/templates/alsijil/print/full_register.html
@@ -317,14 +317,26 @@
 
       <tbody>
       {% for note in person.personal_notes.all %}
-        {% if note.lesson_period in lesson_periods %}
+        {% if note.register_object in register_objects %}
           {% if note.absent or note.late or note.remarks or note.extra_marks.all %}
-              {% weekday_to_date note.calendar_week note.lesson_period.period.weekday as note_date %}
             <tr>
-              <td>{{ note_date }}</td>
-              <td>{{ note.lesson_period.period.period }}</td>
-              <td>{{ note.lesson_period.get_subject.short_name }} </td>
-              <td>{{ note.lesson_period.get_teachers.first.short_name }}</td>
+              {% if note.date %}
+                <td>{{ note.date }}</td>
+                <td>{{ note.register_object.period.period }}</td>
+              {% else %}
+                <td colspan="2">
+                  {{ note.register_object.date_start }} {{ note.register_object.period_from.period }}.–{{ note.register_object.date_end }}
+                  {{ note.register_object.period_to.period }}.
+                </td>
+              {% endif %}
+              <td>
+                {% if note.register_object.label_ != "event" %}
+                  {{ note.register_object.get_subject.short_name }}
+                {% else %}
+                  {% trans "Event" %}
+                {% endif %}
+              </td>
+              <td>{{ note.register_object.teacher_short_names }}</td>
               <td>
                 {% if note.absent %}
                   {% trans 'Yes' %}
@@ -376,8 +388,8 @@
       </thead>
       <tbody>
       {% for day in week %}
-        {% with periods_by_day|get_dict:day as periods %}
-          {% for period, documentations, notes, substitution in periods %}
+        {% with register_objects_by_day|get_dict:day as register_objects %}
+          {% for register_object, documentations, notes, substitution in register_objects %}
             <tr class="
                     {% if substitution %}
                       {% if substitution.cancelled %}
@@ -391,18 +403,28 @@
                     {% endif %}
                   ">
               {% if forloop.first %}
-                <th rowspan="{{ periods|length }}" class="lessons-day-head">{{ day }}</th>
+                <th rowspan="{{ register_objects|length }}" class="lessons-day-head">{{ day }}</th>
               {% endif %}
-              <td class="lesson-pe">{{ period.period.period }}</td>
+              <td class="lesson-pe">
+                {% if register_object.label_ != "event" %}
+                  {{ register_object.period.period }}
+                {% else %}
+                  {{ register_object.period_from_on_day }}.–{{ register_object.period_to_on_day }}.
+                {% endif %}
+              </td>
               <td class="lesson-subj">
-                {% if substitution %}
+                {% if register_object.label_ == "event" %}
+                  <strong>{% trans "Event" %}</strong>
+                  {% elif substitution %}
                   {% include "chronos/partials/subs/subject.html" with type="substitution" el=substitution %}
                 {% else %}
-                  {% include "chronos/partials/subject.html" with subject=period.lesson.subject %}
+                  {% include "chronos/partials/subject.html" with subject=register_object.get_subject %}
                 {% endif %}
               </td>
               <td class="lesson-topic">
-                {% if substitution.cancelled %}
+                {% if register_object.label_ == "event" %}
+                  {{ register_object.title }}: {{ documentations.0.topic }}
+                {% elif substitution.cancelled %}
                   {% trans 'Lesson cancelled' %}
                 {% else %}
                   {{ documentations.0.topic }}
@@ -453,7 +475,7 @@
               </td>
               <td class="lesson-te">
                 {% if documentations.0.topic %}
-                  {{ substitution.teachers.first.short_name|default:period.lesson.teachers.first.short_name }}
+                  {{ substitution.teachers.first.short_name|default:register_object.get_teachers.first.short_name }}
                 {% endif %}
               </td>
             </tr>
diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py
index 447025ea8..7442f4a2e 100644
--- a/aleksis/apps/alsijil/views.py
+++ b/aleksis/apps/alsijil/views.py
@@ -493,10 +493,59 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
             "personal_notes__groups_of_person",
         )
     )
+    events = (
+        Event.objects.filter_group(group)
+        .distinct()
+        .prefetch_related(
+            "documentations",
+            "personal_notes",
+            "personal_notes__excuse_type",
+            "personal_notes__extra_marks",
+            "personal_notes__person",
+            "personal_notes__groups_of_person",
+        )
+    )
+    extra_lessons = (
+        ExtraLesson.objects.filter_group(group)
+        .distinct()
+        .prefetch_related(
+            "documentations",
+            "personal_notes",
+            "personal_notes__excuse_type",
+            "personal_notes__extra_marks",
+            "personal_notes__person",
+            "personal_notes__groups_of_person",
+        )
+    )
+    weeks = CalendarWeek.weeks_within(group.school_term.date_start, group.school_term.date_end)
+
+    register_objects_by_day = {}
+    for extra_lesson in extra_lessons:
+        day = extra_lesson.date
+        register_objects_by_day.setdefault(day, []).append(
+            (
+                extra_lesson,
+                list(extra_lesson.documentations.all()),
+                list(extra_lesson.personal_notes.all()),
+                None,
+            )
+        )
 
-    weeks = CalendarWeek.weeks_within(group.school_term.date_start, group.school_term.date_end,)
+    for event in events:
+        day_number = (event.date_end - event.date_start).days + 1
+        for i in range(day_number):
+            day = event.date_start + timedelta(days=i)
+            event_copy = deepcopy(event)
+            event_copy.annotate_day(day)
+            register_objects_by_day.setdefault(day, []).append(
+                (
+                    event_copy,
+                    list(event_copy.documentations.all()),
+                    list(event_copy.personal_notes.all()),
+                    None,
+                )
+            )
 
-    periods_by_day = {}
     for lesson_period in lesson_periods:
         for week in weeks:
             day = week[lesson_period.period.weekday]
@@ -520,7 +569,7 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
                 )
                 substitution = lesson_period.get_substitution(week)
 
-                periods_by_day.setdefault(day, []).append(
+                register_objects_by_day.setdefault(day, []).append(
                     (lesson_period, documentations, notes, substitution)
                 )
 
@@ -543,8 +592,8 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
     context["extra_marks"] = ExtraMark.objects.all()
     context["group"] = group
     context["weeks"] = weeks
-    context["periods_by_day"] = periods_by_day
-    context["lesson_periods"] = lesson_periods
+    context["register_objects_by_day"] = register_objects_by_day
+    context["register_objects"] = list(lesson_periods) + list(events) + list(extra_lessons)
     context["today"] = date.today()
     context["lessons"] = (
         group.lessons.all()
-- 
GitLab