From 1229e2eceeb6a439c6636ca3e8631dac2b8a975b Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Wed, 22 Jul 2020 19:47:32 +0200
Subject: [PATCH] Show only personal notes of (historical) group members

Archive groups of a person for every personal note to check against it
to decide what data should be displayed in full register print out
---
 .../migrations/0003_groups_of_person.py       | 33 +++++++++++++++++++
 aleksis/apps/alsijil/models.py                |  1 +
 .../alsijil/print/full_register.html          |  2 +-
 aleksis/apps/alsijil/views.py                 |  5 ++-
 4 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 aleksis/apps/alsijil/migrations/0003_groups_of_person.py

diff --git a/aleksis/apps/alsijil/migrations/0003_groups_of_person.py b/aleksis/apps/alsijil/migrations/0003_groups_of_person.py
new file mode 100644
index 000000000..c645b663a
--- /dev/null
+++ b/aleksis/apps/alsijil/migrations/0003_groups_of_person.py
@@ -0,0 +1,33 @@
+# Generated by Django 3.0.8 on 2020-07-22 17:29
+
+from django.db import migrations, models
+
+
+def add_groups(apps, schema_editor):
+    PersonalNote = apps.get_model("alsijil", "PersonalNote")
+
+    db_alias = schema_editor.connection.alias
+
+    for personal_note in PersonalNote.objects.using(db_alias).all():
+        groups = list(personal_note.person.member_of.using(db_alias).all())
+        personal_note.groups_of_person.set(groups)
+        personal_note.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("core", "0003_drop_image_cropping"),
+        ("alsijil", "0002_excuse_type"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="personalnote",
+            name="groups_of_person",
+            field=models.ManyToManyField(
+                related_name="_personalnote_groups_of_person_+", to="core.Group"
+            ),
+        ),
+        migrations.RunPython(add_groups),
+    ]
diff --git a/aleksis/apps/alsijil/models.py b/aleksis/apps/alsijil/models.py
index d10243b0d..ea7355d0c 100644
--- a/aleksis/apps/alsijil/models.py
+++ b/aleksis/apps/alsijil/models.py
@@ -42,6 +42,7 @@ class PersonalNote(ExtensibleModel):
     person = models.ForeignKey(
         "core.Person", models.CASCADE, related_name="personal_notes"
     )
+    groups_of_person = models.ManyToManyField("core.Group", related_name="+")
 
     week = models.IntegerField()
     lesson_period = models.ForeignKey(
diff --git a/aleksis/apps/alsijil/templates/alsijil/print/full_register.html b/aleksis/apps/alsijil/templates/alsijil/print/full_register.html
index 5b14cc7ad..5e7d92449 100644
--- a/aleksis/apps/alsijil/templates/alsijil/print/full_register.html
+++ b/aleksis/apps/alsijil/templates/alsijil/print/full_register.html
@@ -397,7 +397,7 @@
               <td class="lesson-homework">{{ documentations.0.homework }}</td>
               <td class="lesson-notes">
                 {% for note in notes %}
-                  {% if note.person in group.members.all %}
+                  {% if group in note.groups_of_person.all %}
                     {% if note.absent %}
                       <span class="lesson-note-absent">
                         {{ note.person.last_name }}, {{ note.person.first_name|slice:"0:1" }}.
diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py
index efad0b58c..01fa397b2 100644
--- a/aleksis/apps/alsijil/views.py
+++ b/aleksis/apps/alsijil/views.py
@@ -334,7 +334,10 @@ def full_register_group(request: HttpRequest, id_: int) -> HttpResponse:
                     (lesson_period, documentations, notes, substitution)
                 )
 
-    persons = group.members.annotate(
+    persons = Person.objects.filter(
+        personal_notes__groups_of_person=group,
+        personal_notes__lesson_period__lesson__validity__school_term=current_school_term,
+    ).annotate(
         absences_count=Count(
             "personal_notes__absent", filter=Q(personal_notes__absent=True)
         ),
-- 
GitLab