forked from mirrors/bookwyrm
avoid naming clash is to_activity for field vs model
This commit is contained in:
parent
77aead722d
commit
1ec2f20486
5 changed files with 34 additions and 44 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue