Skip to content

Add full_url field and update download_url format in media API #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Added

- Added a `full_url` field

### Changed

- Updated the `download_url` format to match images in Wagtail, removing the domain part from the URL (#254)

## [0.16.0] - 2025-05-06

### Added
Expand Down
7 changes: 3 additions & 4 deletions src/wagtailmedia/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

from rest_framework.fields import ReadOnlyField
from wagtail.api.v2.serializers import BaseSerializer
from wagtail.api.v2.utils import get_full_url


class MediaDownloadUrlField(ReadOnlyField):
"""
Serializes the "download_url" field for media items.

Example:
"download_url": "http://api.example.com/media/my_video.mp4"
"download_url": "/media/my_video.mp4"
"""

def get_attribute(self, instance):
return instance

def to_representation(self, instance):
return get_full_url(self.context["request"], instance.url)
def to_representation(self, media):
return media.url


class MediaItemSerializer(BaseSerializer):
Expand Down
3 changes: 3 additions & 0 deletions src/wagtailmedia/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MediaAPIViewSet(BaseAPIViewSet):
"height",
"media_type",
"collection",
"full_url",
]
meta_fields = BaseAPIViewSet.meta_fields + [
"tags",
Expand All @@ -28,11 +29,13 @@ class MediaAPIViewSet(BaseAPIViewSet):
"collection",
"thumbnail",
"download_url",
"full_url",
]
nested_default_fields = BaseAPIViewSet.nested_default_fields + [
"title",
"collection",
"download_url",
"full_url",
]
name = "media"
model = get_media_model()
7 changes: 7 additions & 0 deletions src/wagtailmedia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def file_extension(self):
def url(self):
return self.file.url

@property
def full_url(self):
url = self.url
if hasattr(settings, "WAGTAILADMIN_BASE_URL") and url.startswith("/"):
url = settings.WAGTAILADMIN_BASE_URL + url
return url

@property
def sources(self):
return [
Expand Down
59 changes: 48 additions & 11 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def test_basic(self):

media = Media.objects.get(pk=item["id"])
# Check download_url
self.assertEqual(
item["meta"]["download_url"], f"http://localhost/media/{media.file}"
)
self.assertEqual(item["meta"]["download_url"], f"/media/{media.file}")
# Check full_url
self.assertEqual(item["full_url"], f"http://localhost/media/{media.file}")

self.assertEqual(item["media_type"], media.type)

Expand All @@ -86,7 +86,16 @@ def test_fields_default(self):
for item in content["items"]:
self.assertEqual(
set(item.keys()),
{"id", "meta", "title", "width", "height", "media_type", "collection"},
{
"id",
"meta",
"title",
"width",
"height",
"media_type",
"collection",
"full_url",
},
)
self.assertEqual(
set(item["meta"].keys()),
Expand All @@ -100,7 +109,16 @@ def test_fields(self):
for item in content["items"]:
self.assertEqual(
set(item.keys()),
{"id", "meta", "title", "width", "height", "media_type", "collection"},
{
"id",
"meta",
"title",
"width",
"height",
"media_type",
"collection",
"full_url",
},
)
self.assertEqual(
set(item["meta"].keys()),
Expand All @@ -113,7 +131,8 @@ def test_remove_fields(self):

for item in content["items"]:
self.assertEqual(
set(item.keys()), {"id", "meta", "width", "height", "media_type"}
set(item.keys()),
{"id", "meta", "width", "height", "media_type", "full_url"},
)

def test_remove_meta_fields(self):
Expand Down Expand Up @@ -154,7 +173,16 @@ def test_all_fields(self):
for item in content["items"]:
self.assertEqual(
set(item.keys()),
{"id", "meta", "title", "width", "height", "media_type", "collection"},
{
"id",
"meta",
"title",
"width",
"height",
"media_type",
"collection",
"full_url",
},
)
self.assertEqual(
set(item["meta"].keys()),
Expand All @@ -168,7 +196,9 @@ def test_all_fields_then_remove_something(self):
content = json.loads(response.content.decode("UTF-8"))

for item in content["items"]:
self.assertEqual(set(item.keys()), {"id", "meta", "width", "height"})
self.assertEqual(
set(item.keys()), {"id", "meta", "width", "height", "full_url"}
)
self.assertEqual(set(item["meta"].keys()), {"type", "detail_url", "tags"})

def test_fields_tags(self):
Expand Down Expand Up @@ -465,6 +495,13 @@ def test_basic(self):
self.assertIn("download_url", content["meta"])
self.assertEqual(
content["meta"]["download_url"],
"/media/media/2001_a_space_odyssey.mp4",
)

# Check full_url
self.assertIn("full_url", content)
self.assertEqual(
content["full_url"],
"http://localhost/media/media/2001_a_space_odyssey.mp4",
)

Expand All @@ -488,13 +525,13 @@ def test_tags(self):
self.assertEqual(content["meta"]["tags"], ["hello", "world"])

@override_settings(WAGTAILAPI_BASE_URL="http://api.example.com/")
def test_download_url_with_custom_base_url(self):
def test_full_url_with_custom_base_url(self):
response = self.get_response(self.pink_floyd_time.pk)
content = json.loads(response.content.decode("UTF-8"))

self.assertIn("download_url", content["meta"])
self.assertIn("full_url", content)
self.assertEqual(
content["meta"]["download_url"],
content["full_url"],
"http://api.example.com/media/media/pink_floyd_time.mp3",
)

Expand Down