diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 0672fbb8..620579a0 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -124,7 +124,7 @@ class ActivityObject: formatted_value = timezone.make_aware(date_value) except ValueError: formatted_value = date_value - except ParserError: + except (ParserError, TypeError): formatted_value = None elif isinstance(model_field, ForwardManyToOneDescriptor) and \ formatted_value: @@ -182,12 +182,17 @@ class ActivityObject: model = model_field.model items = [] for link in values: - # check that the Type matches the model (because Status - # tags contain both user mentions and book tags) - if not model.activity_serializer.type == link.get('type'): - continue + if isinstance(link, dict): + # check that the Type matches the model (Status + # tags contain both user mentions and book tags) + if not model.activity_serializer.type == \ + link.get('type'): + continue + remote_id = link.get('href') + else: + remote_id = link items.append( - resolve_remote_id(model, link.get('href')) + resolve_remote_id(model, remote_id) ) getattr(instance, model_key).set(items) diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 29301fb5..877f094e 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -10,7 +10,9 @@ 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.db.models.fields.files import ImageFileDescriptor +from django.db.models.fields.related_descriptors \ + import ManyToManyDescriptor from django.dispatch import receiver from bookwyrm import activitypub @@ -72,13 +74,16 @@ class ActivitypubMixin: # this field on the model isn't serialized continue value = getattr(self, mapping.model_key) + model_field_type = getattr(self.__class__, mapping.model_key) if hasattr(value, 'remote_id'): # this is probably a foreign key field, which we want to # serialize as just the remote_id url reference value = value.remote_id + elif isinstance(model_field_type, ManyToManyDescriptor): + value = [i.remote_id for i in value.all()] elif isinstance(value, datetime): value = value.isoformat() - elif isinstance(value, ImageFieldFile): + elif isinstance(model_field_type, ImageFileDescriptor): value = image_formatter(value) # run the custom formatter function set in the model diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 8892119a..70eb8652 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -55,15 +55,10 @@ class Book(ActivitypubMixin, BookWyrmModel): published_date = models.DateTimeField(blank=True, null=True) objects = InheritanceManager() - @property - def ap_authors(self): - ''' the activitypub serialization should be a list of author ids ''' - return [a.remote_id for a in self.authors.all()] - activity_mappings = [ ActivityMapping('id', 'remote_id'), - ActivityMapping('authors', 'ap_authors'), + ActivityMapping('authors', 'authors'), ActivityMapping('firstPublishedDate', 'firstpublished_date'), ActivityMapping('publishedDate', 'published_date'), @@ -91,7 +86,7 @@ class Book(ActivitypubMixin, BookWyrmModel): ActivityMapping('publishers', 'publishers'), ActivityMapping('lccn', 'lccn'), - ActivityMapping('editions', 'editions_path'), + ActivityMapping('editions', 'editions'), ActivityMapping('cover', 'cover'), ] @@ -127,16 +122,6 @@ class Work(OrderedCollectionPageMixin, Book): null=True ) - @property - def editions_path(self): - ''' it'd be nice to serialize the edition instead but, recursion ''' - default = self.default_edition - ed_list = [ - e.remote_id for e in \ - self.edition_set.filter(~Q(id=default.id)).all() - ] - return [default.remote_id] + ed_list - def to_edition_list(self, **kwargs): ''' activitypub serialization for this work's editions '''