Exemplo n.º 1
0
    def test_mimeparser_best_match_should_ignore_invalid_mime_types(self):

        self.assertEqual(
            best_match(
                ['application/xbel+xml', 'text/xml'],
                'text/*;q=0.5, application, application/xbel+xml/2, */*; q=0.1'
            ), 'text/xml')
Exemplo n.º 2
0
def build_response(request, status_code, context, formatters, headers=None):

    if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
        content_type = 'application/json; charset=utf-8'
    else:
        formatter_keys = list(formatters.keys())
        formatter_keys.remove('')
        content_type = mimeparser.best_match(
            formatter_keys, request.META.get('HTTP_ACCEPT', '*/*'))

    if content_type in formatters:
        formatter = formatters[content_type]
    else:
        raise Exception('No suitable formatter found')

    body = formatter(request, content_type, status_code, context)
    response = HttpResponse(body,
                            content_type=content_type,
                            status=status_code)
    if headers is None:
        headers = {}

    for header_name in headers:
        response[header_name] = headers[header_name]

    return response
Exemplo n.º 3
0
    def test_mimeparser_best_match_should_ignore_blank_media_ranges_params(
            self):

        self.assertEqual(
            best_match(['application/xbel+xml; a=1; b=2', 'application/xml'],
                       'application/*, application/xbel+xml; a=1; b=2'),
            'application/xbel+xml; a=1; b=2')
Exemplo n.º 4
0
        def wrapper(self, request, *args, **kwargs):
            accept_header = request.META.get('HTTP_ACCEPT', '*/*')
            request.best_response_mimetype = mimeparser.best_match(mime_types, accept_header)
            if request.best_response_mimetype == '':
                msg = _("The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request")
                details = {'supported_mime_types': mime_types}
                return build_error_response(request, 406, msg, details=details)

            return func(self, request, *args, **kwargs)
Exemplo n.º 5
0
        def wrapper(self, request, *args, **kwargs):
            accept_header = request.META.get('HTTP_ACCEPT', '*/*')
            request.best_response_mimetype = mimeparser.best_match(mime_types, accept_header)
            if request.best_response_mimetype == '':
                msg = _("The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request")
                details = {'supported_mime_types': mime_types}
                return build_error_response(request, 406, msg, details=details)

            return func(self, request, *args, **kwargs)
Exemplo n.º 6
0
    def read(self, request, vendor, name, version):

        formats = ('application/json', 'application/xml')

        if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
            mimetype = 'application/json; charset=utf-8'
        else:
            mimetype = mimeparser.best_match(formats, request.META.get('HTTP_ACCEPT', ''))

        # Get the resource's id for those vendor, name and version
        resource = get_object_or_404(CatalogueResource, short_name=name, vendor=vendor, version=version)

        return get_vote_response(resource, request.user, mimetype)
Exemplo n.º 7
0
    def update(self, request, vendor, name, version):

        formats = ('application/json', 'application/xml')

        if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
            mimetype = 'application/json; charset=utf-8'
        else:
            mimetype = mimeparser.best_match(formats, request.META.get('HTTP_ACCEPT', ''))

        # Get the vote from the request
        content_type = get_content_type(request)[0]
        if content_type == 'application/json':
            try:
                vote = json.loads(request.raw_post_data)['vote']
            except ValueError, e:
                msg = _("malformed json data: %s") % unicode(e)
                return build_error_response(request, 400, msg)
Exemplo n.º 8
0
def build_error_response(request, status_code, error_msg, extra_formats=None, headers=None, details=None):

    if extra_formats is not None:
        formatters = extra_formats.copy()
        formatters.update(ERROR_FORMATTERS)
    else:
        formatters = ERROR_FORMATTERS

    if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
        mimetype = 'application/json; charset=utf-8'
    else:
        mimetype = mimeparser.best_match(formatters.keys(), request.META.get('HTTP_ACCEPT', 'text/plain'))

    body = formatters[mimetype](request, mimetype, status_code, error_msg, details)
    response = HttpResponse(body, mimetype=mimetype, status=status_code)
    if headers is None:
        headers = {}

    for header_name in headers:
        response[header_name] = headers[header_name]

    return response
Exemplo n.º 9
0
def build_response(request, status_code, context, formatters, headers=None):

    if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
        content_type = 'application/json; charset=utf-8'
    else:
        formatter_keys = list(formatters.keys())
        formatter_keys.remove('')
        content_type = mimeparser.best_match(formatter_keys, request.META.get('HTTP_ACCEPT', '*/*'))

    if content_type in formatters:
        formatter = formatters[content_type]
    else:
        raise Exception('No suitable formatter found')

    body = formatter(request, content_type, status_code, context)
    response = HttpResponse(body, content_type=content_type, status=status_code)
    if headers is None:
        headers = {}

    for header_name in headers:
        response[header_name] = headers[header_name]

    return response
Exemplo n.º 10
0
    def test_mimeparser_best_match_should_ignore_blank_media_ranges_params(self):

        self.assertEqual(best_match(['application/xbel+xml; a=1; b=2', 'application/xml'], 'application/*, application/xbel+xml; a=1; b=2'), 'application/xbel+xml; a=1; b=2')
Exemplo n.º 11
0
    def test_mimeparser_best_match_should_ignore_blank_media_ranges(self):

        self.assertEqual(best_match(['application/xbel+xml', 'text/xml'], 'text/*;q=0.5, , */*; q=0.1'), 'text/xml')
Exemplo n.º 12
0
    def test_mimeparser_best_match_should_ignore_invalid_mime_types(self):

        self.assertEqual(best_match(['application/xbel+xml', 'text/xml'], 'text/*;q=0.5, application, application/xbel+xml/2, */*; q=0.1'), 'text/xml')
Exemplo n.º 13
0
    def test_mimeparser_best_match_should_ignore_blank_media_ranges(self):

        self.assertEqual(
            best_match(['application/xbel+xml', 'text/xml'],
                       'text/*;q=0.5, , */*; q=0.1'), 'text/xml')