mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-11 02:36:37 +00:00
Serialize links for books
This commit is contained in:
parent
86b294afd7
commit
5c99f142f9
5 changed files with 45 additions and 30 deletions
|
@ -20,23 +20,6 @@ class ActivityEncoder(JSONEncoder):
|
||||||
return o.__dict__
|
return o.__dict__
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Link:
|
|
||||||
"""for tagging a book in a status"""
|
|
||||||
|
|
||||||
href: str
|
|
||||||
name: str
|
|
||||||
mediaType: str = None
|
|
||||||
type: str = "Link"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Mention(Link):
|
|
||||||
"""a subtype of Link for mentioning an actor"""
|
|
||||||
|
|
||||||
type: str = "Mention"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
class Signature:
|
class Signature:
|
||||||
|
@ -199,8 +182,9 @@ class ActivityObject:
|
||||||
)
|
)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self, **kwargs):
|
||||||
"""convert to dictionary with context attr"""
|
"""convert to dictionary with context attr"""
|
||||||
|
omit = kwargs.get("omit", ())
|
||||||
data = self.__dict__.copy()
|
data = self.__dict__.copy()
|
||||||
# recursively serialize
|
# recursively serialize
|
||||||
for (k, v) in data.items():
|
for (k, v) in data.items():
|
||||||
|
@ -209,7 +193,8 @@ class ActivityObject:
|
||||||
data[k] = v.serialize()
|
data[k] = v.serialize()
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
data = {k: v for (k, v) in data.items() if v is not None}
|
data = {k: v for (k, v) in data.items() if v is not None and k not in omit}
|
||||||
|
if "@context" not in omit:
|
||||||
data["@context"] = "https://www.w3.org/ns/activitystreams"
|
data["@context"] = "https://www.w3.org/ns/activitystreams"
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -305,3 +290,26 @@ def resolve_remote_id(
|
||||||
|
|
||||||
# if we're refreshing, "result" will be set and we'll update it
|
# if we're refreshing, "result" will be set and we'll update it
|
||||||
return item.to_model(model=model, instance=result, save=save)
|
return item.to_model(model=model, instance=result, save=save)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(init=False)
|
||||||
|
class Link(ActivityObject):
|
||||||
|
"""for tagging a book in a status"""
|
||||||
|
|
||||||
|
href: str
|
||||||
|
name: str
|
||||||
|
mediaType: str = None
|
||||||
|
id: str = None
|
||||||
|
type: str = "Link"
|
||||||
|
|
||||||
|
def serialize(self, **kwargs):
|
||||||
|
"""remove fields"""
|
||||||
|
omit = ("id", "type", "@context")
|
||||||
|
return super().serialize(omit=omit)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(init=False)
|
||||||
|
class Mention(Link):
|
||||||
|
"""a subtype of Link for mentioning an actor"""
|
||||||
|
|
||||||
|
type: str = "Mention"
|
||||||
|
|
|
@ -15,6 +15,11 @@ class PublicKey(ActivityObject):
|
||||||
publicKeyPem: str
|
publicKeyPem: str
|
||||||
type: str = "PublicKey"
|
type: str = "PublicKey"
|
||||||
|
|
||||||
|
def serialize(self, **kwargs):
|
||||||
|
"""remove fields"""
|
||||||
|
omit = ("type", "@context")
|
||||||
|
return super().serialize(omit=omit)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
@dataclass(init=False)
|
@dataclass(init=False)
|
||||||
|
|
|
@ -234,7 +234,10 @@ class Work(OrderedCollectionPageMixin, Book):
|
||||||
)
|
)
|
||||||
|
|
||||||
activity_serializer = activitypub.Work
|
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")]
|
deserialize_reverse_fields = [("editions", "editions"), ("file_links", "fileLinks")]
|
||||||
|
|
||||||
|
|
||||||
|
@ -289,6 +292,8 @@ class Edition(Book):
|
||||||
|
|
||||||
activity_serializer = activitypub.Edition
|
activity_serializer = activitypub.Edition
|
||||||
name_field = "title"
|
name_field = "title"
|
||||||
|
serialize_reverse_fields = [("file_links", "fileLinks", "-created_date")]
|
||||||
|
deserialize_reverse_fields = [("file_links", "fileLinks")]
|
||||||
|
|
||||||
def get_rank(self):
|
def get_rank(self):
|
||||||
"""calculate how complete the data is on this edition"""
|
"""calculate how complete the data is on this edition"""
|
||||||
|
|
|
@ -23,11 +23,15 @@ class Link(ActivitypubMixin, BookWyrmModel):
|
||||||
del kwargs["broadcast"]
|
del kwargs["broadcast"]
|
||||||
return super().save(*args, **kwargs)
|
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):
|
class FileLink(Link):
|
||||||
"""a link to a file"""
|
"""a link to a file"""
|
||||||
|
|
||||||
book = fields.ForeignKey(
|
book = models.ForeignKey(
|
||||||
"Book", on_delete=models.CASCADE, related_name="file_links", null=True
|
"Book", on_delete=models.CASCADE, related_name="file_links", null=True
|
||||||
)
|
)
|
||||||
filetype = fields.CharField(max_length=5, activitypub_field="mediaType")
|
filetype = fields.CharField(max_length=5, activitypub_field="mediaType")
|
||||||
|
|
|
@ -344,6 +344,7 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
"""deactivate rather than delete a user"""
|
"""deactivate rather than delete a user"""
|
||||||
|
# pylint: disable=attribute-defined-outside-init
|
||||||
self.is_active = False
|
self.is_active = False
|
||||||
# skip the logic in this class's save()
|
# skip the logic in this class's save()
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
@ -404,14 +405,6 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
|
||||||
self.private_key, self.public_key = create_key_pair()
|
self.private_key, self.public_key = create_key_pair()
|
||||||
return super().save(*args, **kwargs)
|
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):
|
class AnnualGoal(BookWyrmModel):
|
||||||
"""set a goal for how many books you read in a year"""
|
"""set a goal for how many books you read in a year"""
|
||||||
|
|
Loading…
Reference in a new issue