From 28422019747df6183af6caf14991922bfe371999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 12 Oct 2016 15:22:18 +0200 Subject: [PATCH] Activity logging for shots --- attract/__init__.py | 3 +- attract/shots/__init__.py | 6 ++++ attract/shots/eve_hooks.py | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 attract/shots/eve_hooks.py diff --git a/attract/__init__.py b/attract/__init__.py index 07b407c..3dcd218 100644 --- a/attract/__init__.py +++ b/attract/__init__.py @@ -83,10 +83,11 @@ class AttractExtension(PillarExtension): def setup_app(self, app): """Connects Blinker signals.""" - from . import subversion, tasks, eve_hooks, subquery + from . import subversion, tasks, eve_hooks, subquery, shots subversion.task_logged.connect(self.task_manager.task_logged_in_svn) tasks.setup_app(app) + shots.setup_app(app) eve_hooks.setup_app(app) subquery.setup_app(app) diff --git a/attract/shots/__init__.py b/attract/shots/__init__.py index 41d5d52..273a43b 100644 --- a/attract/shots/__init__.py +++ b/attract/shots/__init__.py @@ -262,3 +262,9 @@ def assert_is_valid_patch(patch): raise wz_exceptions.BadRequest(u"Operation '%s' does not allow you to set fields %s" % ( op, disallowed_fields )) + + +def setup_app(app): + from . import eve_hooks + + eve_hooks.setup_app(app) diff --git a/attract/shots/eve_hooks.py b/attract/shots/eve_hooks.py new file mode 100644 index 0000000..cd54f48 --- /dev/null +++ b/attract/shots/eve_hooks.py @@ -0,0 +1,56 @@ +import logging + +from attract.node_types.shot import node_type_shot +from pillar.api.nodes import only_for_node_type_decorator +import pillar.api.activities +import pillar.api.utils.authentication + +log = logging.getLogger(__name__) +only_for_shot = only_for_node_type_decorator(node_type_shot['name']) + + +def register_shot_activity(shot, descr): + user_id = pillar.api.utils.authentication.current_user_id() + pillar.api.activities.register_activity( + user_id, + descr, + 'node', shot['_id'], + 'project', shot['project'], + shot['project'], + node_type=shot['node_type'], + ) + + +@only_for_shot +def activity_after_replacing_shot(shot, original): + """ + Note: this is also used on PATCH, since our custom shot PATCH handler + performs a PUT-internal to run the patched node through Eve for + validation. + """ + + # TODO: compare to original, and either mention the things that changed, + # or (if they are equal) don't log an activity at all. + register_shot_activity(shot, 'edited shot "%s"' % shot['name']) + + +@only_for_shot +def activity_after_creating_shot(shot): + register_shot_activity(shot, 'created a new shot "%s"' % shot['name']) + + +def activity_after_creating_shots(nodes): + for node in nodes: + activity_after_creating_shot(node) + + +@only_for_shot +def activity_after_deleting_shot(shot): + register_shot_activity(shot, 'deleted shot "%s"' % shot['name']) + + +def setup_app(app): + app.on_replaced_nodes += activity_after_replacing_shot + app.on_inserted_nodes += activity_after_creating_shots + app.on_deleted_item_nodes += activity_after_deleting_shot + app.on_deleted_resource_nodes += activity_after_deleting_shot