avoid naming clash is to_activity for field vs model
This commit is contained in:
parent
77aead722d
commit
1ec2f20486
|
@ -66,7 +66,7 @@ def unfurl_related_field(related_field):
|
||||||
if hasattr(related_field, 'all'):
|
if hasattr(related_field, 'all'):
|
||||||
return [unfurl_related_field(i) for i in related_field.all()]
|
return [unfurl_related_field(i) for i in related_field.all()]
|
||||||
if related_field.reverse_unfurl:
|
if related_field.reverse_unfurl:
|
||||||
return related_field.to_activity()
|
return related_field.field_to_activity()
|
||||||
return related_field.remote_id
|
return related_field.remote_id
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,10 +79,10 @@ class ActivitypubMixin:
|
||||||
''' convert from a model to an activity '''
|
''' convert from a model to an activity '''
|
||||||
activity = {}
|
activity = {}
|
||||||
for field in self.__class__._meta.get_fields():
|
for field in self.__class__._meta.get_fields():
|
||||||
if not hasattr(field, 'to_activity'):
|
if not hasattr(field, 'field_to_activity'):
|
||||||
continue
|
continue
|
||||||
key = get_field_name(field)
|
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:
|
if value is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class ActivitypubFieldMixin:
|
||||||
self.activitypub_field = activitypub_field
|
self.activitypub_field = activitypub_field
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
''' formatter to convert a model value into activitypub '''
|
''' formatter to convert a model value into activitypub '''
|
||||||
if hasattr(self, 'activitypub_wrapper'):
|
if hasattr(self, 'activitypub_wrapper'):
|
||||||
value = {self.activitypub_wrapper: value}
|
value = {self.activitypub_wrapper: value}
|
||||||
|
@ -81,13 +81,13 @@ class UsernameField(ActivitypubFieldMixin, models.CharField):
|
||||||
del kwargs['error_messages']
|
del kwargs['error_messages']
|
||||||
return name, path, args, kwargs
|
return name, path, args, kwargs
|
||||||
|
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
return value.split('@')[0]
|
return value.split('@')[0]
|
||||||
|
|
||||||
|
|
||||||
class ForeignKey(ActivitypubFieldMixin, models.ForeignKey):
|
class ForeignKey(ActivitypubFieldMixin, models.ForeignKey):
|
||||||
''' activitypub-aware foreign key field '''
|
''' activitypub-aware foreign key field '''
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
return None
|
return None
|
||||||
return value.remote_id
|
return value.remote_id
|
||||||
|
@ -97,7 +97,7 @@ class ForeignKey(ActivitypubFieldMixin, models.ForeignKey):
|
||||||
|
|
||||||
class OneToOneField(ActivitypubFieldMixin, models.OneToOneField):
|
class OneToOneField(ActivitypubFieldMixin, models.OneToOneField):
|
||||||
''' activitypub-aware foreign key field '''
|
''' activitypub-aware foreign key field '''
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
return None
|
return None
|
||||||
return value.to_activity()
|
return value.to_activity()
|
||||||
|
@ -112,7 +112,7 @@ class ManyToManyField(ActivitypubFieldMixin, models.ManyToManyField):
|
||||||
self.link_only = link_only
|
self.link_only = link_only
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
if self.link_only:
|
if self.link_only:
|
||||||
return '%s/followers' % value.instance.remote_id
|
return '%s/followers' % value.instance.remote_id
|
||||||
return [i.remote_id for i in value.all()]
|
return [i.remote_id for i in value.all()]
|
||||||
|
@ -129,7 +129,7 @@ class TagField(ManyToManyField):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.activitypub_field = 'tag'
|
self.activitypub_field = 'tag'
|
||||||
|
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
tags = []
|
tags = []
|
||||||
for item in value.all():
|
for item in value.all():
|
||||||
activity_type = item.__class__.__name__
|
activity_type = item.__class__.__name__
|
||||||
|
@ -156,7 +156,7 @@ def image_serializer(value):
|
||||||
|
|
||||||
class ImageField(ActivitypubFieldMixin, models.ImageField):
|
class ImageField(ActivitypubFieldMixin, models.ImageField):
|
||||||
''' activitypub-aware image field '''
|
''' activitypub-aware image field '''
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
return image_serializer(value)
|
return image_serializer(value)
|
||||||
|
|
||||||
def from_activity(self, activity_data):
|
def from_activity(self, activity_data):
|
||||||
|
@ -183,14 +183,14 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
|
||||||
|
|
||||||
class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):
|
class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):
|
||||||
''' activitypub-aware datetime field '''
|
''' activitypub-aware datetime field '''
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
return None
|
return None
|
||||||
return value.isoformat()
|
return value.isoformat()
|
||||||
|
|
||||||
class ArrayField(ActivitypubFieldMixin, DjangoArrayField):
|
class ArrayField(ActivitypubFieldMixin, DjangoArrayField):
|
||||||
''' activitypub-aware array field '''
|
''' activitypub-aware array field '''
|
||||||
def to_activity(self, value):
|
def field_to_activity(self, value):
|
||||||
return [str(i) for i in value]
|
return [str(i) for i in value]
|
||||||
|
|
||||||
class CharField(ActivitypubFieldMixin, models.CharField):
|
class CharField(ActivitypubFieldMixin, models.CharField):
|
||||||
|
|
|
@ -2,20 +2,23 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
from .base_model import ActivitypubMixin, ActivityMapping, BookWyrmModel
|
from .base_model import ActivitypubMixin, BookWyrmModel
|
||||||
|
from . import fields
|
||||||
|
|
||||||
|
|
||||||
class UserRelationship(ActivitypubMixin, BookWyrmModel):
|
class UserRelationship(ActivitypubMixin, BookWyrmModel):
|
||||||
''' many-to-many through table for followers '''
|
''' many-to-many through table for followers '''
|
||||||
user_subject = models.ForeignKey(
|
user_subject = fields.ForeignKey(
|
||||||
'User',
|
'User',
|
||||||
on_delete=models.PROTECT,
|
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',
|
'User',
|
||||||
on_delete=models.PROTECT,
|
on_delete=models.PROTECT,
|
||||||
related_name='%(class)s_user_object'
|
related_name='%(class)s_user_object',
|
||||||
|
activitypub_field='object',
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
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
|
activity_serializer = activitypub.Follow
|
||||||
|
|
||||||
def get_remote_id(self, status=None):
|
def get_remote_id(self, status=None):
|
||||||
|
|
|
@ -3,17 +3,19 @@ import re
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
from .base_model import ActivityMapping, BookWyrmModel
|
from .base_model import BookWyrmModel
|
||||||
from .base_model import OrderedCollectionMixin, PrivacyLevels
|
from .base_model import OrderedCollectionMixin, PrivacyLevels
|
||||||
|
from . import fields
|
||||||
|
|
||||||
|
|
||||||
class Shelf(OrderedCollectionMixin, BookWyrmModel):
|
class Shelf(OrderedCollectionMixin, BookWyrmModel):
|
||||||
''' a list of books owned by a user '''
|
''' 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)
|
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)
|
editable = models.BooleanField(default=True)
|
||||||
privacy = models.CharField(
|
privacy = fields.CharField(
|
||||||
max_length=255,
|
max_length=255,
|
||||||
default='public',
|
default='public',
|
||||||
choices=PrivacyLevels.choices
|
choices=PrivacyLevels.choices
|
||||||
|
@ -48,31 +50,21 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
|
||||||
''' user/shelf unqiueness '''
|
''' user/shelf unqiueness '''
|
||||||
unique_together = ('user', 'identifier')
|
unique_together = ('user', 'identifier')
|
||||||
|
|
||||||
activity_mappings = [
|
|
||||||
ActivityMapping('id', 'remote_id'),
|
|
||||||
ActivityMapping('owner', 'user'),
|
|
||||||
ActivityMapping('name', 'name'),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class ShelfBook(BookWyrmModel):
|
class ShelfBook(BookWyrmModel):
|
||||||
''' many to many join table for books and shelves '''
|
''' many to many join table for books and shelves '''
|
||||||
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
book = fields.ForeignKey(
|
||||||
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
|
'Edition', on_delete=models.PROTECT, activitypub_field='object')
|
||||||
added_by = models.ForeignKey(
|
shelf = fields.ForeignKey(
|
||||||
|
'Shelf', on_delete=models.PROTECT, activitypub_field='target')
|
||||||
|
added_by = fields.ForeignKey(
|
||||||
'User',
|
'User',
|
||||||
blank=True,
|
blank=True,
|
||||||
null=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
|
activity_serializer = activitypub.AddBook
|
||||||
|
|
||||||
def to_add_activity(self, user):
|
def to_add_activity(self, user):
|
||||||
|
|
|
@ -89,7 +89,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
||||||
mentions = [u.remote_id for u in self.mention_users.all()]
|
mentions = [u.remote_id for u in self.mention_users.all()]
|
||||||
# this is a link to the followers list:
|
# this is a link to the followers list:
|
||||||
followers = self.user.__class__._meta.get_field('followers')\
|
followers = self.user.__class__._meta.get_field('followers')\
|
||||||
.to_activity(self.user.followers)
|
.field_to_activity(self.user.followers)
|
||||||
if self.privacy == 'public':
|
if self.privacy == 'public':
|
||||||
activity['to'] = [public]
|
activity['to'] = [public]
|
||||||
activity['cc'] = [followers] + mentions
|
activity['cc'] = [followers] + mentions
|
||||||
|
|
Loading…
Reference in New Issue