mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-24 10:31:05 +00:00
Replace image_serialzier helper with built-in serializers
This commit is contained in:
parent
d204e8dbb8
commit
4f5d23e785
3 changed files with 2 additions and 32 deletions
|
@ -383,19 +383,6 @@ class CustomImageField(DjangoImageField):
|
|||
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):
|
||||
"""activitypub-aware image field"""
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ from bookwyrm.settings import ENABLE_PREVIEW_IMAGES
|
|||
from .activitypub_mixin import ActivitypubMixin, ActivityMixin
|
||||
from .activitypub_mixin import OrderedCollectionPageMixin
|
||||
from .base_model import BookWyrmModel
|
||||
from .fields import image_serializer
|
||||
from .readthrough import ProgressMode
|
||||
from . import fields
|
||||
|
||||
|
@ -190,15 +189,8 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
|||
if hasattr(activity, "name"):
|
||||
activity.name = self.pure_name
|
||||
activity.type = self.pure_type
|
||||
activity.attachment = [
|
||||
image_serializer(b.cover, b.alt_text)
|
||||
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)
|
||||
)
|
||||
covers = [b.to_activity().get("cover") for b in [getattr(self, "book", None)] + list(self.mention_books.all()) if b]
|
||||
activity.attachment = covers
|
||||
return activity
|
||||
|
||||
def to_activity(self, pure=False): # pylint: disable=arguments-differ
|
||||
|
|
|
@ -447,15 +447,6 @@ class ModelFields(TestCase):
|
|||
self.assertIsInstance(loaded_image, list)
|
||||
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, *_):
|
||||
"""this one is pretty simple, it just has to use isoformat"""
|
||||
instance = fields.DateTimeField()
|
||||
|
|
Loading…
Reference in a new issue