Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def apply_filters(request, response):
if request.matched_route is not None:
# do some sanity checking on the response using filters
services = request.registry.cornice_services
pattern = request.matched_route.pattern
service = services.get(pattern, None)
if service is not None:
kwargs, ob = getattr(request, "cornice_args", ({}, None))
for _filter in kwargs.get('filters', []):
if is_string(_filter) and ob is not None:
_filter = getattr(ob, _filter)
try:
response = _filter(response, request)
except TypeError:
response = _filter(response)
if service.cors_enabled:
apply_cors_post_request(service, request, response)
return response
# ob = args['klass'](request)
params = dict(request=request)
if 'factory' in route_args:
params['context'] = request.context
ob = args['klass'](**params)
if is_string(view):
view_ = getattr(ob, view.lower())
elif isinstance(view, _UnboundView):
view_ = view.make_bound_view(ob)
# the validators can either be a list of callables or contain some
# non-callable values. In which case we want to resolve them using the
# object if any
validators = args.get('validators', ())
for validator in validators:
if is_string(validator) and ob is not None:
validator = getattr(ob, validator)
validator(request, **args)
# only call the view if we don't have validation errors
if len(request.errors) == 0:
try:
# If we have an object, it already has the request.
if ob:
response = view_()
else:
response = view_(request)
except Exception:
# cors headers need to be set if an exception was raised
request.info['cors_checked'] = False
raise
def wrapper(request):
# if the args contain a klass argument then use it to resolve the view
# location (if the view argument isn't a callable)
ob = None
view_ = view
if 'klass' in args and not callable(view):
# XXX: given that request.context exists and root-factory
# only expects request param, having params seems unnecessary
# ob = args['klass'](request)
params = dict(request=request)
if 'factory' in route_args:
params['context'] = request.context
ob = args['klass'](**params)
if is_string(view):
view_ = getattr(ob, view.lower())
elif isinstance(view, _UnboundView):
view_ = view.make_bound_view(ob)
# the validators can either be a list of callables or contain some
# non-callable values. In which case we want to resolve them using the
# object if any
validators = args.get('validators', ())
for validator in validators:
if is_string(validator) and ob is not None:
validator = getattr(ob, validator)
validator(request, **args)
# only call the view if we don't have validation errors
if len(request.errors) == 0:
try:
def _resolve_obj_to_docstring(self, obj, args):
# Resolve a view or validator to an object if type string
# and return docstring.
if is_string(obj):
if 'klass' in args:
ob = args['klass']
obj_ = getattr(ob, obj.lower())
return format_docstring(obj_)
else:
return ''
else:
return format_docstring(obj)
return args['error_handler'](request)
# if the view returns its own response, cors headers need to be set
if isinstance(response, Response):
request.info['cors_checked'] = False
# We can't apply filters at this level, since "response" may not have
# been rendered into a proper Response object yet. Instead, give the
# request a reference to its api_kwargs so that a tween can apply them.
# We also pass the object we created (if any) so we can use it to find
# the filters that are in fact methods.
request.cornice_args = (args, ob)
return response
# return the wrapper, not the function, keep the same signature
if not is_string(view):
functools.update_wrapper(wrapper, view)
# Set the wrapper name to something useful
wrapper.__name__ = "{0}__{1}".format(func_name(view), method)
return wrapper