From 6995c47b7068b314ca5173a254aee0ba9fe90ab1 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sat, 27 May 2023 12:53:00 +0200
Subject: [PATCH] Add more detailed properties to LessonEvent

---
 aleksis/apps/chronos/models.py | 47 +++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py
index 9b486d9b..8035ad4d 100644
--- a/aleksis/apps/chronos/models.py
+++ b/aleksis/apps/chronos/models.py
@@ -2,6 +2,7 @@
 from __future__ import annotations
 
 import os
+import itertools
 from datetime import date, datetime, time, timedelta
 from itertools import chain
 from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
@@ -1367,7 +1368,7 @@ class ChronosGlobalPermissions(GlobalPermissionModel):
 
 class LessonEvent(CalendarEvent):
     name = "lesson"
-    verbose_name = _("Lesson")
+    verbose_name = _("Lessons")
 
     title = models.CharField(verbose_name=_("Name"), max_length=255, blank=True)
 
@@ -1402,8 +1403,24 @@ class LessonEvent(CalendarEvent):
         null=True,
     )
 
+    @property
+    def all_members(self: "LessonEvent") -> list[Person]:
+        return list(itertools.chain(*[list(g.members.all()) for g in self.groups.all()]))
+
+    @property
+    def group_names(self: "LessonEvent") -> str:
+        return ", ".join([g.name for g in self.groups.all()])
+
+    @property
+    def teacher_names(self: "LessonEvent") -> str:
+        return ", ".join([t.full_name    for t in self.teachers.all()])
+
+    @property
+    def room_names(self: "LessonEvent") -> str:
+        return ", ".join([r.name for r in self.rooms.all()])
+
     @classmethod
-    def value_title(cls, reference_object: "CalendarEvent") -> str:
+    def value_title(cls, reference_object: "LessonEvent") -> str:
         """Get the title of the event."""
         if reference_object.title:
             return reference_object.title
@@ -1411,17 +1428,17 @@ class LessonEvent(CalendarEvent):
             return (
                 reference_object.subject.name
                 + " · "
-                + ", ".join([g.name for g in reference_object.groups.all()])
+                + reference_object.group_names
             )
 
         return _("Lesson")
 
     @classmethod
-    def value_description(cls, reference_object: "CalendarEvent") -> str:
-        return ""
+    def value_description(cls, reference_object: "LessonEvent") -> str:
+        return render_to_string("chronos/lesson_event_description.txt", {"event": reference_object})
 
     @classmethod
-    def value_color(cls, reference_object: "CalendarEvent") -> str:
+    def value_color(cls, reference_object: "LessonEvent") -> str:
         """Get the color of the event."""
         return (
             reference_object.subject.colour_bg
@@ -1429,6 +1446,24 @@ class LessonEvent(CalendarEvent):
             else super().value_color(reference_object)
         )
 
+    @classmethod
+    def value_organizer(cls, reference_object: "LessonEvent") -> str:
+        """Get the organizer of the event."""
+        # TODO: Do not use the teachers as organizer, because only one organizer is allowed
+        return [t.get_vcal_address(role="CHAIR") for t in reference_object.teachers.all()]
+
+    @classmethod
+    def value_attendee(cls, reference_object: "LessonEvent") -> str:
+        """Get the attendees of the event."""
+        # FIXME: Permissions
+        attendees = [t.get_vcal_address(role="CHAIR") for t in reference_object.teachers.all()] + [g.get_vcal_address(role="REQ-PARTICIPANT") for g in reference_object.all_members]
+        return [a for a in attendees if a]
+
+    @classmethod
+    def value_location(cls, reference_object: "LessonEvent") -> str:
+        """Get the location of the event."""
+        return ", ".join([r.name for r in reference_object.rooms.all()])
+
     @classmethod
     def get_objects(cls, request) -> Iterable:
         """Return all objects that should be included in the calendar."""
-- 
GitLab