From f034c9b2d2a161f1a23586dd02ffb298e4c3b308 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Fri, 4 Mar 2016 15:47:37 +0100 Subject: [PATCH] Introducing pop_none_attributes This function allows cleanup of dicts with None values before they get sent to the validator (and cause potential validation failures). --- pillarsdk/nodes.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pillarsdk/nodes.py b/pillarsdk/nodes.py index 6902ee9..735a80e 100755 --- a/pillarsdk/nodes.py +++ b/pillarsdk/nodes.py @@ -54,6 +54,17 @@ class Node(List, Find, Create, Post, Update, Delete, Replace): raise ResourceNotFound(response) def update(self, attributes=None, api=None): + def pop_none_attributes(attributes): + """Return a new dict with all None values removed""" + out = {} + for k, v in attributes.iteritems(): + if v: + if type(v) is dict: + attributes[k] = pop_none_attributes(v) + else: + out[k] = v + return out + api = api or self.api attributes = attributes or self.to_dict() etag = attributes['_etag'] @@ -63,9 +74,11 @@ class Node(List, Find, Create, Post, Update, Delete, Replace): attributes.pop('_updated') attributes.pop('_links', None) attributes.pop('allowed_methods') - for attr in ['parent', 'picture']: - if attr in attributes and attributes[attr] is None: - attributes.pop(attr, None) + # for attr in ['parent', 'picture']: + # if attr in attributes and attributes[attr] is None: + # attributes.pop(attr, None) + pop_none_attributes(attributes) + url = utils.join_url(self.path, str(self['_id'])) headers = utils.merge_dict( self.http_headers(),