Example #1
0
    def _get_format(self, request):
        """
        Determine and return a 'formats.Format' instance describing the most desired response format
        that is supported by these views.

        Formats specified by extension (e.g. '/articles/index.html') take precedence over formats
        given in the HTTP Accept header, even if it's a format that isn't known by Respite.

        If the request doesn't specify a format by extension (e.g. '/articles/' or '/articles/new')
        and none of the formats in the HTTP Accept header are supported, Respite will fall back
        on the format given in DEFAULT_FORMAT.

        Arguments:
        request -- The request object.
        """

        # Derive a list of 'formats.Format' instances from the list of formats these views support.
        supported_formats = [formats.find(format) for format in self.supported_formats]

        # Determine format by extension...
        if '.' in request.path:
            extension = request.path.split('.')[-1]

            try:
                format = formats.find_by_extension(extension)
            except formats.UnknownFormat:
                return None

            return format if format in supported_formats else None

        # Determine format by HTTP Accept header...
        if 'HTTP_ACCEPT' in request.META:

            # Parse the HTTP Accept header, returning a list of accepted content types sorted by quality
            for accepted_content_type in parse_http_accept_header(request.META['HTTP_ACCEPT']):

                # Default to the format given in DEFAULT_FORMAT for the '*/*' content type.
                if accepted_content_type == '*/*' and DEFAULT_FORMAT:
                    default_format = formats.find(DEFAULT_FORMAT)
                    if default_format in supported_formats:
                        return default_format

                try:
                    format = formats.find_by_content_type(accepted_content_type)
                except formats.UnknownFormat:
                    continue

                if format in supported_formats:
                    return format
                else:
                    continue

        # If none of the formats given in the HTTP 'accept' header are supported by these views,
        # or no HTTP 'accept' header was given at all, default to the format given in
        # DEFAULT_FORMAT if that's supported.
        if DEFAULT_FORMAT:
            default_format = formats.find(DEFAULT_FORMAT)
            if default_format in supported_formats:
                return default_format
Example #2
0
    def _get_format(self, request):
        """
        Determine and return a 'formats.Format' instance describing the most desired response format
        that is supported by these views.

        :param request: A django.http.HttpRequest instance.

        Formats specified by extension (e.g. '/articles/index.html') take precedence over formats
        given in the HTTP Accept header, even if it's a format that isn't known by Respite.

        If the request doesn't specify a format by extension (e.g. '/articles/' or '/articles/new')
        and none of the formats in the HTTP Accept header are supported, Respite will fall back
        on the format given in DEFAULT_FORMAT.
        """

        # Derive a list of 'formats.Format' instances from the list of formats these views support.
        supported_formats = [formats.find(format) for format in self.supported_formats]

        # Determine format by extension...
        if '.' in request.path:
            extension = request.path.split('.')[-1]

            try:
                format = formats.find_by_extension(extension)
            except formats.UnknownFormat:
                return None

            if format in supported_formats:
                return format
            else:
                return None

        # Determine format by HTTP Accept header...
        if 'HTTP_ACCEPT' in request.META:
            content_types = parse_http_accept_header(request.META['HTTP_ACCEPT'])

            for content_type in content_types:
                # If the request has no preference as to the format of its response, prefer the
                # first of the view's supported formats.
                if content_type == '*/*':
                    return supported_formats[0]

                try:
                    format = formats.find_by_content_type(content_type)
                except formats.UnknownFormat:
                    pass

                if format in supported_formats:
                    return format
                else:
                    pass
            return None

        # If no format is given by either extension or header, default to the format given in
        # RESPITE_DEFAULT_FORMAT (given, of course, that it's supported by the view).
        if DEFAULT_FORMAT:
            format = formats.find(DEFAULT_FORMAT)

            if format in supported_formats:
                return format
            else:
                return None
Example #3
0
    def _get_format(self, request):
        """
        Determine and return a 'formats.Format' instance describing the most desired response format
        that is supported by these views.

        :param request: A django.http.HttpRequest instance.

        Formats specified by extension (e.g. '/articles/index.html') take precedence over formats
        given in the HTTP Accept header, even if it's a format that isn't known by Respite.

        If the request doesn't specify a format by extension (e.g. '/articles/' or '/articles/new')
        and none of the formats in the HTTP Accept header are supported, Respite will fall back
        on the format given in DEFAULT_FORMAT.
        """

        # Derive a list of 'formats.Format' instances from the list of formats these views support.
        supported_formats = [
            formats.find(format) for format in self.supported_formats
        ]

        # Determine format by extension...
        if '.' in request.path:
            extension = request.path.split('.')[-1]

            try:
                format = formats.find_by_extension(extension)
            except formats.UnknownFormat:
                return None

            if format in supported_formats:
                return format
            else:
                return None

        # Determine format by HTTP Accept header...
        if 'HTTP_ACCEPT' in request.META:
            content_types = parse_http_accept_header(
                request.META['HTTP_ACCEPT'])

            for content_type in content_types:
                # If the request has no preference as to the format of its response, prefer the
                # first of the view's supported formats.
                if content_type == '*/*':
                    return supported_formats[0]

                try:
                    format = formats.find_by_content_type(content_type)
                except formats.UnknownFormat:
                    pass

                if format in supported_formats:
                    return format
                else:
                    pass
            return None

        # If no format is given by either extension or header, default to the format given in
        # RESPITE_DEFAULT_FORMAT (given, of course, that it's supported by the view).
        if DEFAULT_FORMAT:
            format = formats.find(DEFAULT_FORMAT)

            if format in supported_formats:
                return format
            else:
                return None
Example #4
0
def test_find_by_extension():
    format = formats.find_by_extension('html')

    assert_equal('HyperText Markup Language', format.name)