diff --git a/program/services.py b/program/services.py index e8744fabfdb755787c9796857ccc0ca976f515ed..480c4aef43b2d4d6a634169165aab59adcf8273d 100644 --- a/program/services.py +++ b/program/services.py @@ -45,7 +45,7 @@ class ScheduleData(TypedDict): end_time: str first_date: str id: int | None - is_repetition: bool + is_repetition: bool | None last_date: str | None rrule: int show: int | None @@ -97,11 +97,11 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s """ schedule = data["schedule"] - solutions = data.get("solutions", []) # only needed if conflicts exist + solutions = data.get("solutions", []) # only needed if conflicts exist new_schedule = instantiate_upcoming_schedule(schedule, show_pk, schedule_pk) - last_date_is_unknown = new_schedule.last_date is None # we need to keep track of this + last_date_is_unknown = new_schedule.last_date is None # we need to keep track of this # FIXME: refactor this to eliminate the duplication if last_date_is_unknown: @@ -369,7 +369,7 @@ def instantiate_upcoming_schedule( rrule = RRule.objects.get(pk=data["rrule"]) show = Show.objects.get(pk=show_pk) - is_repetition = data["is_repetition"] + is_repetition = data["is_repetition"] if "is_repetition" in data else False # default is `False` add_business_days_only = ( @@ -424,7 +424,11 @@ def make_conflicts(data: ScheduleData, schedule_pk: int | None, show_pk: int) -> if schedule_pk is not None: existing_schedule = Schedule.objects.get(pk=schedule_pk) - if new_schedule.last_date > existing_schedule.last_date: + if ( + new_schedule.last_date + and existing_schedule.last_date + and new_schedule.last_date > existing_schedule.last_date + ): last_timeslot = ( TimeSlot.objects.filter(schedule=existing_schedule).order_by("start").reverse()[0] ) @@ -453,11 +457,15 @@ def generate_timeslots(schedule: Schedule) -> list[TimeSlot]: and schedule.rrule.interval == 1 and schedule.rrule.by_weekdays is None ): # weekly: Use schedule.by_weekday for by_weekday - by_weekday_start = by_weekday_end = int(schedule.by_weekday) + by_weekday_start = by_weekday_end = ( + int(schedule.by_weekday) if schedule.by_weekday is not None else None + ) # adjust by_weekday_end if end_time is after midnight if schedule.end_time < schedule.start_time: - by_weekday_end = by_weekday_start + 1 if by_weekday_start < 6 else 0 + by_weekday_end = ( + by_weekday_start + 1 if by_weekday_start and by_weekday_start < 6 else 0 + ) elif ( schedule.rrule.freq == 2 and schedule.rrule.interval == 1 @@ -491,7 +499,9 @@ def generate_timeslots(schedule: Schedule) -> list[TimeSlot]: # adjust by_weekday_end if end_time is after midnight if schedule.end_time < schedule.start_time: - by_weekday_end = by_weekday_start + 1 if by_weekday_start < 6 else 0 + by_weekday_end = ( + by_weekday_start + 1 if by_weekday_start and by_weekday_start < 6 else 0 + ) # adjust last_date if end_time is after midnight if schedule.end_time < schedule.start_time: