diff --git a/program/models.py b/program/models.py index c5aab0863b0026e722527619ed49a34d456719ce..c491fa6c3d6247d7bddf1e4740bd48ce0ee0fac5 100644 --- a/program/models.py +++ b/program/models.py @@ -26,6 +26,7 @@ from versatileimagefield.fields import PPOIField, VersatileImageField from django.contrib.auth.models import User from django.db import models from django.db.models import Q, QuerySet +from django.utils import timezone from django.utils.translation import gettext_lazy as _ from program.utils import parse_datetime from steering.settings import THUMBNAIL_SIZES @@ -248,6 +249,20 @@ class Show(models.Model): def __str__(self): return self.name + def save(self, *args, **kwargs): + now = timezone.datetime.now() + today = now.date() + + if self.pk and self.is_active is False: + # deactivating a show means: + # - **delete all* the timeslots that belong to a schedule of this show the after now + # - **update all** the schedules of this show have today as `last_date` + TimeSlot.objects.filter(schedule__show=self, start__gt=now).delete() + self.schedules.filter(Q(last_date__gt=today) | Q(last_date=None)).update( + last_date=today + ) + super().save(*args, **kwargs) + class ShowLink(Link): show = models.ForeignKey(Show, on_delete=models.CASCADE, related_name="links")