]> git.0d.be Git - django-panik-emissions.git/commitdiff
fix support for midnight crossing and empty days
authorFrédéric Péters <fpeters@0d.be>
Wed, 4 Jul 2018 23:40:27 +0000 (01:40 +0200)
committerFrédéric Péters <fpeters@0d.be>
Wed, 4 Jul 2018 23:40:27 +0000 (01:40 +0200)
emissions/models.py
emissions/utils.py

index 8855cb890ba41e9baa5d865dc532b33d0dad8777..4bf7cf376a464c42cd3d1590ee3fbcd091f7b251 100644 (file)
@@ -33,7 +33,7 @@ LICENSES = (
 
 
 class WeekdayMixin(object):
-    DAY_HOUR_START = 6
+    DAY_HOUR_START = 5
 
     def get_weekday(self):
         weekday = self.datetime.weekday() + 7
index 68655679d0fa38cd5d31566ed3c670738dde115c..e98694abedf0b4601ebea4f73bc97f79a8337d90 100644 (file)
@@ -91,6 +91,10 @@ def period_program(date_start, date_end, prefetch_sounds=True,
                     current_date.year, current_date.month, current_date.day,
                     schedule.datetime.hour, schedule.datetime.minute) + \
                             timedelta(days=schedule.datetime.weekday()-current_date.weekday())
+            if week_day == 6 and schedule.datetime.weekday() == 0:
+                # on Sundays we can have Sunday->Monday night programming, and
+                # we need to get them on the right date.
+                schedule.datetime += timedelta(days=7)
         program.extend(day_schedules)
         current_date += timedelta(days=1)
 
@@ -204,16 +208,14 @@ def period_program(date_start, date_end, prefetch_sounds=True,
         def get_serie(cls, start, end):
             cells = []
             dt = None
+            delta_day = 0
+            last_added_end = None
             for nonstop in nonstops:
                 nonstop_day_start = start.replace(hour=nonstop.start.hour, minute=nonstop.start.minute)
                 nonstop_day_end = start.replace(hour=nonstop.end.hour, minute=nonstop.end.minute)
-                if nonstop.end < nonstop.start:
-                    if start.time() < nonstop.end:
-                        nonstop_day_start -= timedelta(days=1)
-                    else:
-                        nonstop_day_end += timedelta(days=1)
-                elif nonstop.start < dawn:
-                    nonstop_day_start += timedelta(days=1)
+                nonstop_day_start += timedelta(days=delta_day)
+                nonstop_day_end += timedelta(days=delta_day)
+                if nonstop.start > nonstop.end:
                     nonstop_day_end += timedelta(days=1)
 
                 if nonstop_day_start < end and nonstop_day_end > start:
@@ -222,11 +224,20 @@ def period_program(date_start, date_end, prefetch_sounds=True,
                     else:
                         dt = nonstop_day_start
                     cells.append(cls(nonstop, dt))
+                    last_added_end = nonstop_day_end
+                    if nonstop.start > nonstop.end:
+                        # we just added a midnight crossing slot, future slots
+                        # should be one day later.
+                        delta_day += 1
                     if nonstop.start < dawn and nonstop.end > dawn and start.time() < dawn:
                         dt = dt.replace(hour=dawn.hour, minute=dawn.minute)
                         cells.append(cls(nonstop, dt))
 
             cells.sort(key=lambda x: x.datetime)
+            if last_added_end < end:
+                # we used all nonstop slots and we still did not reach the end;
+                # let's go for one more turn.
+                cells.extend(cls.get_serie(last_added_end, end))
             return cells