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 #44 from postatum/94290158_check_fields_put
Browse files Browse the repository at this point in the history
Process PUT in 'replace' method. Fill params with nulls when PUT
  • Loading branch information
jstoiko committed May 25, 2015
2 parents 9135c3b + a9e5673 commit a1931f8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nefertari/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def add_route_and_view(config, action, route_name, path, request_method,
'GET', traverse=_traverse)

add_route_and_view(
config, 'update', name_prefix + member_name, path + id_name,
config, 'replace', name_prefix + member_name, path + id_name,
'PUT', traverse=_traverse)

add_route_and_view(
Expand Down
28 changes: 24 additions & 4 deletions nefertari/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ def _run_init_actions(self):
self.setup_default_wrappers()
self.convert_ids2objects()
self.set_public_limits()
if self.request.method == 'PUT':
self.fill_null_values()

def fill_null_values(self, model_cls=None):
""" Fill missing model fields in JSON with {key: None}.
Only run for PUT requests.
"""
if model_cls is None:
model_cls = self._model_class
if not model_cls:
log.info("%s has no model defined" % self.__class__.__name__)
return

empty_values = model_cls.get_null_values()
for field, value in empty_values.items():
if field not in self._json_params:
self._json_params[field] = value

def set_public_limits(self):
""" Set public limits if auth is enabled and user is not
Expand All @@ -152,20 +170,22 @@ def set_public_limits(self):
if auth_enabled and not getattr(self.request, 'user', None):
wrappers.set_public_limits(self)

def convert_ids2objects(self):
def convert_ids2objects(self, model_cls=None):
""" Convert object IDs from `self._json_params` to objects if needed.
Only IDs tbat belong to relationship field of `self._model_class`
are converted.
"""
if not self._model_class:
if model_cls is None:
model_cls = self._model_class
if not model_cls:
log.info("%s has no model defined" % self.__class__.__name__)
return

for field in self._json_params.keys():
if not engine.is_relationship_field(field, self._model_class):
if not engine.is_relationship_field(field, model_cls):
continue
model_cls = engine.get_relationship_cls(field, self._model_class)
model_cls = engine.get_relationship_cls(field, model_cls)
self.id2obj(field, model_cls)

def get_debug(self, package=None):
Expand Down
15 changes: 13 additions & 2 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __getattr__(self, attr):
def convert_ids2objects(self, *args, **kwargs):
pass

def fill_null_values(self, *args, **kwargs):
pass

return View


Expand Down Expand Up @@ -218,6 +221,10 @@ def test_get_member(self):

def test_put_member(self):
result = self.app.put('/messages/1').body
self.assertEqual(result, 'replace')

def test_patch_member(self):
result = self.app.patch('/messages/1').body
self.assertEqual(result, 'update')

def test_delete_member(self):
Expand Down Expand Up @@ -303,11 +310,15 @@ def test_singular_resource(self, *a):

self.assertEqual(app.delete('/grandpas/1').body, '"delete"')

self.assertEqual(app.put('/thing').body, '"update"')
self.assertEqual(app.put('/thing').body, '"replace"')

self.assertEqual(app.patch('/thing').body, '"update"')

self.assertEqual(app.delete('/thing').body, '"delete"')

self.assertEqual(app.put('/grandpas/1/wife').body, '"update"')
self.assertEqual(app.put('/grandpas/1/wife').body, '"replace"')

self.assertEqual(app.patch('/grandpas/1/wife').body, '"update"')

self.assertEqual(app.delete('/grandpas/1/wife').body, '"delete"')

Expand Down
15 changes: 15 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ def test_run_init_actions(self, limit, conv, setpub):
conv.assert_called_once_with()
setpub.assert_called_once_with()

@patch('nefertari.view.BaseView._run_init_actions')
def test_fill_null_values(self, run):
request = Mock(content_type='', method='', accept=[''])
view = BaseView(
context={}, request=request,
_query_params={'foo': 'bar'})
view._model_class = Mock()
view._model_class.get_null_values.return_value = {
'name': None, 'email': 1, 'foo': None}
view._json_params = {'foo': 'bar'}
view.fill_null_values()
assert view._json_params == {
'foo': 'bar', 'name': None, 'email': 1
}

@patch('nefertari.view.wrappers')
@patch('nefertari.view.BaseView._run_init_actions')
def test_set_public_limits_no_root(self, run, wrap):
Expand Down

0 comments on commit a1931f8

Please sign in to comment.