From ade62033bafae2f26d52b0c7c60b6149372bbbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 12 Oct 2016 13:41:16 +0200 Subject: [PATCH] Added only_for_node_type_decorator(node_type_name) decorator factory func This allows you to create a decorator for Eve hooks. The decorator returns a decorator that checks its first argument's node type. If the node type is not of the required node type, returns None, otherwise calls the wrapped function. --- pillar/api/nodes/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pillar/api/nodes/__init__.py b/pillar/api/nodes/__init__.py index 6acdaece..4b7a8703 100644 --- a/pillar/api/nodes/__init__.py +++ b/pillar/api/nodes/__init__.py @@ -1,4 +1,5 @@ import base64 +import functools import logging import urlparse @@ -20,6 +21,27 @@ blueprint = Blueprint('nodes_api', __name__) ROLES_FOR_SHARING = {u'subscriber', u'demo'} +def only_for_node_type_decorator(required_node_type_name): + """Returns a decorator that checks its first argument's node type. + + If the node type is not of the required node type, returns None, + otherwise calls the wrapped function. + """ + + def only_for_node_type(wrapped): + @functools.wraps(wrapped) + def wrapper(node, *args, **kwargs): + if node.get('node_type') != required_node_type_name: + return + + return wrapped(node, *args, **kwargs) + return wrapper + + only_for_node_type.__doc__ = "Decorator, immediately returns when " \ + "the first argument is not of type %s." % required_node_type_name + return only_for_node_type + + @blueprint.route('//share', methods=['GET', 'POST']) @require_login(require_roles=ROLES_FOR_SHARING) def share_node(node_id):