Skip to content

Commit 1852b75

Browse files
API udpate for multi object operations
1 parent d530760 commit 1852b75

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ Clone the repository and cd into the project folder:
5858
* `ActionManager.get_for_actor(actor)`:
5959
Returns all the `Action` objects involving `actor`
6060

61+
* `ActionManager.get_for_actors(actors)`:
62+
Similar to above, but acts on a list of actors
63+
6164
* `ActionManager.get_for_target(target)`:
6265
Returns all the `Action` objects involving `target`
6366

67+
* `ActionManager.get_for_targets(targets)`:
68+
Similar to above, but acts on a list of targets
69+
6470
* `ActionManager.get_for_action_object(obj)`:
6571
Returns all the `Action` objects involving `obj`
6672

73+
* `ActionManager.get_for_action_objects(objects)`:
74+
Similar to above, but acts on a list of action objects.
75+
6776
### Utils
6877

6978
* `stream.utils.register_actor(Model)`:

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ Manager
7676
- ``ActionManager.get_for_actor(actor)``:
7777
Returns all the ``Action`` objects involving ``actor``
7878

79+
- ``ActionManager.get_for_actors(actors)``:
80+
Similar to above, but acts on a list of actors
81+
7982
- ``ActionManager.get_for_target(target)``:
8083
Returns all the ``Action`` objects involving ``target``
8184

85+
- ``ActionManager.get_for_targets(targets)``:
86+
Similar to above, but acts on a list of targets
87+
8288
- ``ActionManager.get_for_action_object(obj)``:
8389
Returns all the ``Action`` objects involving ``obj``
8490

91+
- ``ActionManager.get_for_action_objects(objects)``:
92+
Similar to above, but acts on a list of action objects.
93+
8594
Utils
8695
~~~~~
8796

stream/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.1'
1+
__version__ = '0.2.2'

stream/models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,45 @@ def get_or_create(self, actor, verb, target=None, action_object=None, **kwargs):
3535
return result[0], False
3636
raise Action.MultipleObjectsReturned
3737

38+
def _multi_lookup(self, objects, method):
39+
result = method(objects[0])
40+
for obj in objects[1:]:
41+
result = result | method(obj)
42+
return result
43+
3844
def get_for_actor(self, actor):
3945
""" Returns all objects involving `actor` """
4046
_, f_name = actor_map[actor.__class__]
4147
return self.filter(**{f_name:actor})
4248

49+
def get_for_actors(self, actors):
50+
"""
51+
Similar to `get_for_actor` - but does lookups for multiple actors.
52+
"""
53+
return self._multi_lookup(actors, self.get_for_actor)
54+
4355
def get_for_target(self, target):
4456
""" Returns all objects involving `target` """
4557
_, f_name = target_map[target.__class__]
4658
return self.filter(**{f_name: target})
4759

60+
def get_for_targets(self, targets):
61+
"""
62+
Similar to `get_for_target` - but does lookups for multiple targets.
63+
"""
64+
return self._multi_lookup(targets, self.get_for_target)
65+
4866
def get_for_action_object(self, obj):
4967
""" Returns all objects involving `obj` """
5068
_, f_name = action_object_map[obj.__class__]
5169
return self.filter(**{f_name: obj})
70+
71+
def get_for_action_objects(self, objects):
72+
"""
73+
Similar to `get_for_action_object` - but does lookups for multiple
74+
action objects.
75+
"""
76+
return self._multi_lookup(objects, self.get_for_action_object)
5277

5378
class Action(models.Model):
5479
"""

stream/tests.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
from django import template
2-
from django.contrib.auth.models import User
2+
from django.conf import settings
3+
from django.contrib.auth.models import User, Group
34
from django.test import TestCase
45
from stream import signals
56
from stream.models import Action
67
from stream import utils
78

9+
10+
utils.register_actor([User, Group])
11+
utils.register_target([User, Group])
12+
utils.register_action_object([User, Group])
13+
14+
815
class TestStream(TestCase):
916
def setUp(self):
1017
self.lennon = User.objects.create(username='lennon')
@@ -64,7 +71,7 @@ def test_getters_setters(self):
6471
action.action_object = self.lennon
6572
action.save()
6673

67-
action = Action.objects.get(id=1)
74+
action = Action.objects.get(id=action.id)
6875

6976
self.assertEqual(self.morrison, action.actor)
7077
self.assertEqual(self.hendrix, action.target)
@@ -96,3 +103,36 @@ def test_template_tag(self):
96103
ctx = template.Context({'action': action})
97104

98105
self.assertEqual("lennon did Stream Item hendrix.", tpl.render(ctx))
106+
107+
def test_multi_lookups(self):
108+
def target_result():
109+
return len(Action.objects.get_for_targets([self.lennon, self.hendrix, self.morrison]))
110+
def actor_result():
111+
return len(Action.objects.get_for_actors([self.lennon, self.hendrix, self.morrison]))
112+
113+
self.assertEqual(0, target_result())
114+
self.assertEqual(0, actor_result())
115+
116+
utils.action.send(self.lennon, 'follow', self.hendrix)
117+
self.assertEqual(1, target_result())
118+
self.assertEqual(1, actor_result())
119+
120+
utils.action.send(self.lennon, 'follow', self.morrison)
121+
self.assertEqual(2, target_result())
122+
self.assertEqual(2, actor_result())
123+
124+
utils.action.send(self.hendrix, 'follow', self.morrison)
125+
self.assertEqual(3, target_result())
126+
self.assertEqual(3, actor_result())
127+
128+
utils.action.send(self.hendrix, 'follow', self.lennon)
129+
self.assertEqual(4, target_result())
130+
self.assertEqual(4, actor_result())
131+
132+
self.assertEqual(2, Action.objects.get_for_actors([self.lennon]).count())
133+
self.assertEqual(2, Action.objects.get_for_actors([self.hendrix]).count())
134+
self.assertEqual(0, Action.objects.get_for_actors([self.morrison]).count())
135+
136+
self.assertEqual(1, Action.objects.get_for_targets([self.hendrix]).count())
137+
self.assertEqual(1, Action.objects.get_for_targets([self.lennon]).count())
138+
self.assertEqual(2, Action.objects.get_for_targets([self.morrison]).count())

test_project/app/models.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,3 @@
22

33
# Create your models here.
44

5-
from django.contrib.auth.models import User, Group
6-
from stream import utils
7-
8-
9-
utils.register_actor([User, Group])
10-
utils.register_target([User, Group])
11-
utils.register_action_object([User, Group])

0 commit comments

Comments
 (0)