Skip to content

Commit

Permalink
Add JsonVideoContent
Browse files Browse the repository at this point in the history
* `JsonVideoContent` integrates with the `render_json` extension in
  `feincms-extensions`.
  • Loading branch information
LilyFoote committed Mar 16, 2015
1 parent bc745b4 commit d474e71
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
26 changes: 26 additions & 0 deletions videos/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
1 change: 1 addition & 0 deletions videos/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Binary file added videos/tests/files/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion videos/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from feincms.models import create_base_model

from ..content import VideoContent
from ..content import JsonVideoContent, VideoContent
from ..models import Video


Expand All @@ -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'),
))
29 changes: 28 additions & 1 deletion videos/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d474e71

Please sign in to comment.