kopia lustrzana https://gitlab.com/marnanel/chapeau
Thing adds Audiences for audience fields. Plus test.
rodzic
b0a5b78f75
commit
02baeaa1f4
|
@ -7,6 +7,7 @@ from django_kepi.models.audience import Audience
|
|||
|
||||
__all__ = [
|
||||
'Actor',
|
||||
'Audience',
|
||||
'Thing',
|
||||
'ThingField',
|
||||
'create',
|
||||
|
|
|
@ -22,6 +22,7 @@ FIELD_CHOICES = [
|
|||
]
|
||||
|
||||
FIELD_NAMES = dict([(v,f) for (f,v) in FIELD_CHOICES])
|
||||
AUDIENCE_FIELD_NAMES = FIELD_NAMES.keys()
|
||||
|
||||
class Audience(models.Model):
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ from django.conf import settings
|
|||
from django_kepi.find import find, is_local
|
||||
from django_kepi.delivery import deliver
|
||||
import django_kepi.models.following
|
||||
import django_kepi.models.audience
|
||||
import logging
|
||||
import random
|
||||
import json
|
||||
|
@ -178,6 +179,8 @@ class Thing(models.Model):
|
|||
elif self.f_type=='Follow':
|
||||
|
||||
local_user = find(self['object'], local_only=True)
|
||||
remote_user = find(self['actor'])
|
||||
|
||||
if local_user is not None and local_user.auto_follow:
|
||||
logger.info('Local user %s has auto_follow set; must Accept',
|
||||
local_user)
|
||||
|
@ -188,7 +191,7 @@ class Thing(models.Model):
|
|||
)
|
||||
|
||||
accept_the_request = create(
|
||||
f_to = [self['actor']],
|
||||
f_to = remote_user['inbox'],
|
||||
f_type = 'Accept',
|
||||
f_actor = self['object'],
|
||||
f_object = self.url,
|
||||
|
@ -364,16 +367,21 @@ class Thing(models.Model):
|
|||
logger.warn(message)
|
||||
raise ValueError(message)
|
||||
|
||||
# Right, we need to create some objects.
|
||||
# Everything in "value" needs to go into:
|
||||
# - a Thing, containing records such as "type",
|
||||
# - zero or more Audiences, for "to" etc
|
||||
# - zero or more ThingFields, for everything else
|
||||
|
||||
for f,v in value.items():
|
||||
if f in [
|
||||
'actor',
|
||||
'name',
|
||||
'type',
|
||||
]:
|
||||
record_fields['f_'+f] = v
|
||||
del other_fields[f]
|
||||
|
||||
record_fields['f_type'] = value['type']
|
||||
|
||||
if 'id' in value:
|
||||
record_fields['remote_url'] = value['id']
|
||||
|
||||
|
@ -389,14 +397,22 @@ class Thing(models.Model):
|
|||
)
|
||||
|
||||
for f, v in other_fields.items():
|
||||
value = json.dumps(v, sort_keys=True)
|
||||
n = ThingField(
|
||||
parent = result,
|
||||
name = f,
|
||||
value = value,
|
||||
|
||||
if f in django_kepi.models.audience.AUDIENCE_FIELD_NAMES:
|
||||
django_kepi.models.audience.Audience.add_audiences_for(
|
||||
thing = result,
|
||||
field = f,
|
||||
value = v,
|
||||
)
|
||||
n.save()
|
||||
logger.debug(' -- %s', n)
|
||||
else:
|
||||
value = json.dumps(v, sort_keys=True)
|
||||
n = ThingField(
|
||||
parent = result,
|
||||
name = f,
|
||||
value = value,
|
||||
)
|
||||
n.save()
|
||||
logger.debug(' -- %s', n)
|
||||
|
||||
for f,v in [
|
||||
('actor', result.f_actor),
|
||||
|
|
|
@ -32,6 +32,26 @@ class TestAudience(TestCase):
|
|||
self.assertEqual(results[0].recipient, REMOTE_FRED)
|
||||
self.assertEqual(results[1].recipient, REMOTE_JIM)
|
||||
|
||||
def test_create(self):
|
||||
narcissus = create_local_person(
|
||||
name = 'narcissus',
|
||||
)
|
||||
|
||||
like = create(
|
||||
f_type = 'Like',
|
||||
f_actor = narcissus,
|
||||
f_object = narcissus,
|
||||
to = [ REMOTE_FRED, REMOTE_JIM, ],
|
||||
)
|
||||
|
||||
results = Audience.objects.filter(
|
||||
parent = like,
|
||||
)
|
||||
|
||||
self.assertEqual(len(results), 2)
|
||||
self.assertEqual(results[0].recipient, REMOTE_FRED)
|
||||
self.assertEqual(results[1].recipient, REMOTE_JIM)
|
||||
|
||||
def test_get_audiences_for(self):
|
||||
narcissus = create_local_person(
|
||||
name = 'narcissus',
|
||||
|
|
Ładowanie…
Reference in New Issue