diff --git a/videos/content.py b/videos/content.py index dd6bf1a..6493e2f 100644 --- a/videos/content.py +++ b/videos/content.py @@ -61,3 +61,29 @@ def render(self, **kwargs): self.get_context_data(**kwargs), context_instance=kwargs.get('context'), ) + + +class JsonVideoContent(VideoContent): + class Meta(VideoContent.Meta): + abstract = True + + def json(self): + """Return a json serializable representation of the video.""" + video = self.video + + def source_json(source): + """Return a json serializable representation of a video source.""" + return { + 'file': source.file.url, + 'type': source.type, + } + + return { + 'created': video.created, + 'length': video.length, + 'preview': video.preview.url, + 'recorded': video.recorded, + 'slug': video.slug, + 'sources': [source_json(source) for source in video.sources.all()], + 'title': video.title, + } diff --git a/videos/tests/factories.py b/videos/tests/factories.py index fcbda2e..55b64a4 100644 --- a/videos/tests/factories.py +++ b/videos/tests/factories.py @@ -17,3 +17,4 @@ class VideoFactory(factory.DjangoModelFactory): title = factory.Sequence('Video {}'.format) slug = factory.Sequence('video-{}'.format) captions_file = 'captionfile.txt' + preview = factory.django.FileField(from_path='videos/tests/files/image.png') diff --git a/videos/tests/files/image.png b/videos/tests/files/image.png new file mode 100644 index 0000000..c8b9539 Binary files /dev/null and b/videos/tests/files/image.png differ diff --git a/videos/tests/models.py b/videos/tests/models.py index 7d6169f..14f30ed 100644 --- a/videos/tests/models.py +++ b/videos/tests/models.py @@ -1,6 +1,6 @@ from feincms.models import create_base_model -from ..content import VideoContent +from ..content import JsonVideoContent, VideoContent from ..models import Video @@ -20,3 +20,6 @@ class DummyPage(create_base_model()): DummyPage.create_content_type(VideoContent, TYPE_CHOICES=( ('block', 'Block'), )) +DummyPage.create_content_type(JsonVideoContent, TYPE_CHOICES=( + ('block', 'Block'), +)) diff --git a/videos/tests/test_content.py b/videos/tests/test_content.py index 5b288ce..1a22c7a 100644 --- a/videos/tests/test_content.py +++ b/videos/tests/test_content.py @@ -3,7 +3,7 @@ from . import factories from .models import DummyPage -from ..content import VideoContent +from ..content import JsonVideoContent, VideoContent from videos.module.chapters.tests.factories import ChapterFactory from videos.module.speakers.tests.factories import SpeakerFactory @@ -78,6 +78,33 @@ def test_render(self): self.assertLess(result.index(captions_str), result.index(end_video_str)) +class TestJsonVideoContent(TestCase): + model = DummyPage.content_type_for(JsonVideoContent) + + def test_json(self): + """A JsonVideoContent can be rendered to json.""" + video = factories.VideoFactory.create() + sources = factories.SourceFactory.create_batch(2, video=video) + content = self.model(video=video) + + def source_json(source): + return { + 'file': source.file.url, + 'type': source.type, + } + + expected = { + 'sources': [source_json(source) for source in sources], + 'title': video.title, + 'slug': video.slug, + 'preview': video.preview.url, + 'length': video.length, + 'recorded': video.recorded, + 'created': video.created, + } + self.assertEqual(content.json(), expected) + + class TestContentAccessible(TestCase): def test_object_has_content(self): concrete_content_type = DummyPage.content_type_for(VideoContent)