From d474e71afd2845f210c44577f6765cf1a100bcbb Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Mon, 16 Mar 2015 15:03:28 +0000 Subject: [PATCH] Add `JsonVideoContent` * `JsonVideoContent` integrates with the `render_json` extension in `feincms-extensions`. --- videos/content.py | 26 ++++++++++++++++++++++++++ videos/tests/factories.py | 1 + videos/tests/files/image.png | Bin 0 -> 146 bytes videos/tests/models.py | 5 ++++- videos/tests/test_content.py | 29 ++++++++++++++++++++++++++++- 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 videos/tests/files/image.png 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 0000000000000000000000000000000000000000..c8b9539cdc4fb1ec7071e7bb56313ca8f450dab8 GIT binary patch literal 146 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1SBVv2j2ryoCO|{#S9GG!XV7ZFl&wkP>{XE z)7O>#K9evHvvtdxHz$BXk|nMYCC>S|xv6<249-QVi6yBi3gww484B*6z5(HleBwYw lJf1F&Asp9}fBgS%&%pYR>EE3*k