Replace image_serialzier helper with built-in serializers

This commit is contained in:
Mouse Reeve 2021-11-10 10:28:43 -08:00
parent d204e8dbb8
commit 4f5d23e785
3 changed files with 2 additions and 32 deletions

View file

@ -383,19 +383,6 @@ class CustomImageField(DjangoImageField):
widget = ClearableFileInputWithWarning widget = ClearableFileInputWithWarning
def image_serializer(value, alt):
"""helper for serializing images"""
if value and hasattr(value, "url"):
url = value.url
else:
return None
if url is not None:
url = url.lstrip("/")
url = urljoin(MEDIA_FULL_URL, url)
return activitypub.Document(url=url, name=alt)
class ImageField(ActivitypubFieldMixin, models.ImageField): class ImageField(ActivitypubFieldMixin, models.ImageField):
"""activitypub-aware image field""" """activitypub-aware image field"""

View file

@ -19,7 +19,6 @@ from bookwyrm.settings import ENABLE_PREVIEW_IMAGES
from .activitypub_mixin import ActivitypubMixin, ActivityMixin from .activitypub_mixin import ActivitypubMixin, ActivityMixin
from .activitypub_mixin import OrderedCollectionPageMixin from .activitypub_mixin import OrderedCollectionPageMixin
from .base_model import BookWyrmModel from .base_model import BookWyrmModel
from .fields import image_serializer
from .readthrough import ProgressMode from .readthrough import ProgressMode
from . import fields from . import fields
@ -190,15 +189,8 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
if hasattr(activity, "name"): if hasattr(activity, "name"):
activity.name = self.pure_name activity.name = self.pure_name
activity.type = self.pure_type activity.type = self.pure_type
activity.attachment = [ covers = [b.to_activity().get("cover") for b in [getattr(self, "book", None)] + list(self.mention_books.all()) if b]
image_serializer(b.cover, b.alt_text) activity.attachment = covers
for b in self.mention_books.all()[:4]
if b.cover
]
if hasattr(self, "book") and self.book.cover:
activity.attachment.append(
image_serializer(self.book.cover, self.book.alt_text)
)
return activity return activity
def to_activity(self, pure=False): # pylint: disable=arguments-differ def to_activity(self, pure=False): # pylint: disable=arguments-differ

View file

@ -447,15 +447,6 @@ class ModelFields(TestCase):
self.assertIsInstance(loaded_image, list) self.assertIsInstance(loaded_image, list)
self.assertIsInstance(loaded_image[1], ContentFile) self.assertIsInstance(loaded_image[1], ContentFile)
def test_image_serialize(self, *_):
"""make sure we're creating sensible image paths"""
ValueMock = namedtuple("ValueMock", ("url"))
value_mock = ValueMock("https://your.domain.here/images/fish.jpg")
result = fields.image_serializer(value_mock, "hello")
self.assertEqual(result.type, "Document")
self.assertEqual(result.url, "https://your.domain.here/images/fish.jpg")
self.assertEqual(result.name, "hello")
def test_datetime_field(self, *_): def test_datetime_field(self, *_):
"""this one is pretty simple, it just has to use isoformat""" """this one is pretty simple, it just has to use isoformat"""
instance = fields.DateTimeField() instance = fields.DateTimeField()