Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from postatum/92308124_update_dict_from_querys…
Browse files Browse the repository at this point in the history
…tring

Allow Patch/Put of dict/list field using query string
  • Loading branch information
jstoiko committed Apr 27, 2015
2 parents 86d2ff8 + ea3fc2b commit 3b63195
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion nefertari/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,27 @@ class BaseView(object):
_json_encoder = None
_model_class = None

@staticmethod
def convert_dotted(params):
""" Convert dotted keys in :params: dictset to a nested dictset.
E.g. {'settings.foo': 'bar'} -> {'settings': {'foo': 'bar'}}
"""
dotted = defaultdict(dict)
dotted_items = {k: v for k, v in params.items() if '.' in k}

if dotted_items:
for key, value in dotted_items.items():
field, subfield = key.split('.')
dotted[field].update({subfield: value})
params = params.subset(['-' + k for k in dotted_items.keys()])
params.update(dict(dotted))

return params

def __init__(self, context, request, _params={}):
self.context = context
self.request = request

self._params = dictset(_params or request.params.mixed())

ctype = request.content_type
Expand All @@ -81,6 +98,8 @@ def __init__(self, context, request, _params={}):
"Expecting JSON. Received: '{}'. Request: {} {}".format(
request.body, request.method, request.url))

self._params = BaseView.convert_dotted(self._params)

# dict of the callables {'action':[callable1, callable2..]}
# as name implies, before calls are executed before the action is called
# after_calls are called after the action returns.
Expand Down

0 comments on commit 3b63195

Please sign in to comment.