Exemplo n.º 1
0
    def render(self, request, context, status):
        acceptable_types = get_acceptable_types(request)

        native_type = None
        if "native_mediatype" in context:
            native_type = context["native_mediatype"]

        acceptable_languages = get_acceptable_languages(request)

        output_type = None
        if acceptable_types == "application/json+html":
            acceptable_types = "application/json"
            output_type = "text/html"

        language, mediatype, renderer = self.select_renderer(acceptable_languages, native_type, acceptable_types)

        if not output_type:
            output_type = mediatype

        if not renderer:
            raise HttpException(
                406,
                "There were no representations deemed acceptable given '%s' in '%s'."
                % (acceptable_types, acceptable_languages),
            )
        else:
            if "-" in language:
                context["language"], context["dialect"] = language.split("-")
            else:
                context["language"] = language
                favorite_language = LanguageAcceptor(acceptable_languages).ranges[0]
                if favorite_language.type == language and favorite_language.subtype:
                    context["dialect"] = favorite_language.subtype

            content = renderer(request, context)

            if "__charset__" in context and context["__charset__"]:
                mediatype += "; charset=" + context["__charset__"]

            response = HttpResponse(content, status=status, content_type=output_type)

            response["Content-Language"] = context["language"] + (
                "-" + context["dialect"] if "dialect" in context else ""
            )

            if "__content_disposition__" in context and context["__content_disposition__"]:
                response["Content-Disposition"] = context["__content_disposition__"]

            return response
Exemplo n.º 2
0
    def render(self, request, context, status):
        acceptable_types = get_acceptable_types(request)

        native_type = None
        if "native_mediatype" in context:
            native_type = context["native_mediatype"]

        acceptable_languages = get_acceptable_languages(request)

        output_type = None
        if acceptable_types == "application/json+html":
            acceptable_types = "application/json"
            output_type = "text/html"

        language, mediatype, renderer = self.select_renderer(acceptable_languages,
                                                             native_type,
                                                             acceptable_types)

        if not output_type:
            output_type = mediatype

        if not renderer:
            raise HttpException(406,
                                "There were no representations deemed acceptable given '%s' in '%s'." %
                                (acceptable_types, acceptable_languages))
        else:
            if "-" in language:
                context["language"], context["dialect"] = language.split("-")
            else:
                context["language"] = language
                favorite_language = LanguageAcceptor(acceptable_languages).ranges[0]
                if favorite_language.type == language and favorite_language.subtype:
                    context["dialect"] = favorite_language.subtype

            content = renderer(request, context)

            if "__charset__" in context and context["__charset__"]:
                mediatype += "; charset=" + context["__charset__"]

            response = HttpResponse(content, status=status, mimetype=output_type)

            response["Content-Language"] = (context["language"] +
                                            ("-" + context["dialect"] if "dialect" in context else ""))

            if "__content_disposition__" in context and context["__content_disposition__"]:
                response["Content-Disposition"] = context["__content_disposition__"]

            return response
Exemplo n.º 3
0
    def dispatch(cls, request, *args, **kwargs):
        resource = cls()

        try:
            method = getattr(resource, request.method)
        except AttributeError as e:
            return HttpException(405, "%s is not supported on %s." % (request.method, request.path),
                                 headers = {"Allow": ", ".join(cls.allowed_methods()) }).as_http_response()

        try:
            return method(request, *args, **kwargs)
        except HttpException as e:
            if "application/json+html" in get_acceptable_types(request):
                return HttpResponse(status=httplib.OK, mimetype="text/html",
                                    content=json.dumps({ "success": False, "message": e.response }))
            return e.as_http_response()