Automatically handle image fields in model serializer

This commit is contained in:
Mouse Reeve 2020-11-27 18:26:07 -08:00
parent 4ae785a7f7
commit 2480690378
4 changed files with 11 additions and 25 deletions

View file

@ -139,7 +139,7 @@ class ActivityObject:
for (model_key, value) in image_fields.items():
if not value:
continue
#formatted_value = image_formatter(value)
formatted_value = image_formatter(value)
getattr(instance, model_key).save(*value, save=True)
# add one to many fields

View file

@ -10,6 +10,7 @@ from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from django.db import models
from django.db.models.fields.files import ImageFieldFile
from django.dispatch import receiver
from bookwyrm import activitypub
@ -77,16 +78,18 @@ class ActivitypubMixin:
value = value.remote_id
elif isinstance(value, datetime):
value = value.isoformat()
elif isinstance(value, ImageFieldFile):
value = image_formatter(value)
# run the custom formatter function set in the model
result = mapping.activity_formatter(value)
formatted_value = mapping.activity_formatter(value)
if mapping.activity_key in fields and \
isinstance(fields[mapping.activity_key], list):
# there can be two database fields that map to the same AP list
# this happens in status tags, which combines user and book tags
fields[mapping.activity_key] += result
fields[mapping.activity_key] += formatted_value
else:
fields[mapping.activity_key] = result
fields[mapping.activity_key] = formatted_value
if pure:
return self.pure_activity_serializer(
@ -270,12 +273,10 @@ def tag_formatter(items, name_field, activity_type):
return tags
def image_formatter(image, default_path=None):
def image_formatter(image):
''' convert images into activitypub json '''
if image and hasattr(image, 'url'):
url = image.url
elif default_path:
url = default_path
else:
return None
url = 'https://%s%s' % (DOMAIN, url)

View file

@ -12,7 +12,6 @@ from bookwyrm.utils.fields import ArrayField
from .base_model import ActivityMapping, BookWyrmModel
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
from .base_model import image_attachments_formatter
class Book(ActivitypubMixin, BookWyrmModel):
''' a generic book, which can mean either an edition or a work '''
@ -61,11 +60,6 @@ class Book(ActivitypubMixin, BookWyrmModel):
''' the activitypub serialization should be a list of author ids '''
return [a.remote_id for a in self.authors.all()]
@property
def ap_parent_work(self):
''' reference the work via local id not remote '''
return self.parent_work.remote_id
activity_mappings = [
ActivityMapping('id', 'remote_id'),
@ -87,7 +81,7 @@ class Book(ActivitypubMixin, BookWyrmModel):
ActivityMapping('librarythingKey', 'librarything_key'),
ActivityMapping('goodreadsKey', 'goodreads_key'),
ActivityMapping('work', 'ap_parent_work'),
ActivityMapping('work', 'parent_work'),
ActivityMapping('isbn10', 'isbn_10'),
ActivityMapping('isbn13', 'isbn_13'),
ActivityMapping('oclcNumber', 'oclc_number'),
@ -98,12 +92,7 @@ class Book(ActivitypubMixin, BookWyrmModel):
ActivityMapping('lccn', 'lccn'),
ActivityMapping('editions', 'editions_path'),
ActivityMapping(
'attachment', 'cover',
# this expects an iterable and the field is just an image
lambda x: image_attachments_formatter([x]),
activitypub.image_formatter
),
ActivityMapping('cover', 'cover'),
]
def save(self, *args, **kwargs):

View file

@ -112,11 +112,7 @@ class User(OrderedCollectionPageMixin, AbstractUser):
activity_formatter=lambda x: {'sharedInbox': x},
model_formatter=lambda x: x.get('sharedInbox')
),
ActivityMapping(
'icon', 'avatar',
lambda x: image_formatter(x, '/static/images/default_avi.jpg'),
activitypub.image_formatter
),
ActivityMapping('icon', 'avatar'),
ActivityMapping(
'manuallyApprovesFollowers',
'manually_approves_followers'