Serialize links for books

This commit is contained in:
Mouse Reeve
2021-12-15 17:10:59 -08:00
parent 86b294afd7
commit 5c99f142f9
5 changed files with 45 additions and 30 deletions

View File

@ -234,7 +234,10 @@ class Work(OrderedCollectionPageMixin, Book):
)
activity_serializer = activitypub.Work
serialize_reverse_fields = [("editions", "editions", "-edition_rank")]
serialize_reverse_fields = [
("editions", "editions", "-edition_rank"),
("file_links", "fileLinks", "-created_date"),
]
deserialize_reverse_fields = [("editions", "editions"), ("file_links", "fileLinks")]
@ -289,6 +292,8 @@ class Edition(Book):
activity_serializer = activitypub.Edition
name_field = "title"
serialize_reverse_fields = [("file_links", "fileLinks", "-created_date")]
deserialize_reverse_fields = [("file_links", "fileLinks")]
def get_rank(self):
"""calculate how complete the data is on this edition"""

View File

@ -23,11 +23,15 @@ class Link(ActivitypubMixin, BookWyrmModel):
del kwargs["broadcast"]
return super().save(*args, **kwargs)
def to_activity(self, omit=(), **kwargs):
"""we don't need ALL the fields"""
return super().to_activity(omit=("@context", "id"), **kwargs)
class FileLink(Link):
"""a link to a file"""
book = fields.ForeignKey(
book = models.ForeignKey(
"Book", on_delete=models.CASCADE, related_name="file_links", null=True
)
filetype = fields.CharField(max_length=5, activitypub_field="mediaType")

View File

@ -344,6 +344,7 @@ class User(OrderedCollectionPageMixin, AbstractUser):
def delete(self, *args, **kwargs):
"""deactivate rather than delete a user"""
# pylint: disable=attribute-defined-outside-init
self.is_active = False
# skip the logic in this class's save()
super().save(*args, **kwargs)
@ -404,14 +405,6 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
self.private_key, self.public_key = create_key_pair()
return super().save(*args, **kwargs)
def to_activity(self, **kwargs):
"""override default AP serializer to add context object
idk if this is the best way to go about this"""
activity_object = super().to_activity(**kwargs)
del activity_object["@context"]
del activity_object["type"]
return activity_object
class AnnualGoal(BookWyrmModel):
"""set a goal for how many books you read in a year"""