-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add media serialized field for rich media
- Loading branch information
1 parent
e338a17
commit 0d12e8f
Showing
5 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from rest_framework.fields import Field | ||
|
||
|
||
class MediaSerializedField(Field): | ||
"""A custom serializer used to serialize media in Wagtails v2 API.""" | ||
|
||
def to_representation(self, val): | ||
"""Return the media URL, title and dimensions.""" | ||
media = [] | ||
for value in val.all(): | ||
media.append( | ||
{ | ||
"url": value.featured_media.file.url, | ||
"title": value.featured_media.title, | ||
"type": value.featured_media.type, | ||
"duration": value.featured_media.duration, | ||
"width": value.featured_media.width, | ||
"height": value.featured_media.height, | ||
"thumbnail": value.featured_media.thumbnail.url | ||
if value.featured_media.thumbnail | ||
else "", | ||
} | ||
) | ||
|
||
return media |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pytest | ||
from django.utils import timezone | ||
from model_bakery import baker | ||
from wagtail.core.models import Page | ||
|
||
from mycarehub.content.models import ( | ||
Author, | ||
ContentItem, | ||
ContentItemIndexPage, | ||
ContentItemMediaLink, | ||
) | ||
from mycarehub.content.serializers import MediaSerializedField | ||
from mycarehub.home.models import HomePage | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_media_serialized_field(): | ||
# get the root page | ||
root = Page.get_first_root_node().specific | ||
|
||
# set up a home page | ||
home = HomePage( | ||
title="Home", | ||
slug="index", | ||
) | ||
root.add_child(instance=home) | ||
|
||
# set up a content item index page | ||
content_item_index = ContentItemIndexPage( | ||
title="Content Item Index", | ||
slug="articles", | ||
intro="content", | ||
) | ||
home.add_child(instance=content_item_index) | ||
|
||
# set up a content item | ||
author = baker.make(Author) | ||
content_item = ContentItem( | ||
title="An article", | ||
slug="article-1", | ||
intro="intro", | ||
body="body", | ||
item_type="ARTICLE", | ||
date=timezone.now().date(), | ||
author=author, | ||
) | ||
content_item_index.add_child(instance=content_item) | ||
|
||
featured_media = baker.make("wagtailmedia.Media", _create_files=True) | ||
media = baker.make( | ||
ContentItemMediaLink, | ||
page=content_item, | ||
_create_files=True, | ||
featured_media=featured_media, | ||
) | ||
serializer_field = MediaSerializedField() | ||
representation = serializer_field.to_representation(content_item.featured_media) | ||
|
||
value = representation[0] | ||
assert value["url"] == media.featured_media.file.url | ||
assert value["title"] == media.featured_media.title | ||
assert value["type"] == media.featured_media.type | ||
assert value["duration"] == media.featured_media.duration | ||
assert value["width"] == media.featured_media.width | ||
assert value["height"] == media.featured_media.height |