From 1ec2f204864e5b8da3d0fbeb4f7c4a9f007472d9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 30 Nov 2020 14:54:45 -0800 Subject: [PATCH] avoid naming clash is to_activity for field vs model --- bookwyrm/models/base_model.py | 6 +++--- bookwyrm/models/fields.py | 18 ++++++++--------- bookwyrm/models/relationship.py | 18 ++++++++--------- bookwyrm/models/shelf.py | 34 +++++++++++++-------------------- bookwyrm/models/status.py | 2 +- 5 files changed, 34 insertions(+), 44 deletions(-) diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 34c4e5bc..af65a36a 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -66,7 +66,7 @@ def unfurl_related_field(related_field): if hasattr(related_field, 'all'): return [unfurl_related_field(i) for i in related_field.all()] if related_field.reverse_unfurl: - return related_field.to_activity() + return related_field.field_to_activity() return related_field.remote_id @@ -79,10 +79,10 @@ class ActivitypubMixin: ''' convert from a model to an activity ''' activity = {} for field in self.__class__._meta.get_fields(): - if not hasattr(field, 'to_activity'): + if not hasattr(field, 'field_to_activity'): continue key = get_field_name(field) - value = field.to_activity(getattr(self, field.name)) + value = field.field_to_activity(getattr(self, field.name)) if value is None: continue diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index b03c5492..197b6e8e 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -33,7 +33,7 @@ class ActivitypubFieldMixin: self.activitypub_field = activitypub_field super().__init__(*args, **kwargs) - def to_activity(self, value): + def field_to_activity(self, value): ''' formatter to convert a model value into activitypub ''' if hasattr(self, 'activitypub_wrapper'): value = {self.activitypub_wrapper: value} @@ -81,13 +81,13 @@ class UsernameField(ActivitypubFieldMixin, models.CharField): del kwargs['error_messages'] return name, path, args, kwargs - def to_activity(self, value): + def field_to_activity(self, value): return value.split('@')[0] class ForeignKey(ActivitypubFieldMixin, models.ForeignKey): ''' activitypub-aware foreign key field ''' - def to_activity(self, value): + def field_to_activity(self, value): if not value: return None return value.remote_id @@ -97,7 +97,7 @@ class ForeignKey(ActivitypubFieldMixin, models.ForeignKey): class OneToOneField(ActivitypubFieldMixin, models.OneToOneField): ''' activitypub-aware foreign key field ''' - def to_activity(self, value): + def field_to_activity(self, value): if not value: return None return value.to_activity() @@ -112,7 +112,7 @@ class ManyToManyField(ActivitypubFieldMixin, models.ManyToManyField): self.link_only = link_only super().__init__(*args, **kwargs) - def to_activity(self, value): + def field_to_activity(self, value): if self.link_only: return '%s/followers' % value.instance.remote_id return [i.remote_id for i in value.all()] @@ -129,7 +129,7 @@ class TagField(ManyToManyField): super().__init__(*args, **kwargs) self.activitypub_field = 'tag' - def to_activity(self, value): + def field_to_activity(self, value): tags = [] for item in value.all(): activity_type = item.__class__.__name__ @@ -156,7 +156,7 @@ def image_serializer(value): class ImageField(ActivitypubFieldMixin, models.ImageField): ''' activitypub-aware image field ''' - def to_activity(self, value): + def field_to_activity(self, value): return image_serializer(value) def from_activity(self, activity_data): @@ -183,14 +183,14 @@ class ImageField(ActivitypubFieldMixin, models.ImageField): class DateTimeField(ActivitypubFieldMixin, models.DateTimeField): ''' activitypub-aware datetime field ''' - def to_activity(self, value): + def field_to_activity(self, value): if not value: return None return value.isoformat() class ArrayField(ActivitypubFieldMixin, DjangoArrayField): ''' activitypub-aware array field ''' - def to_activity(self, value): + def field_to_activity(self, value): return [str(i) for i in value] class CharField(ActivitypubFieldMixin, models.CharField): diff --git a/bookwyrm/models/relationship.py b/bookwyrm/models/relationship.py index dbf99778..8913b9ab 100644 --- a/bookwyrm/models/relationship.py +++ b/bookwyrm/models/relationship.py @@ -2,20 +2,23 @@ from django.db import models from bookwyrm import activitypub -from .base_model import ActivitypubMixin, ActivityMapping, BookWyrmModel +from .base_model import ActivitypubMixin, BookWyrmModel +from . import fields class UserRelationship(ActivitypubMixin, BookWyrmModel): ''' many-to-many through table for followers ''' - user_subject = models.ForeignKey( + user_subject = fields.ForeignKey( 'User', on_delete=models.PROTECT, - related_name='%(class)s_user_subject' + related_name='%(class)s_user_subject', + activitypub_field='actor', ) - user_object = models.ForeignKey( + user_object = fields.ForeignKey( 'User', on_delete=models.PROTECT, - related_name='%(class)s_user_object' + related_name='%(class)s_user_object', + activitypub_field='object', ) class Meta: @@ -32,11 +35,6 @@ class UserRelationship(ActivitypubMixin, BookWyrmModel): ) ] - activity_mappings = [ - ActivityMapping('id', 'remote_id'), - ActivityMapping('actor', 'user_subject'), - ActivityMapping('object', 'user_object'), - ] activity_serializer = activitypub.Follow def get_remote_id(self, status=None): diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index b8d5ea17..fc63d198 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -3,17 +3,19 @@ import re from django.db import models from bookwyrm import activitypub -from .base_model import ActivityMapping, BookWyrmModel +from .base_model import BookWyrmModel from .base_model import OrderedCollectionMixin, PrivacyLevels +from . import fields class Shelf(OrderedCollectionMixin, BookWyrmModel): ''' a list of books owned by a user ''' - name = models.CharField(max_length=100) + name = fields.CharField(max_length=100) identifier = models.CharField(max_length=100) - user = models.ForeignKey('User', on_delete=models.PROTECT) + user = fields.ForeignKey( + 'User', on_delete=models.PROTECT, activitypub_field='owner') editable = models.BooleanField(default=True) - privacy = models.CharField( + privacy = fields.CharField( max_length=255, default='public', choices=PrivacyLevels.choices @@ -48,31 +50,21 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel): ''' user/shelf unqiueness ''' unique_together = ('user', 'identifier') - activity_mappings = [ - ActivityMapping('id', 'remote_id'), - ActivityMapping('owner', 'user'), - ActivityMapping('name', 'name'), - ] - class ShelfBook(BookWyrmModel): ''' many to many join table for books and shelves ''' - book = models.ForeignKey('Edition', on_delete=models.PROTECT) - shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT) - added_by = models.ForeignKey( + book = fields.ForeignKey( + 'Edition', on_delete=models.PROTECT, activitypub_field='object') + shelf = fields.ForeignKey( + 'Shelf', on_delete=models.PROTECT, activitypub_field='target') + added_by = fields.ForeignKey( 'User', blank=True, null=True, - on_delete=models.PROTECT + on_delete=models.PROTECT, + activitypub_field='actor' ) - activity_mappings = [ - ActivityMapping('id', 'remote_id'), - ActivityMapping('actor', 'added_by'), - ActivityMapping('object', 'book'), - ActivityMapping('target', 'shelf'), - ] - activity_serializer = activitypub.AddBook def to_add_activity(self, user): diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index c47a33bb..371a6345 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -89,7 +89,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel): mentions = [u.remote_id for u in self.mention_users.all()] # this is a link to the followers list: followers = self.user.__class__._meta.get_field('followers')\ - .to_activity(self.user.followers) + .field_to_activity(self.user.followers) if self.privacy == 'public': activity['to'] = [public] activity['cc'] = [followers] + mentions