예제 #1
0
파일: view.py 프로젝트: mbijon/nefertari
    def setup_default_wrappers(self):
        """ Setup defaulf wrappers.

        Wrappers are applied when view method does not return instance
        of Response. In this case nefertari renderers call wrappers and
        handle response generation.

        Note: It's important for `add_etag` wrapper be applied before
        `apply_privacy` as later may remove response data that
        is used to generate etag
        """
        # Index
        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
            wrappers.add_etag(self.request),
        ]

        # Show
        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Create
        self._after_calls['create'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Update
        self._after_calls['update'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Replace
        self._after_calls['replace'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Privacy wrappers
        if self._auth_enabled:
            for meth in ('index', 'show', 'create', 'update', 'replace'):
                self._after_calls[meth] += [
                    wrappers.apply_privacy(self.request),
                ]
            for meth in ('update', 'replace', 'update_many'):
                self._before_calls[meth] += [
                    wrappers.apply_request_privacy(
                        self.Model, self._json_params),
                ]
예제 #2
0
    def setup_default_wrappers(self):
        """ Setup defaulf wrappers.

        Wrappers are applied when view method does not return instance
        of Response. In this case nefertari renderers call wrappers and
        handle response generation.

        Note: It's important for `add_etag` wrapper be applied before
        `apply_privacy` as later may remove response data that
        is used to generate etag
        """
        # Index
        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
            wrappers.add_etag(self.request),
        ]

        # Show
        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Create
        self._after_calls['create'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Update
        self._after_calls['update'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Replace
        self._after_calls['replace'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Privacy wrappers
        if self._auth_enabled:
            for meth in ('index', 'show', 'create', 'update', 'replace'):
                self._after_calls[meth] += [
                    wrappers.apply_privacy(self.request),
                ]
            for meth in ('update', 'replace', 'update_many'):
                self._before_calls[meth] += [
                    wrappers.apply_request_privacy(self.Model,
                                                   self._json_params),
                ]
예제 #3
0
 def test_add_object_url_collection_no_type(self):
     result = {'data': [{'_pk': 4}]}
     request = DummyRequest(path='http://example.com', environ={})
     wrapper = wrappers.add_object_url(request=request)
     wrapper.model_collections = {'Story': 123}
     result = wrapper(result=result)
     assert '_self' not in result['data'][0]
예제 #4
0
 def test_add_object_url_collection_no_type(self):
     result = {"data": [{"_pk": 4}]}
     request = DummyRequest(path="http://example.com", environ={})
     wrapper = wrappers.add_object_url(request=request)
     wrapper.model_collections = {"Story": 123}
     result = wrapper(result=result)
     assert "_self" not in result["data"][0]
예제 #5
0
 def test_add_object_url_collection_no_type(self):
     result = {'data': [{'_pk': 4}]}
     request = DummyRequest(path='http://example.com', environ={})
     wrapper = wrappers.add_object_url(request=request)
     wrapper.model_collections = {'Story': 123}
     result = wrapper(result=result)
     assert '_self' not in result['data'][0]
예제 #6
0
 def test_add_object_url_collection_not_found_resource(self):
     result = {'data': [{'_pk': 4, '_type': 'User'}]}
     environ = {'QUERY_STRING': '_limit=100'}
     request = DummyRequest(path='http://example.com', environ=environ)
     wrapper = wrappers.add_object_url(request=request)
     wrapper.model_collections = {'Story': 123}
     result = wrapper(result=result)
     assert result['data'][0]['_self'] == 'http://example.com'
예제 #7
0
 def test_add_object_url_with_parent(self):
     result = {"_pk": 4, "_type": "Story"}
     request = Mock(matchdict={"user_username": "******"})
     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="******", story_id=4)
     assert result["_self"] == request.route_url()
예제 #8
0
 def test_add_object_url_collection(self):
     result = {"data": [{"_pk": 4, "_type": "Story"}]}
     request = Mock(matchdict=None)
     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", story_id=4)
     assert result["data"][0]["_self"] == request.route_url()
예제 #9
0
 def test_add_object_url_collection_not_found_resource(self):
     result = {"data": [{"_pk": 4, "_type": "User"}]}
     environ = {"QUERY_STRING": "_limit=100"}
     request = DummyRequest(path="http://example.com", environ=environ)
     wrapper = wrappers.add_object_url(request=request)
     wrapper.model_collections = {"Story": 123}
     result = wrapper(result=result)
     assert result["data"][0]["_self"] == "http://example.com"
예제 #10
0
 def test_add_object_url_collection_not_found_resource(self):
     result = {'data': [{'_pk': 4, '_type': 'User'}]}
     environ = {'QUERY_STRING': '_limit=100'}
     request = DummyRequest(path='http://example.com', environ=environ)
     wrapper = wrappers.add_object_url(request=request)
     wrapper.model_collections = {'Story': 123}
     result = wrapper(result=result)
     assert result['data'][0]['_self'] == 'http://example.com'
예제 #11
0
 def test_add_object_url_item(self):
     result = {'_pk': 4, '_type': 'Story'}
     request = Mock(matchdict=None)
     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',
                                               story_id=4)
     assert result['_self'] == request.route_url()
예제 #12
0
 def test_add_object_url_item(self):
     result = {'_pk': 4, '_type': 'Story'}
     request = Mock()
     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', story_id=4)
     assert result['_self'] == request.route_url()