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(): for (model_key, value) in image_fields.items():
if not value: if not value:
continue continue
#formatted_value = image_formatter(value) formatted_value = image_formatter(value)
getattr(instance, model_key).save(*value, save=True) getattr(instance, model_key).save(*value, save=True)
# add one to many fields # add one to many fields

View File

@ -10,6 +10,7 @@ from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15 from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256 from Crypto.Hash import SHA256
from django.db import models from django.db import models
from django.db.models.fields.files import ImageFieldFile
from django.dispatch import receiver from django.dispatch import receiver
from bookwyrm import activitypub from bookwyrm import activitypub
@ -77,16 +78,18 @@ class ActivitypubMixin:
value = value.remote_id value = value.remote_id
elif isinstance(value, datetime): elif isinstance(value, datetime):
value = value.isoformat() value = value.isoformat()
elif isinstance(value, ImageFieldFile):
value = image_formatter(value)
# run the custom formatter function set in the model # 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 \ if mapping.activity_key in fields and \
isinstance(fields[mapping.activity_key], list): isinstance(fields[mapping.activity_key], list):
# there can be two database fields that map to the same AP 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 # this happens in status tags, which combines user and book tags
fields[mapping.activity_key] += result fields[mapping.activity_key] += formatted_value
else: else:
fields[mapping.activity_key] = result fields[mapping.activity_key] = formatted_value
if pure: if pure:
return self.pure_activity_serializer( return self.pure_activity_serializer(
@ -270,12 +273,10 @@ def tag_formatter(items, name_field, activity_type):
return tags return tags
def image_formatter(image, default_path=None): def image_formatter(image):
''' convert images into activitypub json ''' ''' convert images into activitypub json '''
if image and hasattr(image, 'url'): if image and hasattr(image, 'url'):
url = image.url url = image.url
elif default_path:
url = default_path
else: else:
return None return None
url = 'https://%s%s' % (DOMAIN, url) 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 ActivityMapping, BookWyrmModel
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
from .base_model import image_attachments_formatter
class Book(ActivitypubMixin, BookWyrmModel): class Book(ActivitypubMixin, BookWyrmModel):
''' a generic book, which can mean either an edition or a work ''' ''' 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 ''' ''' the activitypub serialization should be a list of author ids '''
return [a.remote_id for a in self.authors.all()] 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 = [ activity_mappings = [
ActivityMapping('id', 'remote_id'), ActivityMapping('id', 'remote_id'),
@ -87,7 +81,7 @@ class Book(ActivitypubMixin, BookWyrmModel):
ActivityMapping('librarythingKey', 'librarything_key'), ActivityMapping('librarythingKey', 'librarything_key'),
ActivityMapping('goodreadsKey', 'goodreads_key'), ActivityMapping('goodreadsKey', 'goodreads_key'),
ActivityMapping('work', 'ap_parent_work'), ActivityMapping('work', 'parent_work'),
ActivityMapping('isbn10', 'isbn_10'), ActivityMapping('isbn10', 'isbn_10'),
ActivityMapping('isbn13', 'isbn_13'), ActivityMapping('isbn13', 'isbn_13'),
ActivityMapping('oclcNumber', 'oclc_number'), ActivityMapping('oclcNumber', 'oclc_number'),
@ -98,12 +92,7 @@ class Book(ActivitypubMixin, BookWyrmModel):
ActivityMapping('lccn', 'lccn'), ActivityMapping('lccn', 'lccn'),
ActivityMapping('editions', 'editions_path'), ActivityMapping('editions', 'editions_path'),
ActivityMapping( ActivityMapping('cover', 'cover'),
'attachment', 'cover',
# this expects an iterable and the field is just an image
lambda x: image_attachments_formatter([x]),
activitypub.image_formatter
),
] ]
def save(self, *args, **kwargs): def save(self, *args, **kwargs):

View File

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