From 2e159ec978be9bb2cf273853b826e37857d48594 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 20 Jul 2016 22:12:28 +0200 Subject: [PATCH] Group events by parent id --- kinto/core/events.py | 6 ++++-- kinto/core/resource/__init__.py | 4 +++- kinto/tests/core/resource/test_events.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/kinto/core/events.py b/kinto/core/events.py index 429748a423..047248629b 100644 --- a/kinto/core/events.py +++ b/kinto/core/events.py @@ -123,7 +123,8 @@ def get_resource_events(request, after_commit=False): return events -def notify_resource_event(request, timestamp, data, action, old=None): +def notify_resource_event(request, parent_id, timestamp, data, action, + old=None): """ Request helper to stack a resource event. @@ -146,10 +147,11 @@ def notify_resource_event(request, timestamp, data, action, old=None): # Get previously triggered events. events = request.bound_data.setdefault("resource_events", OrderedDict()) + resource_name = request.current_resource_name # Group events by resource and action. - group_by = resource_name + action.value + group_by = resource_name + parent_id + action.value if group_by in events: # Add to impacted records of existing event. diff --git a/kinto/core/resource/__init__.py b/kinto/core/resource/__init__.py index d9e269397e..751e8be0c6 100644 --- a/kinto/core/resource/__init__.py +++ b/kinto/core/resource/__init__.py @@ -653,7 +653,9 @@ def postprocess(self, result, action=ACTIONS.READ, old=None): 'data': result } - self.request.notify_resource_event(timestamp=self.timestamp, + parent_id = self.get_parent_id(self.request) + self.request.notify_resource_event(parent_id=parent_id, + timestamp=self.timestamp, data=result, action=action, old=old) diff --git a/kinto/tests/core/resource/test_events.py b/kinto/tests/core/resource/test_events.py index 84259b40d0..0e3e1c9990 100644 --- a/kinto/tests/core/resource/test_events.py +++ b/kinto/tests/core/resource/test_events.py @@ -352,6 +352,22 @@ def test_one_event_is_sent_per_resource(self): self.app.post_json("/batch", body, headers=self.headers) self.assertEqual(len(self.events), 2) + def test_one_event_is_sent_per_parent_id(self): + body = { + "defaults": { + "path": '/mushrooms', + "method": "POST", + "body": self.body + }, + "requests": [ + {"headers": {"Authorization": "Basic bWF0OjE="}}, + {"headers": {"Authorization": "Basic dG90bzp0dXR1"}}, + {"headers": {"Authorization": "Basic bWF0OjE="}}, + ] + } + self.app.post_json("/batch", body, headers=self.headers) + self.assertEqual(len(self.events), 2) + def test_one_event_is_sent_per_action(self): body = { "defaults": {