Skip to content

Commit

Permalink
Fix inconsistency in cornice.service.decorate_view() arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Dec 14, 2016
1 parent 3b4c0ae commit e2c79f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Expand Up @@ -12,6 +12,12 @@ CHANGELOG
`Access-Control-Request-Headers` header correctly event if its value
contains zero number of white spaces between commas (#422)

**Internal changes**

- Clean-up an inconsistency in ``cornice.service.decorate_view()`` function
where ``acl`` and ``factory`` were expected as view arguments (whereas
deprecated since 1.0)


2.2.0 (2016-11-25)
==================
Expand Down
8 changes: 1 addition & 7 deletions cornice/pyramidhook.py
Expand Up @@ -213,18 +213,12 @@ def register_service_views(config, service):
if service.cors_enabled:
args['validators'].insert(0, cors_validator)

decorated_view = decorate_view(view, dict(args), method)
decorated_view = decorate_view(view, dict(args), method, route_args)

for item in cornice_parameters:
if item in args:
del args[item]

# These attributes are used in routes not views
deprecated_attrs = ['acl', 'factory', 'traverse']
for attr in deprecated_attrs:
if attr in args:
args.pop(attr)

# filter predicates defined on Resource
route_predicates = config.get_predlist('route').sorter.names
view_predicates = config.get_predlist('view').sorter.names
Expand Down
10 changes: 8 additions & 2 deletions cornice/service.py
Expand Up @@ -279,6 +279,11 @@ def add_view(self, method, view, **kwargs):
view = _UnboundView(kwargs['klass'], view)

args = self.get_arguments(kwargs)

# These attributes were used in views in Cornice < 1.0.
deprecated_attrs = ('acl', 'factory', 'traverse')
args = {k: v for k, v in args.items() if k not in deprecated_attrs}

if hasattr(self, 'get_view_wrapper'):
view = self.get_view_wrapper(kwargs)(view)
self.definitions.append((method, view, args))
Expand Down Expand Up @@ -443,7 +448,7 @@ def cors_max_age_for(self, method=None):
return max_age


def decorate_view(view, args, method):
def decorate_view(view, args, method, route_args={}):
"""Decorate a given view with cornice niceties.
This function returns a function with the same signature than the one
Expand All @@ -452,6 +457,7 @@ def decorate_view(view, args, method):
:param view: the view to decorate
:param args: the args to use for the decoration
:param method: the HTTP method
:param route_args: the args used for the associated route
"""
def wrapper(request):
# if the args contain a klass argument then use it to resolve the view
Expand All @@ -460,7 +466,7 @@ def wrapper(request):
view_ = view
if 'klass' in args and not callable(view):
params = dict(request=request)
if 'factory' in args and 'acl' not in args:
if 'factory' in route_args and 'acl' not in route_args:
params['context'] = request.context
ob = args['klass'](**params)
if is_string(view):
Expand Down
7 changes: 4 additions & 3 deletions tests/test_service.py
Expand Up @@ -505,10 +505,11 @@ def dummy_view(request):

def test_decorate_view_factory(self):

args = {'factory': u'TheFactoryMethodCalledByPyramid',
'klass': DummyAPI}
args = {'klass': DummyAPI}
route_args = {'factory': u'TheFactoryMethodCalledByPyramid'}

decorated_view = decorate_view('collection_get', args, 'GET')
decorated_view = decorate_view('collection_get', args,
'GET', route_args)
dummy_request = DummyRequest()
ret = decorated_view(dummy_request)
self.assertEqual(ret, ['douggy', 'rusty'])
Expand Down

0 comments on commit e2c79f5

Please sign in to comment.