From 24806903781dd634cf95de2b2039037f9ed64e19 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 27 Nov 2020 18:26:07 -0800 Subject: [PATCH] Automatically handle image fields in model serializer --- bookwyrm/activitypub/base_activity.py | 2 +- bookwyrm/models/base_model.py | 13 +++++++------ bookwyrm/models/book.py | 15 ++------------- bookwyrm/models/user.py | 6 +----- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 3fe44f8b..33820e1e 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -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 diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 8c28c8ab..4109a49b 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -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) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 7a652832..132b4c07 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -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): diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index b38a4b19..4d511d56 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -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'