Add full URL generation to image_serializer

This commit is contained in:
Joachim 2021-10-27 18:56:37 +02:00
parent 35bd4a4071
commit b956b79bd0
2 changed files with 11 additions and 6 deletions

View file

@ -389,6 +389,10 @@ def image_serializer(value, alt):
url = value.url url = value.url
else: else:
return None return None
if url is not None:
url = url.lstrip("/")
url = urljoin(MEDIA_FULL_URL, url)
return activitypub.Document(url=url, name=alt) return activitypub.Document(url=url, name=alt)

View file

@ -2,6 +2,7 @@
from unittest.mock import patch from unittest.mock import patch
from io import BytesIO from io import BytesIO
import pathlib import pathlib
from urllib.parse import urljoin
from django.http import Http404 from django.http import Http404
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
@ -192,7 +193,7 @@ class Status(TestCase):
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
f"https://{settings.MEDIA_FULL_URL}{self.book.cover.url}", urljoin(settings.MEDIA_FULL_URL, self.book.cover.url.lstrip("/")),
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") self.assertEqual(activity["attachment"][0].name, "Test Edition")
@ -222,7 +223,7 @@ class Status(TestCase):
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
f"https://{settings.MEDIA_FULL_URL}{self.book.cover.url}", urljoin(settings.MEDIA_FULL_URL,self.book.cover.url.lstrip("/")),
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") self.assertEqual(activity["attachment"][0].name, "Test Edition")
@ -259,7 +260,7 @@ class Status(TestCase):
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
"https://{settings.MEDIA_FULL_URL}{self.book.cover.url}", urljoin(settings.MEDIA_FULL_URL, self.book.cover.url.lstrip("/")),
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") self.assertEqual(activity["attachment"][0].name, "Test Edition")
@ -300,7 +301,7 @@ class Status(TestCase):
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
f"https://{settings.MEDIA_FULL_URL}{self.book.cover.url}", urljoin(settings.MEDIA_FULL_URL, self.book.cover.url.lstrip("/"))
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") self.assertEqual(activity["attachment"][0].name, "Test Edition")
@ -322,7 +323,7 @@ class Status(TestCase):
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
f"https://{settings.MEDIA_FULL_URL}{self.book.cover.url}", urljoin(settings.MEDIA_FULL_URL, self.book.cover.url.lstrip("/")),
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") self.assertEqual(activity["attachment"][0].name, "Test Edition")
@ -343,7 +344,7 @@ class Status(TestCase):
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
f"https://{settings.MEDIA_FULL_URL}{self.book.cover.url}", urljoin(settings.MEDIA_FULL_URL, self.book.cover.url.lstrip("/")),
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") self.assertEqual(activity["attachment"][0].name, "Test Edition")