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).
This commit is contained in:
2016-03-04 15:47:37 +01:00
parent 819245762a
commit f034c9b2d2

View File

@@ -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(),