From 5a856c89729104d982b4d126097f422556de6787 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Mon, 12 Oct 2015 18:24:57 +0200 Subject: [PATCH] Tweaks to node update method We override the update method so that the allowed_methods property can be stripped before being sent to the server, avoiding validation issues. We also introduce a new has_method method, which can be used in the UI to check if the user can perform a specific method on the node. --- pillarsdk/nodes.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pillarsdk/nodes.py b/pillarsdk/nodes.py index 1fce060..83ebf61 100755 --- a/pillarsdk/nodes.py +++ b/pillarsdk/nodes.py @@ -31,6 +31,30 @@ class Node(List, Find, Create, Post, Update, Delete, Replace): url = utils.join_url_params(url, params) return cls(api.get(url)) + def update(self, attributes=None, api=None): + api = api or self.api + attributes = attributes or self.to_dict() + etag = attributes['_etag'] + attributes.pop('_id') + attributes.pop('_etag') + attributes.pop('_created') + attributes.pop('_updated') + attributes.pop('_links') + attributes.pop('allowed_methods') + url = utils.join_url(self.path, str(self['_id'])) + headers = utils.merge_dict( + self.http_headers(), + {'If-Match': str(etag)}) + new_attributes = api.put(url, attributes, headers) + self.error = None + self.merge(new_attributes) + return self.success() + + def has_method(self, method): + if method in self.allowed_methods: + return True + return False + class NodeType(List, Find, Create, Post, Delete): """NodeType class wrapping the REST node_types endpoint