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 #125 from postatum/fix_nested_parent_issue
Browse files Browse the repository at this point in the history
Fix nested parent issue
  • Loading branch information
jstoiko committed Dec 9, 2015
2 parents 0af529c + fec5627 commit 89a644e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 10 additions & 3 deletions nefertari/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def __call__(self, **kwargs):
class add_object_url(object):
""" Add '_self' to each object in results
For each object in `result['data']` adds a uri which points
to current object
For each object in `result['data']` fetches a uri from pyramid
which points to current object
"""
def __init__(self, request):
self.request = request
Expand All @@ -301,16 +301,23 @@ def _set_object_self(self, obj):
""" Add '_self' key value to :obj: dict. """
from nefertari.elasticsearch import ES
location = self.request.path_url
route_kwargs = {}

""" Check for parents """
if self.request.matchdict:
route_kwargs.update(self.request.matchdict)
try:
type_, obj_pk = obj['_type'], obj['_pk']
except KeyError:
return
resource = (self.model_collections.get(type_) or
self.model_collections.get(ES.src2type(type_)))
if resource is not None:
route_kwargs.update({resource.id_name: obj_pk})
location = self.request.route_url(
resource.uid, **{resource.id_name: obj_pk})
resource.uid, **route_kwargs)
obj.setdefault('_self', location)
log.info('Added `_self` attr: {}'.format(location))

def __call__(self, **kwargs):
result = kwargs['result']
Expand Down
16 changes: 14 additions & 2 deletions tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_add_object_url_collection_no_type(self):

def test_add_object_url_collection(self):
result = {'data': [{'_pk': 4, '_type': 'Story'}]}
request = Mock()
request = Mock(matchdict=None)
wrapper = wrappers.add_object_url(request=request)
wrapper.model_collections = {
'Story': Mock(uid='stories_resource', id_name='story_id'),
Expand All @@ -138,7 +138,7 @@ def test_add_object_url_collection(self):

def test_add_object_url_item(self):
result = {'_pk': 4, '_type': 'Story'}
request = Mock()
request = Mock(matchdict=None)
wrapper = wrappers.add_object_url(request=request)
wrapper.model_collections = {
'Story': Mock(uid='stories_resource', id_name='story_id'),
Expand All @@ -148,6 +148,18 @@ def test_add_object_url_item(self):
'stories_resource', story_id=4)
assert result['_self'] == request.route_url()

def test_add_object_url_with_parent(self):
result = {'_pk': 4, '_type': 'Story'}
request = Mock(matchdict={'user_username': 'admin'})
wrapper = wrappers.add_object_url(request=request)
wrapper.model_collections = {
'Story': Mock(uid='stories_resource', id_name='story_id'),
}
result = wrapper(result=result)
request.route_url.assert_called_once_with(
'stories_resource', user_username='admin', story_id=4)
assert result['_self'] == request.route_url()

@patch('nefertari.utils.validate_data_privacy')
def test_apply_request_privacy_valid(self, mock_validate):
wrapper = wrappers.apply_request_privacy(
Expand Down

0 comments on commit 89a644e

Please sign in to comment.