Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

API udpate for multi object operations

  • Loading branch information...
commit 1852b75327f8b3e6107742106a9b10283c60bceb 1 parent d530760
@flashingpumpkin flashingpumpkin authored
View
9 README.md
@@ -58,12 +58,21 @@ Clone the repository and cd into the project folder:
* `ActionManager.get_for_actor(actor)`:
Returns all the `Action` objects involving `actor`
+* `ActionManager.get_for_actors(actors)`:
+ Similar to above, but acts on a list of actors
+
* `ActionManager.get_for_target(target)`:
Returns all the `Action` objects involving `target`
+* `ActionManager.get_for_targets(targets)`:
+ Similar to above, but acts on a list of targets
+
* `ActionManager.get_for_action_object(obj)`:
Returns all the `Action` objects involving `obj`
+* `ActionManager.get_for_action_objects(objects)`:
+ Similar to above, but acts on a list of action objects.
+
### Utils
* `stream.utils.register_actor(Model)`:
View
9 README.rst
@@ -76,12 +76,21 @@ Manager
- ``ActionManager.get_for_actor(actor)``:
Returns all the ``Action`` objects involving ``actor``
+- ``ActionManager.get_for_actors(actors)``:
+ Similar to above, but acts on a list of actors
+
- ``ActionManager.get_for_target(target)``:
Returns all the ``Action`` objects involving ``target``
+- ``ActionManager.get_for_targets(targets)``:
+ Similar to above, but acts on a list of targets
+
- ``ActionManager.get_for_action_object(obj)``:
Returns all the ``Action`` objects involving ``obj``
+- ``ActionManager.get_for_action_objects(objects)``:
+ Similar to above, but acts on a list of action objects.
+
Utils
~~~~~
View
2  stream/__init__.py
@@ -1 +1 @@
-__version__ = '0.2.1'
+__version__ = '0.2.2'
View
25 stream/models.py
@@ -35,20 +35,45 @@ def get_or_create(self, actor, verb, target=None, action_object=None, **kwargs):
return result[0], False
raise Action.MultipleObjectsReturned
+ def _multi_lookup(self, objects, method):
+ result = method(objects[0])
+ for obj in objects[1:]:
+ result = result | method(obj)
+ return result
+
def get_for_actor(self, actor):
""" Returns all objects involving `actor` """
_, f_name = actor_map[actor.__class__]
return self.filter(**{f_name:actor})
+ def get_for_actors(self, actors):
+ """
+ Similar to `get_for_actor` - but does lookups for multiple actors.
+ """
+ return self._multi_lookup(actors, self.get_for_actor)
+
def get_for_target(self, target):
""" Returns all objects involving `target` """
_, f_name = target_map[target.__class__]
return self.filter(**{f_name: target})
+ def get_for_targets(self, targets):
+ """
+ Similar to `get_for_target` - but does lookups for multiple targets.
+ """
+ return self._multi_lookup(targets, self.get_for_target)
+
def get_for_action_object(self, obj):
""" Returns all objects involving `obj` """
_, f_name = action_object_map[obj.__class__]
return self.filter(**{f_name: obj})
+
+ def get_for_action_objects(self, objects):
+ """
+ Similar to `get_for_action_object` - but does lookups for multiple
+ action objects.
+ """
+ return self._multi_lookup(objects, self.get_for_action_object)
class Action(models.Model):
"""
View
44 stream/tests.py
@@ -1,10 +1,17 @@
from django import template
-from django.contrib.auth.models import User
+from django.conf import settings
+from django.contrib.auth.models import User, Group
from django.test import TestCase
from stream import signals
from stream.models import Action
from stream import utils
+
+utils.register_actor([User, Group])
+utils.register_target([User, Group])
+utils.register_action_object([User, Group])
+
+
class TestStream(TestCase):
def setUp(self):
self.lennon = User.objects.create(username='lennon')
@@ -64,7 +71,7 @@ def test_getters_setters(self):
action.action_object = self.lennon
action.save()
- action = Action.objects.get(id=1)
+ action = Action.objects.get(id=action.id)
self.assertEqual(self.morrison, action.actor)
self.assertEqual(self.hendrix, action.target)
@@ -96,3 +103,36 @@ def test_template_tag(self):
ctx = template.Context({'action': action})
self.assertEqual("lennon did Stream Item hendrix.", tpl.render(ctx))
+
+ def test_multi_lookups(self):
+ def target_result():
+ return len(Action.objects.get_for_targets([self.lennon, self.hendrix, self.morrison]))
+ def actor_result():
+ return len(Action.objects.get_for_actors([self.lennon, self.hendrix, self.morrison]))
+
+ self.assertEqual(0, target_result())
+ self.assertEqual(0, actor_result())
+
+ utils.action.send(self.lennon, 'follow', self.hendrix)
+ self.assertEqual(1, target_result())
+ self.assertEqual(1, actor_result())
+
+ utils.action.send(self.lennon, 'follow', self.morrison)
+ self.assertEqual(2, target_result())
+ self.assertEqual(2, actor_result())
+
+ utils.action.send(self.hendrix, 'follow', self.morrison)
+ self.assertEqual(3, target_result())
+ self.assertEqual(3, actor_result())
+
+ utils.action.send(self.hendrix, 'follow', self.lennon)
+ self.assertEqual(4, target_result())
+ self.assertEqual(4, actor_result())
+
+ self.assertEqual(2, Action.objects.get_for_actors([self.lennon]).count())
+ self.assertEqual(2, Action.objects.get_for_actors([self.hendrix]).count())
+ self.assertEqual(0, Action.objects.get_for_actors([self.morrison]).count())
+
+ self.assertEqual(1, Action.objects.get_for_targets([self.hendrix]).count())
+ self.assertEqual(1, Action.objects.get_for_targets([self.lennon]).count())
+ self.assertEqual(2, Action.objects.get_for_targets([self.morrison]).count())
View
7 test_project/app/models.py
@@ -2,10 +2,3 @@
# Create your models here.
-from django.contrib.auth.models import User, Group
-from stream import utils
-
-
-utils.register_actor([User, Group])
-utils.register_target([User, Group])
-utils.register_action_object([User, Group])
Please sign in to comment.
Something went wrong with that request. Please try again.