From 238c8a545cfe90be2e125f16ad01a28c606f05e9 Mon Sep 17 00:00:00 2001
From: Dominik George <nik@naturalnet.de>
Date: Tue, 17 Sep 2019 13:48:34 +0200
Subject: [PATCH] Add absences to register printout. Advances #26.

---
 biscuit/apps/alsijil/models.py                |  2 +-
 .../static/css/alsijil/full_register.css      | 16 +++++++++++
 .../alsijil/print/full_register.html          | 26 +++++++++++++++++-
 biscuit/apps/alsijil/views.py                 | 27 ++-----------------
 4 files changed, 44 insertions(+), 27 deletions(-)

diff --git a/biscuit/apps/alsijil/models.py b/biscuit/apps/alsijil/models.py
index 04732c311..79f6f5df1 100644
--- a/biscuit/apps/alsijil/models.py
+++ b/biscuit/apps/alsijil/models.py
@@ -8,7 +8,7 @@ class PersonalNote(SchoolRelated):
     person = models.ForeignKey('core.Person', models.CASCADE, related_name='personal_notes')
 
     week = models.IntegerField()
-    lesson_period = models.ForeignKey('chronos.LessonPeriod', models.CASCADE)
+    lesson_period = models.ForeignKey('chronos.LessonPeriod', models.CASCADE, related_name='personal_notes')
 
     absent = models.BooleanField(default=False)
     late = models.IntegerField(default=0)
diff --git a/biscuit/apps/alsijil/static/css/alsijil/full_register.css b/biscuit/apps/alsijil/static/css/alsijil/full_register.css
index fc5ed9915..0d771fd94 100644
--- a/biscuit/apps/alsijil/static/css/alsijil/full_register.css
+++ b/biscuit/apps/alsijil/static/css/alsijil/full_register.css
@@ -135,3 +135,19 @@ tr.lesson-cancelled td {
 tr.lesson-substituted td.lesson-subj {
   text-decoration: line-through;
 }
+
+td.lesson-notes {
+  font-size: 80%;
+}
+
+td.lesson-notes span.lesson-note-absent {
+  color: #cc0000;
+}
+
+td.lesson-notes span.lesson-note-late {
+  color: #ff9933;
+}
+
+td.lesson-notes span.lesson-note-excused {
+  color: #009933;
+}
diff --git a/biscuit/apps/alsijil/templates/alsijil/print/full_register.html b/biscuit/apps/alsijil/templates/alsijil/print/full_register.html
index 8ad0fa082..a95cc646a 100644
--- a/biscuit/apps/alsijil/templates/alsijil/print/full_register.html
+++ b/biscuit/apps/alsijil/templates/alsijil/print/full_register.html
@@ -130,11 +130,12 @@
                   <th>{% trans 'Subs.' %}</th>
                   <th>{% trans 'Lesson topic' %}</th>
                   <th>{% trans 'Homework' %}</th>
+                  <th>{% trans 'Notes' %}</th>
                   <th>{% trans 'Te.' %}</th>
                 </tr>
               </thead>
               <tbody>
-                {% for period, documentations, substitution in periods_by_day|get_dict:day %}
+                {% for period, documentations, notes, substitution in periods_by_day|get_dict:day %}
                   <tr class="
                     {% if substitution %}
                       {% if substitution.cancelled %}
@@ -155,6 +156,29 @@
                       {% endif %}
                     </td>
                     <td class="lesson-homework">{{ documentations.0.homework }}</td>
+                    <td class="lesson-notes">
+                      {% for note in notes %}
+                        {% if note.absent %}
+                          <span class="lesson-note-absent">
+                            {{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}.
+                            {% if note.excused %}
+                              <span class="lesson-note-excused">
+                               ({% trans 'e' %})
+                              </span>
+                            {% endif %}
+                        {% endif %}
+                        {% if note.late %}
+                          <span class="lesson-note-late">
+                            {{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}.
+                            ({{ note.late }}′)
+                            {% if note.excused %}
+                              <span class="lesson-note-excused">
+                               ({% trans 'e' %})
+                              </span>
+                            {% endif %}
+                        {% endif %}
+                      {% endfor %}
+                    </td>
                     <td class="lesson-te">
                       {% if documentations.0.topic %}
                         {{ substitution.teachers.first.short_name|default:period.lesson.teachers.first.short_name }}
diff --git a/biscuit/apps/alsijil/views.py b/biscuit/apps/alsijil/views.py
index 2c980af96..619f6b4bf 100644
--- a/biscuit/apps/alsijil/views.py
+++ b/biscuit/apps/alsijil/views.py
@@ -198,28 +198,6 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
         Q(lesson__groups=group) | Q(lesson__groups__parent_groups=group)
     ).distinct()
 
-    # Aggregate all personal notes for this group and week
-    persons = Person.objects.filter(
-        is_active=True
-    ).filter(
-        Q(member_of=group) | Q(member_of__parent_groups=group)
-    ).distinct().prefetch_related(
-        'personal_notes'
-    ).annotate(
-        absences=Count('personal_notes__absent', filter=Q(
-            personal_notes__lesson_period__in=lesson_periods,
-            personal_notes__absent=True
-        )),
-        unexcused=Count('personal_notes__absent', filter=Q(
-            personal_notes__lesson_period__in=lesson_periods,
-            personal_notes__absent=True,
-            personal_notes__excused=False
-        )),
-        tardiness=Sum('personal_notes__late', filter=Q(
-            personal_notes__lesson_period__in=lesson_periods,
-        ))
-    )
-
     weeks = CalendarWeek.weeks_within(group.school.current_term.date_start, group.school.current_term.date_end)
     periods_by_day = {}
     for lesson_period in lesson_periods:
@@ -228,15 +206,14 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
 
             if lesson_period.lesson.date_start <= day and lesson_period.lesson.date_end >= day:
                 documentations = list(filter(lambda d: d.week == week.week, lesson_period.documentations.all()))
+                notes = list(filter(lambda d: d.week == week.week, lesson_period.personal_notes.all()))
                 substitution = lesson_period.get_substitution(week.week)
 
-                periods_by_day.setdefault(day, []).append((lesson_period, documentations, substitution))
+                periods_by_day.setdefault(day, []).append((lesson_period, documentations, notes, substitution))
 
     context['group'] = group
     context['weeks'] = weeks
-    context['lesson_periods'] = lesson_periods
     context['periods_by_day'] = periods_by_day
-    context['persons'] = persons
     context['today'] = date.today()
 
     return render(request, 'alsijil/print/full_register.html', context)
-- 
GitLab