From d06b731482b048d4831914b7e47d14f33ec5b464 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sun, 27 Oct 2019 15:23:54 +0100
Subject: [PATCH] Move current_lesson_periods() to queryset. Advances #33, #34.

---
 biscuit/apps/chronos/models.py |  9 +++++++++
 biscuit/apps/chronos/util.py   | 11 -----------
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/biscuit/apps/chronos/models.py b/biscuit/apps/chronos/models.py
index f6861015..7654c18d 100644
--- a/biscuit/apps/chronos/models.py
+++ b/biscuit/apps/chronos/models.py
@@ -50,6 +50,15 @@ class LessonPeriodQuerySet(models.QuerySet):
             select={'_week': week.week}
         )
 
+    def at_time(self, when: Optional[datetime] = None):
+        now = when or datetime.now()
+
+        return self.filter(lesson__date_start__lte=now.date(),
+                           lesson__date_end__gte=now.date(),
+                           period__weekday=now.isoweekday(),
+                           period__time_start__lte=now.time(),
+                           period__time_end__gte=now.time())
+
     def filter_group(self, group: Union[Group, int]):
         return self.filter(
                 Q(lesson__groups=group) | Q(lesson__groups__parent_groups=group))
diff --git a/biscuit/apps/chronos/util.py b/biscuit/apps/chronos/util.py
index da22485d..f79bdf27 100644
--- a/biscuit/apps/chronos/util.py
+++ b/biscuit/apps/chronos/util.py
@@ -91,17 +91,6 @@ class CalendarWeek:
         return self.__class__.from_date(self[0] - timedelta(days=weeks * 7))
 
 
-def current_lesson_periods(when: Optional[datetime] = None) -> models.query.QuerySet:
-    now = when or datetime.now()
-
-    LessonPeriod = apps.get_model('chronos.LessonPeriod')
-    return LessonPeriod.objects.filter(lesson__date_start__lte=now.date(),
-                                       lesson__date_end__gte=now.date(),
-                                       period__weekday=now.isoweekday(),
-                                       period__time_start__lte=now.time(),
-                                       period__time_end__gte=now.time())
-
-
 def week_weekday_from_date(when: date) -> Tuple[CalendarWeek, int]:
     return (CalendarWeek.from_date(when), when.isoweekday())
 
-- 
GitLab