Merge "Prevent dictionary size from changing while iterating over its items"
This commit is contained in:
commit
fdb5740ecf
|
@ -51,23 +51,21 @@ def getid(obj):
|
|||
def filter_kwargs(f):
|
||||
@functools.wraps(f)
|
||||
def func(*args, **kwargs):
|
||||
for key, ref in kwargs.items():
|
||||
new_kwargs = {}
|
||||
for key, ref in six.iteritems(kwargs):
|
||||
if ref is None:
|
||||
# drop null values
|
||||
del kwargs[key]
|
||||
continue
|
||||
|
||||
id_value = getid(ref)
|
||||
if id_value == ref:
|
||||
continue
|
||||
if id_value != ref:
|
||||
# If an object with an id was passed, then use the id, e.g:
|
||||
# user: user(id=1) becomes user_id: 1
|
||||
key = '%s_id' % key
|
||||
|
||||
# if an object with an id was passed remove the object
|
||||
# from params and replace it with just the id.
|
||||
# e.g user: User(id=1) becomes user_id: 1
|
||||
del kwargs[key]
|
||||
kwargs['%s_id' % key] = id_value
|
||||
new_kwargs[key] = id_value
|
||||
|
||||
return f(*args, **kwargs)
|
||||
return f(*args, **new_kwargs)
|
||||
return func
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue