Merge pull request #1287 from paddatrapper/fix/v2-schedule

Fix v2 schedule API endpoint
This commit is contained in:
Robb 2021-08-12 15:09:36 -04:00 committed by GitHub
commit da4b27965d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 1 deletions

View File

@ -71,7 +71,6 @@ class User(AbstractBaseUser):
self.password = hashlib.md5(password.encode()).hexdigest()
def is_staff(self):
print("is_staff")
return self.type == ADMIN
def check_password(self, password):

View File

@ -22,6 +22,28 @@ class Schedule(models.Model):
def get_owner(self):
return self.instance.get_owner()
def get_cueout(self):
"""
Returns a cueout that is based on the current show. 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. This prevents the next
show having overlapping items playing.
"""
if self.instance.ends < self.ends:
return self.instance.ends - self.starts
return self.cue_out
def get_ends(self):
"""
Returns an item end that is based on the current show. Ends 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.
"""
if self.instance.ends < self.ends:
return self.instance.ends
return self.ends
class Meta:
managed = False
db_table = "cc_schedule"

View File

@ -128,6 +128,8 @@ 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)
class Meta:
model = Schedule

View File

@ -1,7 +1,9 @@
import os
from datetime import datetime, timedelta
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.utils import dateparse
from libretimeapi.views import FileViewSet
from model_bakery import baker
from rest_framework.test import APIRequestFactory, APITestCase
@ -40,3 +42,81 @@ class TestFileViewSet(APITestCase):
self.client.credentials(HTTP_AUTHORIZATION="Api-Key {}".format(self.token))
response = self.client.get(path)
self.assertEqual(response.status_code, 200)
class TestScheduleViewSet(APITestCase):
@classmethod
def setUpTestData(cls):
cls.path = "/api/v2/schedule/"
cls.token = settings.CONFIG.get("general", "api_key")
def test_schedule_item_full_length(self):
music_dir = baker.make(
"libretimeapi.MusicDir",
directory=os.path.join(os.path.dirname(__file__), "resources"),
)
f = baker.make(
"libretimeapi.File",
directory=music_dir,
mime="audio/mp3",
filepath="song.mp3",
length=timedelta(seconds=40.86),
cuein=timedelta(seconds=0),
cueout=timedelta(seconds=40.8131),
)
show = baker.make(
"libretimeapi.ShowInstance",
starts=datetime.now(tz=datetime.timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=datetime.timezone.utc) + timedelta(minutes=5),
)
scheduleItem = baker.make(
"libretimeapi.Schedule",
starts=datetime.now(tz=datetime.timezone.utc),
ends=datetime.now(tz=datetime.timezone.utc) + f.length,
cue_out=f.cueout,
instance=show,
file=f,
)
self.client.credentials(HTTP_AUTHORIZATION="Api-Key {}".format(self.token))
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(dateparse.parse_datetime(result[0]["ends"]), scheduleItem.ends)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), f.cueout)
def test_schedule_item_trunc(self):
music_dir = baker.make(
"libretimeapi.MusicDir",
directory=os.path.join(os.path.dirname(__file__), "resources"),
)
f = baker.make(
"libretimeapi.File",
directory=music_dir,
mime="audio/mp3",
filepath="song.mp3",
length=timedelta(seconds=40.86),
cuein=timedelta(seconds=0),
cueout=timedelta(seconds=40.8131),
)
show = baker.make(
"libretimeapi.ShowInstance",
starts=datetime.now(tz=datetime.timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=datetime.timezone.utc) + timedelta(seconds=20),
)
scheduleItem = baker.make(
"libretimeapi.Schedule",
starts=datetime.now(tz=datetime.timezone.utc),
ends=datetime.now(tz=datetime.timezone.utc) + f.length,
instance=show,
file=f,
)
self.client.credentials(HTTP_AUTHORIZATION="Api-Key {}".format(self.token))
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 - scheduleItem.starts
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), expected)
self.assertNotEqual(
dateparse.parse_datetime(result[0]["ends"]), scheduleItem.ends
)