Exemple #1
0
    def _fallback_view(request):
        # Maybe we failed to match any definitions for the request method?
        if request.method not in service.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = service.defined_methods
            return response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        for method, _, args in service.definitions:
            if method != request.method:
                continue
            if 'accept' in args:
                acceptable.extend(
                        service.get_acceptable(method, filter_callables=True))
                if 'acceptable' in request.info:
                    for content_type in request.info['acceptable']:
                        if content_type not in acceptable:
                            acceptable.append(content_type)

                # Now check if that was actually the source of the problem.
                if not request.accept.best_match(acceptable):
                    response = HTTPNotAcceptable()
                    response.content_type = "application/json"
                    response.body = json.dumps(acceptable)
                    return response

        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(service.name)
Exemple #2
0
 def predicate_wrapper(context, request):
     for predicate in preds:
         if not predicate(context, request):
             view_name = getattr(view, '__name__', view)
             raise PredicateMismatch('predicate mismatch for view %s (%s)' %
                                     (view_name, predicate.text()))
     return view(context, request)
Exemple #3
0
    def _fallback_view(request):
        # Maybe we failed to match any definitions for the request method?
        if request.method not in service.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = service.defined_methods
            raise response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        supported_contenttypes = []
        for method, _, args in service.definitions:
            if method != request.method:
                continue

            if 'accept' in args:
                acceptable.extend(
                    service.get_acceptable(method, filter_callables=True))
                acceptable.extend(
                    request.info.get('acceptable', []))
                acceptable = list(set(acceptable))

                # Now check if that was actually the source of the problem.
                if not request.accept.best_match(acceptable):
                    request.errors.add(
                        'header', 'Accept',
                        'Accept header should be one of {0}'.format(
                            acceptable).encode('ascii'))
                    request.errors.status = HTTPNotAcceptable.code
                    error = service.error_handler(request)
                    raise error

            if 'content_type' in args:
                supported_contenttypes.extend(
                    service.get_contenttypes(method,
                                             filter_callables=True))
                supported_contenttypes.extend(
                    request.info.get('supported_contenttypes', []))
                supported_contenttypes = list(set(supported_contenttypes))

                # Now check if that was actually the source of the problem.
                if not content_type_matches(request, supported_contenttypes):
                    request.errors.add(
                        'header', 'Content-Type',
                        'Content-Type header should be one of {0}'.format(
                            supported_contenttypes).encode('ascii'))
                    request.errors.status = HTTPUnsupportedMediaType.code
                    error = service.error_handler(request)
                    raise error

        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.

        # After "custom predicates" feature has been added there is no need in
        # this line. Instead requests will be filtered by  "custom predicates"
        # feature filter and exception "404 Not found" error will be raised. In
        # order to avoid unpredictable cases, we left this line in place and
        # excluded it from coverage.
        raise PredicateMismatch(service.name)  # pragma: no cover
Exemple #4
0
def render_snippet(context, request):
    name = request.POST['slot_name']
    view_name = 'kottisnippets-view-%s' % name
    response = render_view_to_response(context, request, name=view_name)
    if response is None:
        view_name = 'kottisnippets-view'
        response = render_view_to_response(context, request, name=view_name)
    if response is None:
        raise PredicateMismatch()
    return response
Exemple #5
0
    def _fallback_view(request):
        # Maybe we failed to match any definitions for the request method?
        if request.method not in service.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = service.defined_methods
            raise response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        supported_contenttypes = []
        for method, _, args in service.definitions:
            if method != request.method:
                continue

            if 'accept' in args:
                acceptable.extend(
                    service.get_acceptable(method, filter_callables=True))
                acceptable.extend(
                    request.info.get('acceptable', []))
                acceptable = list(set(acceptable))

                # Now check if that was actually the source of the problem.
                if not request.accept.best_match(acceptable):
                    request.errors.add(
                        'header', 'Accept',
                        'Accept header should be one of {0}'.format(
                            acceptable).encode('ascii'))
                    request.errors.status = HTTPNotAcceptable.code
                    error = service.error_handler(request.errors)
                    raise error

            if 'content_type' in args:
                supported_contenttypes.extend(
                    service.get_contenttypes(method,
                                             filter_callables=True))
                supported_contenttypes.extend(
                    request.info.get('supported_contenttypes', []))
                supported_contenttypes = list(set(supported_contenttypes))

                # Now check if that was actually the source of the problem.
                if not content_type_matches(request, supported_contenttypes):
                    request.errors.add(
                        'header', 'Content-Type',
                        'Content-Type header should be one of {0}'.format(
                            supported_contenttypes).encode('ascii'))
                    request.errors.status = HTTPUnsupportedMediaType.code
                    error = service.error_handler(request.errors)
                    raise error

        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(service.name)
Exemple #6
0
def render_list(context, request):
    request.orig_context = context
    name = request.POST['slot_name']
    if hasattr(context, 'slots'):
        for slot in context.slots:
            if slot.name == name and slot.snippets:
                snippets = request.snippets = list(slot.snippets)
                view_name = 'kottisnippets-list-%s' % name
                response = render_view_to_response(snippets,
                                                   request,
                                                   name=view_name)
                if response is None:
                    view_name = 'kottisnippets-list'
                    response = render_view_to_response(snippets,
                                                       request,
                                                       name=view_name)
                if response is None:
                    request.snippets = slot.snippets
                    return {
                        'slot_name': name,
                        'snippets': slot.snippets,
                    }
                return response
    raise PredicateMismatch()
Exemple #7
0
    def _fallback_view(self, request):
        """Fallback view for this service, called when nothing else matches.

        This method provides the view logic to be executed when the request
        does not match any explicitly-defined view.  Its main responsibility
        is to produce an accurate error response, such as HTTPMethodNotAllowed
        or HTTPNotAcceptable.
        """
        # Maybe we failed to match any definitions for the request method?
        if request.method not in self.defined_methods:
            response = HTTPMethodNotAllowed()
            response.allow = self.defined_methods
            return response
        # Maybe we failed to match an acceptable content-type?
        # First search all the definitions to find the acceptable types.
        # XXX: precalculate this like the defined_methods list?
        acceptable = []
        for api_kwargs in self.definitions:
            if api_kwargs['request_method'] != request.method:
                continue
            if 'accept' in api_kwargs:
                accept = to_list(api_kwargs.get('accept'))
                acceptable.extend(a for a in accept if not callable(a))
                if 'acceptable' in request.info:
                    for content_type in request.info['acceptable']:
                        if content_type not in acceptable:
                            acceptable.append(content_type)
        # Now check if that was actually the source of the problem.
        if not request.accept.best_match(acceptable):
            response = HTTPNotAcceptable()
            response.content_type = "application/json"
            response.body = json.dumps(acceptable)
            return response
        # In the absence of further information about what went wrong,
        # let upstream deal with the mismatch.
        raise PredicateMismatch(self.name)
Exemple #8
0
 def __call__(self, context, request):
     layout = self.choose_layout(context)
     if layout is None:
         raise PredicateMismatch("No layout matches containment for %s." %
                                 type(context))
     return layout(context, request)