Python rest_framework.exceptions.NotAcceptable() Examples

The following are 7 code examples of rest_framework.exceptions.NotAcceptable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.exceptions , or try the search function .
Example #1
Source File: mixins.py    From django-spillway with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle_exception(self, exc):
        response = super(ResponseExceptionMixin, self).handle_exception(exc)
        renderers = tuple(api_settings.DEFAULT_RENDERER_CLASSES)
        accepted = getattr(self.request, 'accepted_renderer', None)
        if (response.exception and accepted
                and not isinstance(accepted, renderers)):
            conneg = self.get_content_negotiator()
            try:
                render_cls, mtype = conneg.select_renderer(
                    self.request, renderers, self.format_kwarg)
            except exceptions.NotAcceptable:
                render_cls = TemplateHTMLRenderer
                mtype = render_cls.media_type
            self.request.accepted_renderer = render_cls()
            self.request.accepted_media_type = mtype
        return response 
Example #2
Source File: views.py    From lego with MIT License 5 votes vote down vote up
def validate_answer(self, request, **kwargs):
        submission = Submission.objects.get(pk=kwargs["pk"])
        answer_pk = request.query_params.get("answer")
        if answer_pk is None:
            raise exceptions.NotAcceptable("No answer specified")
        try:
            answer = submission.answers.get(pk=answer_pk)
        except Answer.DoesNotExist:
            raise exceptions.NotFound("Answer not found")
        if answer.question.question_type != TEXT_FIELD:
            raise exceptions.NotAcceptable("Only text answers can be hidden")
        return submission, answer 
Example #3
Source File: generics.py    From django-spillway with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def options(self, request, *args, **kwargs):
        if isinstance(self.request.accepted_renderer,
                      renderers.gdal.BaseGDALRenderer):
            raise exceptions.NotAcceptable
        return super(BaseRasterView, self).options(request, *args, **kwargs) 
Example #4
Source File: decorators.py    From GloboNetworkAPI with Apache License 2.0 5 votes vote down vote up
def raise_exception_treat(func):
    @functools.wraps(func)
    def inner(self, request, *args, **kwargs):
        try:
            return func(self, request, *args, **kwargs)
        except ValidationError, error:
            log.error(error)
            raise rest_exceptions.ValidationExceptionJson(error)
        except (exceptions_api.APIException, exceptions_api.AuthenticationFailed,
                exceptions_api.MethodNotAllowed, exceptions_api.NotAcceptable,
                exceptions_api.NotAuthenticated, exceptions_api.ParseError,
                exceptions_api.PermissionDenied, exceptions_api.Throttled,
                exceptions_api.UnsupportedMediaType, rest_exceptions.ValidationAPIException), error:
            log.error(error)
            raise error 
Example #5
Source File: versioning.py    From Dailyfresh-B2C with Apache License 2.0 5 votes vote down vote up
def determine_version(self, request, *args, **kwargs):
        media_type = _MediaType(request.accepted_media_type)
        version = media_type.params.get(self.version_param, self.default_version)
        version = unicode_http_header(version)
        if not self.is_allowed_version(version):
            raise exceptions.NotAcceptable(self.invalid_version_message)
        return version

    # We don't need to implement `reverse`, as the versioning is based
    # on the `Accept` header, not on the request URL. 
Example #6
Source File: views.py    From polemarch with GNU Affero General Public License v3.0 5 votes vote down vote up
def clear(self, request, *args, **kwargs):
        '''
        Clear history output.
        '''
        default_message = "Output trancated.\n"
        obj = self.get_object()
        if obj.status in ["RUN", "DELAY"] or obj.raw_stdout == default_message:  # nocv
            raise excepts.NotAcceptable(
                "Job is running or already trancated"
            )
        obj.raw_stdout = default_message
        result = self.get_serializer(obj).get_raw(request)
        return base.Response(result, status.HTTP_204_NO_CONTENT).resp 
Example #7
Source File: negotiation.py    From Dailyfresh-B2C with Apache License 2.0 4 votes vote down vote up
def select_renderer(self, request, renderers, format_suffix=None):
        """
        Given a request and a list of renderers, return a two-tuple of:
        (renderer, media type).
        """
        # Allow URL style format override.  eg. "?format=json
        format_query_param = self.settings.URL_FORMAT_OVERRIDE
        format = format_suffix or request.query_params.get(format_query_param)

        if format:
            renderers = self.filter_renderers(renderers, format)

        accepts = self.get_accept_list(request)

        # Check the acceptable media types against each renderer,
        # attempting more specific media types first
        # NB. The inner loop here isn't as bad as it first looks :)
        #     Worst case is we're looping over len(accept_list) * len(self.renderers)
        for media_type_set in order_by_precedence(accepts):
            for renderer in renderers:
                for media_type in media_type_set:
                    if media_type_matches(renderer.media_type, media_type):
                        # Return the most specific media type as accepted.
                        media_type_wrapper = _MediaType(media_type)
                        if (
                            _MediaType(renderer.media_type).precedence >
                            media_type_wrapper.precedence
                        ):
                            # Eg client requests '*/*'
                            # Accepted media type is 'application/json'
                            full_media_type = ';'.join(
                                (renderer.media_type,) +
                                tuple('{0}={1}'.format(
                                    key, value.decode(HTTP_HEADER_ENCODING))
                                    for key, value in media_type_wrapper.params.items()))
                            return renderer, full_media_type
                        else:
                            # Eg client requests 'application/json; indent=8'
                            # Accepted media type is 'application/json; indent=8'
                            return renderer, media_type

        raise exceptions.NotAcceptable(available_renderers=renderers)