mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-04 14:28:49 +00:00
Generalizes link format in many to many field
This commit is contained in:
parent
f116ce378d
commit
8a900689d3
2 changed files with 26 additions and 1 deletions
|
@ -119,7 +119,7 @@ class ManyToManyField(ActivitypubFieldMixin, models.ManyToManyField):
|
||||||
|
|
||||||
def field_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/%s' % (value.instance.remote_id, self.name)
|
||||||
return [i.remote_id for i in value.all()]
|
return [i.remote_id for i in value.all()]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -78,3 +78,28 @@ class ActivitypubFields(TestCase):
|
||||||
Serializable = namedtuple('Serializable', ('to_activity', 'remote_id'))
|
Serializable = namedtuple('Serializable', ('to_activity', 'remote_id'))
|
||||||
item = Serializable(lambda: {'a': 'b'}, 'https://e.b/c')
|
item = Serializable(lambda: {'a': 'b'}, 'https://e.b/c')
|
||||||
self.assertEqual(instance.field_to_activity(item), 'https://e.b/c')
|
self.assertEqual(instance.field_to_activity(item), 'https://e.b/c')
|
||||||
|
|
||||||
|
def test_one_to_one_field(self):
|
||||||
|
instance = fields.OneToOneField('User', on_delete=models.CASCADE)
|
||||||
|
Serializable = namedtuple('Serializable', ('to_activity', 'remote_id'))
|
||||||
|
item = Serializable(lambda: {'a': 'b'}, 'https://e.b/c')
|
||||||
|
self.assertEqual(instance.field_to_activity(item), {'a': 'b'})
|
||||||
|
|
||||||
|
def test_many_to_many_field(self):
|
||||||
|
instance = fields.ManyToManyField('User')
|
||||||
|
|
||||||
|
Serializable = namedtuple('Serializable', ('to_activity', 'remote_id'))
|
||||||
|
Queryset = namedtuple('Queryset', ('all', 'instance'))
|
||||||
|
item = Serializable(lambda: {'a': 'b'}, 'https://e.b/c')
|
||||||
|
another_item = Serializable(lambda: {}, 'example.com')
|
||||||
|
|
||||||
|
items = Queryset(lambda: [item], another_item)
|
||||||
|
|
||||||
|
self.assertEqual(instance.field_to_activity(items), ['https://e.b/c'])
|
||||||
|
|
||||||
|
instance = fields.ManyToManyField('User', link_only=True)
|
||||||
|
instance.name = 'snake_case'
|
||||||
|
self.assertEqual(
|
||||||
|
instance.field_to_activity(items),
|
||||||
|
'example.com/snake_case'
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue