chore(api): rename schedule models fields

This commit is contained in:
jo 2022-07-17 22:27:57 +02:00 committed by Kyle Robbertze
parent 8ceb1419a0
commit 57046e2a9d
8 changed files with 224 additions and 334 deletions

View file

@ -2,8 +2,14 @@ from django.db import models
class Schedule(models.Model):
starts = models.DateTimeField()
ends = models.DateTimeField()
starts_at = models.DateTimeField(db_column="starts")
ends_at = models.DateTimeField(db_column="ends")
instance = models.ForeignKey(
"schedule.ShowInstance",
on_delete=models.DO_NOTHING,
)
file = models.ForeignKey(
"storage.File",
on_delete=models.DO_NOTHING,
@ -16,31 +22,60 @@ class Schedule(models.Model):
blank=True,
null=True,
)
clip_length = models.DurationField(blank=True, null=True)
length = models.DurationField(blank=True, null=True, db_column="clip_length")
fade_in = models.TimeField(blank=True, null=True)
fade_out = models.TimeField(blank=True, null=True)
cue_in = models.DurationField()
cue_out = models.DurationField()
media_item_played = models.BooleanField(blank=True, null=True)
instance = models.ForeignKey("schedule.ShowInstance", on_delete=models.DO_NOTHING)
playout_status = models.SmallIntegerField()
broadcasted = models.SmallIntegerField()
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
position = models.IntegerField()
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
def overbooked(self):
"""
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
def get_owner(self):
return self.instance.get_owner()
def get_cueout(self):
def get_cue_out(self):
"""
Returns a scheduled item cueout that is based on the current show instance.
Returns a scheduled item cue out that is based on the current show
instance.
Cueout of a specific item can potentially overrun the show that it is
scheduled in. In that case, the cueout should be the end of the show.
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.
This prevents the next show having overlapping items playing.
Cases:
- When the schedule ends before the end of the show instance,
return the stored cueout.
return the stored cue out.
- When the schedule starts before the end of the show instance
and ends after the show instance ends,
@ -49,13 +84,17 @@ class Schedule(models.Model):
- When the schedule starts after the end of the show instance,
return the stored cue_out even if the schedule WILL NOT BE PLAYED.
"""
if self.starts < self.instance.ends and self.instance.ends < self.ends:
return self.instance.ends - self.starts
if (
self.starts_at < self.instance.ends_at
and self.instance.ends_at < self.ends_at
):
return self.instance.ends_at - self.starts_at
return self.cue_out
def get_ends(self):
def get_ends_at(self):
"""
Returns a scheduled item ends that is based on the current show instance.
Returns a scheduled item ends that is based on the current show
instance.
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.
@ -72,17 +111,9 @@ class Schedule(models.Model):
- When the schedule starts after the end of the show instance,
return the show instance ends.
"""
if self.instance.ends < self.ends:
return self.instance.ends
return self.ends
@property
def is_valid(self):
"""
A schedule item is valid if it starts before the end of the show instance
it is in
"""
return self.starts < self.instance.ends
if self.instance.ends_at < self.ends_at:
return self.instance.ends_at
return self.ends_at
class Meta:
managed = False

View file

@ -7,29 +7,30 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer):
file_id = serializers.IntegerField(source="file.id", read_only=True)
stream_id = serializers.IntegerField(source="stream.id", read_only=True)
instance_id = serializers.IntegerField(source="instance.id", read_only=True)
cue_out = serializers.DurationField(source="get_cueout", read_only=True)
ends = serializers.DateTimeField(source="get_ends", read_only=True)
cue_out = serializers.DurationField(source="get_cue_out", read_only=True)
ends_at = serializers.DateTimeField(source="get_ends_at", read_only=True)
class Meta:
model = Schedule
fields = [
"item_url",
"id",
"starts",
"ends",
"starts_at",
"ends_at",
"instance",
"instance_id",
"file",
"file_id",
"stream",
"stream_id",
"clip_length",
"length",
"fade_in",
"fade_out",
"cue_in",
"cue_out",
"media_item_played",
"instance",
"instance_id",
"playout_status",
"broadcasted",
"position",
"position_status",
"broadcasted",
"played",
"overbooked",
]

View file

@ -9,49 +9,49 @@ class TestSchedule(TestCase):
@classmethod
def setUpTestData(cls):
cls.show_instance = ShowInstance(
created=datetime(year=2021, month=10, day=1, hour=12),
starts=datetime(year=2021, month=10, day=2, hour=1),
ends=datetime(year=2021, month=10, day=2, hour=2),
created_at=datetime(year=2021, month=10, day=1, hour=12),
starts_at=datetime(year=2021, month=10, day=2, hour=1),
ends_at=datetime(year=2021, month=10, day=2, hour=2),
)
cls.length = timedelta(minutes=10)
cls.cue_in = timedelta(seconds=1)
cls.cue_out = cls.length - timedelta(seconds=4)
def create_schedule(self, starts):
def create_schedule(self, starts_at):
return Schedule(
starts=starts,
ends=starts + self.length,
starts_at=starts_at,
ends_at=starts_at + self.length,
cue_in=self.cue_in,
cue_out=self.cue_out,
instance=self.show_instance,
)
def test_get_cueout(self):
def test_get_cue_out(self):
# No overlapping schedule datetimes, normal usecase:
item1_starts = datetime(year=2021, month=10, day=2, hour=1, minute=30)
item1 = self.create_schedule(item1_starts)
self.assertEqual(item1.get_cueout(), self.cue_out)
self.assertEqual(item1.get_ends(), item1_starts + self.length)
self.assertEqual(item1.get_cue_out(), self.cue_out)
self.assertEqual(item1.get_ends_at(), item1_starts + self.length)
# Mixed overlapping schedule datetimes (only ends is overlapping):
item_2_starts = datetime(year=2021, month=10, day=2, hour=1, minute=55)
item_2 = self.create_schedule(item_2_starts)
self.assertEqual(item_2.get_cueout(), timedelta(minutes=5))
self.assertEqual(item_2.get_ends(), self.show_instance.ends)
self.assertEqual(item_2.get_cue_out(), timedelta(minutes=5))
self.assertEqual(item_2.get_ends_at(), self.show_instance.ends_at)
# Fully overlapping schedule datetimes (starts and ends are overlapping):
item3_starts = datetime(year=2021, month=10, day=2, hour=2, minute=1)
item3 = self.create_schedule(item3_starts)
self.assertEqual(item3.get_cueout(), self.cue_out)
self.assertEqual(item3.get_ends(), self.show_instance.ends)
self.assertEqual(item3.get_cue_out(), self.cue_out)
self.assertEqual(item3.get_ends_at(), self.show_instance.ends_at)
def test_is_valid(self):
def test_overbooked(self):
# Starts before the schedule ends
item1_starts = datetime(year=2021, month=10, day=2, hour=1, minute=30)
item1 = self.create_schedule(item1_starts)
self.assertTrue(item1.is_valid)
self.assertFalse(item1.overbooked)
# Starts after the schedule ends
item_2_starts = datetime(year=2021, month=10, day=2, hour=3)
item_2 = self.create_schedule(item_2_starts)
self.assertFalse(item_2.is_valid)
self.assertTrue(item_2.overbooked)

View file

@ -25,13 +25,13 @@ class TestScheduleViewSet(APITestCase):
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(minutes=5),
starts_at=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends_at=datetime.now(tz=timezone.utc) + timedelta(minutes=5),
)
schedule_item = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + file.length,
starts_at=datetime.now(tz=timezone.utc),
ends_at=datetime.now(tz=timezone.utc) + file.length,
cue_out=file.cue_out,
instance=show,
file=file,
@ -41,7 +41,7 @@ class TestScheduleViewSet(APITestCase):
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(
dateparse.parse_datetime(result[0]["ends"]), schedule_item.ends
dateparse.parse_datetime(result[0]["ends_at"]), schedule_item.ends_at
)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), file.cue_out)
@ -56,13 +56,13 @@ class TestScheduleViewSet(APITestCase):
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(seconds=20),
starts_at=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends_at=datetime.now(tz=timezone.utc) + timedelta(seconds=20),
)
schedule_item = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + file.length,
starts_at=datetime.now(tz=timezone.utc),
ends_at=datetime.now(tz=timezone.utc) + file.length,
instance=show,
file=file,
)
@ -70,11 +70,11 @@ class TestScheduleViewSet(APITestCase):
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(dateparse.parse_datetime(result[0]["ends"]), show.ends)
expected = show.ends - schedule_item.starts
self.assertEqual(dateparse.parse_datetime(result[0]["ends_at"]), show.ends_at)
expected = show.ends_at - schedule_item.starts_at
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), expected)
self.assertNotEqual(
dateparse.parse_datetime(result[0]["ends"]), schedule_item.ends
dateparse.parse_datetime(result[0]["ends_at"]), schedule_item.ends_at
)
def test_schedule_item_invalid(self):
@ -88,33 +88,33 @@ class TestScheduleViewSet(APITestCase):
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(minutes=5),
starts_at=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends_at=datetime.now(tz=timezone.utc) + timedelta(minutes=5),
)
schedule_item = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + file.length,
starts_at=datetime.now(tz=timezone.utc),
ends_at=datetime.now(tz=timezone.utc) + file.length,
cue_out=file.cue_out,
instance=show,
file=file,
)
invalid_schedule_item = baker.make( # pylint: disable=unused-variable
"schedule.Schedule",
starts=show.ends + timedelta(minutes=1),
ends=show.ends + timedelta(minutes=1) + file.length,
starts_at=show.ends_at + timedelta(minutes=1),
ends_at=show.ends_at + timedelta(minutes=1) + file.length,
cue_out=file.cue_out,
instance=show,
file=file,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
response = self.client.get(self.path, {"is_valid": True})
response = self.client.get(self.path, {"overbooked": False})
self.assertEqual(response.status_code, 200)
result = response.json()
# The invalid item should be filtered out and not returned
self.assertEqual(len(result), 1)
self.assertEqual(
dateparse.parse_datetime(result[0]["ends"]), schedule_item.ends
dateparse.parse_datetime(result[0]["ends_at"]), schedule_item.ends_at
)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), file.cue_out)
@ -131,21 +131,21 @@ class TestScheduleViewSet(APITestCase):
show = baker.make(
"schedule.ShowInstance",
starts=filter_point - timedelta(minutes=5),
ends=filter_point + timedelta(minutes=5),
starts_at=filter_point - timedelta(minutes=5),
ends_at=filter_point + timedelta(minutes=5),
)
schedule_item = baker.make(
"schedule.Schedule",
starts=filter_point,
ends=filter_point + file.length,
starts_at=filter_point,
ends_at=filter_point + file.length,
cue_out=file.cue_out,
instance=show,
file=file,
)
previous_item = baker.make( # pylint: disable=unused-variable
"schedule.Schedule",
starts=filter_point - timedelta(minutes=5),
ends=filter_point - timedelta(minutes=5) + file.length,
starts_at=filter_point - timedelta(minutes=5),
ends_at=filter_point - timedelta(minutes=5) + file.length,
cue_out=file.cue_out,
instance=show,
file=file,
@ -156,12 +156,13 @@ class TestScheduleViewSet(APITestCase):
)
range_end = (filter_point + timedelta(minutes=1)).isoformat(timespec="seconds")
response = self.client.get(
self.path, {"starts__range": f"{range_start},{range_end}"}
self.path,
{"starts_after": range_start, "starts_before": range_end},
)
self.assertEqual(response.status_code, 200)
result = response.json()
# The previous_item should be filtered out and not returned
self.assertEqual(len(result), 1)
self.assertEqual(
dateparse.parse_datetime(result[0]["starts"]), schedule_item.starts
dateparse.parse_datetime(result[0]["starts_at"]), schedule_item.starts_at
)

View file

@ -1,42 +1,33 @@
from django.db.models import F
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
from django.db import models
from django_filters import rest_framework as filters
from rest_framework import viewsets
from ..._constants import FILTER_NUMERICAL_LOOKUPS
from ..models import Schedule
from ..serializers import ScheduleSerializer
@extend_schema_view(
list=extend_schema(
parameters=[
OpenApiParameter(
name="is_valid",
description="Filter on valid instances",
required=False,
type=bool,
),
]
)
)
class ScheduleFilter(filters.FilterSet):
starts = filters.DateTimeFromToRangeFilter(field_name="starts_at")
ends = filters.DateTimeFromToRangeFilter(field_name="ends_at")
position_status = filters.NumberFilter()
broadcasted = filters.NumberFilter()
overbooked = filters.BooleanFilter(method="overbooked_filter")
# pylint: disable=unused-argument
def overbooked_filter(self, queryset, name, value):
# TODO: deduplicate code using the overbooked property
if value:
return queryset.filter(starts_at__gte=models.F("instance__ends_at"))
return queryset.filter(starts_at__lt=models.F("instance__ends_at"))
class Meta:
model = Schedule
fields = [] # type: ignore
class ScheduleViewSet(viewsets.ModelViewSet):
queryset = Schedule.objects.all()
serializer_class = ScheduleSerializer
filterset_fields = {
"starts": FILTER_NUMERICAL_LOOKUPS,
"ends": FILTER_NUMERICAL_LOOKUPS,
"playout_status": FILTER_NUMERICAL_LOOKUPS,
"broadcasted": FILTER_NUMERICAL_LOOKUPS,
}
filterset_class = ScheduleFilter
model_permission_name = "schedule"
def get_queryset(self):
filter_valid = self.request.query_params.get("is_valid")
if filter_valid is None:
return self.queryset.all()
filter_valid = filter_valid.strip().lower() in ("true", "yes", "1")
if filter_valid:
return self.queryset.filter(starts__lt=F("instance__ends"))
return self.queryset.filter(starts__gte=F("instance__ends"))