automatically load authors and editions

This commit is contained in:
Mouse Reeve 2020-11-28 13:00:04 -08:00
parent e99394e6f7
commit 421a13fda0
3 changed files with 20 additions and 25 deletions

View file

@ -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)

View file

@ -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

View file

@ -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 '''