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 test_content_types():
    from django.conf import settings
    from respite import formats

    response = client.get('/news/articles/1', HTTP_ACCEPT='*/*,application/json')
    assert response['Content-Type'] == formats.find(settings.RESPITE_DEFAULT_FORMAT).content_type

    response = client.get('/news/articles/1', HTTP_ACCEPT='unsupported/format, */*')
    assert response['Content-Type'] == formats.find(settings.RESPITE_DEFAULT_FORMAT).content_type
Example #3
0
def test_content_types():
    from django.conf import settings
    from respite import formats
    from .project.app.views import ArticleViews

    response = client.get('/news/articles/1', HTTP_ACCEPT='*/*,application/json')
    assert response['Content-Type'] == '%s; charset=%s' % (
        formats.find(ArticleViews.supported_formats[0]).content_type,
        settings.DEFAULT_CHARSET
    )

    response = client.get('/news/articles/1', HTTP_ACCEPT='unsupported/format, */*')
    assert response['Content-Type'] == '%s; charset=%s' % (
        formats.find(ArticleViews.supported_formats[0]).content_type,
        settings.DEFAULT_CHARSET
    )
Example #4
0
def test_content_types():
    from django.conf import settings
    from respite import formats
    from .project.app.views import ArticleViews

    response = client.get('/news/articles/1',
                          HTTP_ACCEPT='*/*,application/json')
    assert response['Content-Type'] == '%s; charset=%s' % (
        formats.find(ArticleViews.supported_formats[0]).content_type,
        settings.DEFAULT_CHARSET)

    response = client.get('/news/articles/1',
                          HTTP_ACCEPT='unsupported/format, */*')
    assert response['Content-Type'] == '%s; charset=%s' % (
        formats.find(ArticleViews.supported_formats[0]).content_type,
        settings.DEFAULT_CHARSET)
Example #5
0
from respite.serializers.jsonserializer import JSONSerializer
from respite.serializers.jsonpserializer import JSONPSerializer
from respite.serializers.xmlserializer import XMLSerializer
from respite import formats

SERIALIZERS = {
    formats.find('JavaScript Object Notation'): JSONSerializer,
    formats.find('JavaScript'): JSONPSerializer,
    formats.find('Extensible Markup Language'): XMLSerializer
}

def find(format):
    """
    Find and return a serializer for the given format.

    Arguments:
    format -- A Format instance.
    """
    try:
        serializer = SERIALIZERS[format]
    except KeyError:
        raise UnknownSerializer('No serializer found for %s' % format.acronym)

    return serializer

class UnknownSerializer(Exception):
    pass
Example #6
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 #7
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 #8
0
from respite.serializers.jsonserializer import JSONSerializer
from respite import formats

serializers = {
    formats.find('JavaScript Object Notation'): JSONSerializer
}
Example #9
0
def test_find():
    format = formats.find('HTML')

    assert_equal('HyperText Markup Language', format.name)
Example #10
0
def test_preferred_content_type():
    format = formats.find('HTML')

    assert_equal('text/html', format.content_type)
Example #11
0
def test_preferred_extension():
    format = formats.find('HTML')

    assert_equal('html', format.extension)
Example #12
0
from respite.serializers.jsonserializer import JSONSerializer
from respite.serializers.jsonpserializer import JSONPSerializer
from respite.serializers.xmlserializer import XMLSerializer
from respite import formats

SERIALIZERS = {
    formats.find("JavaScript Object Notation"): JSONSerializer,
    formats.find("JavaScript"): JSONPSerializer,
    formats.find("Extensible Markup Language"): XMLSerializer,
}


def find(format):
    """
    Find and return a serializer for the given format.

    Arguments:
    format -- A Format instance.
    """
    try:
        serializer = SERIALIZERS[format]
    except KeyError:
        raise UnknownSerializer("No serializer found for %s" % format.acronym)

    return serializer


class UnknownSerializer(Exception):
    pass