2020-01-30 14:47:36 +01:00
|
|
|
from django.db import models
|
2024-05-05 22:44:30 +02:00
|
|
|
from django.utils.timezone import now
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2020-01-30 14:47:36 +01:00
|
|
|
|
|
|
|
class Schedule(models.Model):
|
2022-07-17 22:27:57 +02:00
|
|
|
starts_at = models.DateTimeField(db_column="starts")
|
|
|
|
ends_at = models.DateTimeField(db_column="ends")
|
|
|
|
|
|
|
|
instance = models.ForeignKey(
|
|
|
|
"schedule.ShowInstance",
|
|
|
|
on_delete=models.DO_NOTHING,
|
|
|
|
)
|
|
|
|
|
2022-06-22 13:05:43 +02:00
|
|
|
file = models.ForeignKey(
|
|
|
|
"storage.File",
|
|
|
|
on_delete=models.DO_NOTHING,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
|
|
|
stream = models.ForeignKey(
|
2022-06-27 17:25:59 +02:00
|
|
|
"schedule.Webstream",
|
2022-06-22 13:05:43 +02:00
|
|
|
on_delete=models.DO_NOTHING,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
2022-07-17 22:27:57 +02:00
|
|
|
|
|
|
|
length = models.DurationField(blank=True, null=True, db_column="clip_length")
|
2020-01-30 14:47:36 +01:00
|
|
|
fade_in = models.TimeField(blank=True, null=True)
|
|
|
|
fade_out = models.TimeField(blank=True, null=True)
|
|
|
|
cue_in = models.DurationField()
|
|
|
|
cue_out = models.DurationField()
|
2022-07-17 22:27:57 +02:00
|
|
|
|
|
|
|
class PositionStatus(models.IntegerChoices):
|
|
|
|
FILLER = -1, "Filler" # Used to fill a show that already started
|
|
|
|
OUTSIDE = 0, "Outside" # Is outside the show time frame
|
|
|
|
INSIDE = 1, "Inside" # Is inside the show time frame
|
|
|
|
BOUNDARY = 2, "Boundary" # Is at the boundary of the show time frame
|
|
|
|
|
2020-01-30 14:47:36 +01:00
|
|
|
position = models.IntegerField()
|
2022-07-17 22:27:57 +02:00
|
|
|
position_status = models.SmallIntegerField(
|
|
|
|
choices=PositionStatus.choices,
|
|
|
|
default=PositionStatus.INSIDE,
|
|
|
|
db_column="playout_status",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Broadcasted is set to 1 when a live source is not
|
|
|
|
# on. Used for the playout history.
|
|
|
|
broadcasted = models.SmallIntegerField()
|
|
|
|
played = models.BooleanField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
db_column="media_item_played",
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2022-07-19 18:34:56 +02:00
|
|
|
def overbooked(self) -> bool:
|
2022-07-17 22:27:57 +02:00
|
|
|
"""
|
|
|
|
A schedule item is overbooked if it starts after the end of the show
|
|
|
|
instance it is in.
|
|
|
|
|
|
|
|
Related to self.position_status
|
|
|
|
"""
|
|
|
|
return self.starts_at >= self.instance.ends_at
|
2020-01-30 14:47:36 +01:00
|
|
|
|
|
|
|
def get_owner(self):
|
|
|
|
return self.instance.get_owner()
|
|
|
|
|
2022-07-17 22:27:57 +02:00
|
|
|
def get_cue_out(self):
|
2021-08-10 16:30:02 +02:00
|
|
|
"""
|
2022-07-17 22:27:57 +02:00
|
|
|
Returns a scheduled item cue out that is based on the current show
|
|
|
|
instance.
|
2021-09-19 20:48:19 +02:00
|
|
|
|
2022-07-17 22:27:57 +02:00
|
|
|
Cue out of a specific item can potentially overrun the show that it is
|
|
|
|
scheduled in. In that case, the cue out should be the end of the show.
|
2021-09-19 20:48:19 +02:00
|
|
|
This prevents the next show having overlapping items playing.
|
|
|
|
|
|
|
|
Cases:
|
|
|
|
- When the schedule ends before the end of the show instance,
|
2022-07-17 22:27:57 +02:00
|
|
|
return the stored cue out.
|
2021-09-19 20:48:19 +02:00
|
|
|
|
|
|
|
- When the schedule starts before the end of the show instance
|
|
|
|
and ends after the show instance ends,
|
|
|
|
return timedelta between schedule starts and show instance ends.
|
|
|
|
|
|
|
|
- When the schedule starts after the end of the show instance,
|
|
|
|
return the stored cue_out even if the schedule WILL NOT BE PLAYED.
|
2021-08-10 16:30:02 +02:00
|
|
|
"""
|
2022-07-17 22:27:57 +02:00
|
|
|
if (
|
|
|
|
self.starts_at < self.instance.ends_at
|
|
|
|
and self.instance.ends_at < self.ends_at
|
|
|
|
):
|
|
|
|
return self.instance.ends_at - self.starts_at
|
2021-08-10 16:30:02 +02:00
|
|
|
return self.cue_out
|
|
|
|
|
2022-07-17 22:27:57 +02:00
|
|
|
def get_ends_at(self):
|
2021-08-10 16:30:02 +02:00
|
|
|
"""
|
2022-07-17 22:27:57 +02:00
|
|
|
Returns a scheduled item ends that is based on the current show
|
|
|
|
instance.
|
2021-09-19 20:48:19 +02:00
|
|
|
|
|
|
|
End of a specific item can potentially overrun the show that it is
|
|
|
|
scheduled in. In that case, the end should be the end of the show.
|
|
|
|
This prevents the next show having overlapping items playing.
|
|
|
|
|
|
|
|
Cases:
|
|
|
|
- When the schedule ends before the end of the show instance,
|
|
|
|
return the scheduled item ends.
|
|
|
|
|
|
|
|
- When the schedule starts before the end of the show instance
|
|
|
|
and ends after the show instance ends,
|
|
|
|
return the show instance ends.
|
|
|
|
|
|
|
|
- When the schedule starts after the end of the show instance,
|
|
|
|
return the show instance ends.
|
2021-08-10 16:30:02 +02:00
|
|
|
"""
|
2022-07-17 22:27:57 +02:00
|
|
|
if self.instance.ends_at < self.ends_at:
|
|
|
|
return self.instance.ends_at
|
|
|
|
return self.ends_at
|
2021-09-21 11:36:49 +02:00
|
|
|
|
2024-05-05 22:44:30 +02:00
|
|
|
@staticmethod
|
|
|
|
def is_file_scheduled_in_the_future(file_id):
|
|
|
|
count = Schedule.objects.filter(
|
|
|
|
file_id=file_id,
|
|
|
|
ends_at__gt=now(),
|
|
|
|
).count()
|
|
|
|
return count > 0
|
|
|
|
|
2020-01-30 14:47:36 +01:00
|
|
|
class Meta:
|
|
|
|
managed = False
|
2021-05-27 16:23:02 +02:00
|
|
|
db_table = "cc_schedule"
|
2020-01-30 14:47:36 +01:00
|
|
|
permissions = [
|
2021-05-27 16:23:02 +02:00
|
|
|
("change_own_schedule", "Change the content on their shows"),
|
|
|
|
("delete_own_schedule", "Delete the content on their shows"),
|
2020-01-30 14:47:36 +01:00
|
|
|
]
|