def process_cache_response(self, view_instance, view_method, request, args,
                               kwargs):
        key = self.calculate_key(view_instance=view_instance,
                                 view_method=view_method,
                                 request=request,
                                 args=args,
                                 kwargs=kwargs)
        response = self.cache.get(key)
        if not response:
            response = view_method(view_instance, request, *args, **kwargs)
            response = view_instance.finalize_response(request, response,
                                                       *args, **kwargs)
            response.render(
            )  # should be rendered, before picklining while storing to cache

            if not response.status_code >= 400 or self.cache_errors:
                response_dict = (response.rendered_content,
                                 response.status_code, response._headers)
                self.cache.set(key, response_dict, self.timeout)
        else:
            content, status, headers = response

            try:
                data = content.decode()
                data = json.loads(data)
                response = DrfResponse(data=data, status=status)
                response._headers = headers
                if not hasattr(response, '_closable_objects'):
                    response._closable_objects = []
                return response
            except:
                pass

            response = HttpResponse(content=content, status=status)
            response._headers = headers

        if not hasattr(response, '_closable_objects'):
            response._closable_objects = []

        return response
示例#2
0
def load_response(file_or_stream):
    from rest_framework.response import Response

    context = json.loads(_read(file_or_stream))
    response = Response(context['data'],
                        status=context['status_code'],
                        content_type=context['content_type'])
    response._is_rendered = True
    if dj_version < (3, 2):
        response._headers = context['headers']
    else:
        response.headers = context['headers']
    return response