Fixes image attachments

This commit is contained in:
Mouse Reeve 2021-03-15 13:55:48 -07:00
parent 8396785a49
commit 220f2c1eb4
4 changed files with 55 additions and 6 deletions

View file

@ -9,5 +9,5 @@ class Image(ActivityObject):
url: str
name: str = ""
type: str = "Image"
id: str = ""
type: str = "Document"
id: str = None

View file

@ -445,7 +445,11 @@ def unfurl_related_field(related_field, sort_field=None):
unfurl_related_field(i) for i in related_field.order_by(sort_field).all()
]
if related_field.reverse_unfurl:
return related_field.field_to_activity()
# if it's a one-to-one (key pair)
if hasattr(related_field, "field_to_activity"):
return related_field.field_to_activity()
# if it's one-to-many (attachments)
return related_field.to_activity()
return related_field.remote_id

View file

@ -25,7 +25,11 @@ class Image(Attachment):
""" an image attachment """
image = fields.ImageField(
upload_to="status/", null=True, blank=True, activitypub_field="url"
upload_to="status/",
null=True,
blank=True,
activitypub_field="url",
alt_field="caption",
)
caption = fields.TextField(null=True, blank=True, activitypub_field="name")

View file

@ -1,5 +1,10 @@
""" test for app action functionality """
from io import BytesIO
from unittest.mock import patch
import pathlib
from PIL import Image
from django.core.files.base import ContentFile
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
@ -9,8 +14,8 @@ from bookwyrm import views
from bookwyrm.activitypub import ActivitypubResponse
class FeedMessageViews(TestCase):
""" dms """
class FeedViews(TestCase):
""" activity feed, statuses, dms """
def setUp(self):
""" we need basic test data and mocks """
@ -59,6 +64,42 @@ class FeedMessageViews(TestCase):
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
def test_status_page_with_image(self):
""" there are so many views, this just makes sure it LOADS """
view = views.Status.as_view()
image_file = pathlib.Path(__file__).parent.joinpath(
"../../static/images/default_avi.jpg"
)
image = Image.open(image_file)
output = BytesIO()
image.save(output, format=image.format)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
status = models.Review.objects.create(
content="hi",
user=self.local_user,
book=self.book,
)
attachment = models.Image.objects.create(
status=status, caption="alt text here"
)
attachment.image.save("test.jpg", ContentFile(output.getvalue()))
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = False
result = view(request, "mouse", status.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
with patch("bookwyrm.views.feed.is_api_request") as is_api:
is_api.return_value = True
result = view(request, "mouse", status.id)
self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200)
def test_replies_page(self):
""" there are so many views, this just makes sure it LOADS """
view = views.Replies.as_view()