Used the cached_property decorator on the request object and the response object.

This commit is contained in:
Omer Katz 2014-10-02 17:46:06 +03:00
parent ffc6aa3abc
commit 868f7dd91b
2 changed files with 13 additions and 9 deletions

View File

@ -9,11 +9,14 @@ The wrapped request then offers a richer API, in particular :
- form overloading of HTTP method, content type and content - form overloading of HTTP method, content type and content
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
from django.conf import settings from django.conf import settings
from django.http import QueryDict from django.http import QueryDict
from django.http.multipartparser import parse_header from django.http.multipartparser import parse_header
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.datastructures import MergeDict as DjangoMergeDict from django.utils.datastructures import MergeDict as DjangoMergeDict
from django.utils.functional import cached_property
from rest_framework import HTTP_HEADER_ENCODING from rest_framework import HTTP_HEADER_ENCODING
from rest_framework import exceptions from rest_framework import exceptions
from rest_framework.compat import BytesIO from rest_framework.compat import BytesIO
@ -163,7 +166,7 @@ class Request(object):
def _default_negotiator(self): def _default_negotiator(self):
return api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS() return api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS()
@property @cached_property
def method(self): def method(self):
""" """
Returns the HTTP method. Returns the HTTP method.
@ -175,7 +178,7 @@ class Request(object):
self._load_method_and_content_type() self._load_method_and_content_type()
return self._method return self._method
@property @cached_property
def content_type(self): def content_type(self):
""" """
Returns the content type header. Returns the content type header.
@ -188,7 +191,7 @@ class Request(object):
self._load_method_and_content_type() self._load_method_and_content_type()
return self._content_type return self._content_type
@property @cached_property
def stream(self): def stream(self):
""" """
Returns an object that may be used to stream the request content. Returns an object that may be used to stream the request content.
@ -211,13 +214,13 @@ class Request(object):
""" """
return self._request.GET return self._request.GET
@property @cached_property
def data(self): def data(self):
if not _hasattr(self, '_full_data'): if not _hasattr(self, '_full_data'):
self._load_data_and_files() self._load_data_and_files()
return self._full_data return self._full_data
@property @cached_property
def DATA(self): def DATA(self):
""" """
Parses the request body and returns the data. Parses the request body and returns the data.
@ -229,7 +232,7 @@ class Request(object):
self._load_data_and_files() self._load_data_and_files()
return self._data return self._data
@property @cached_property
def FILES(self): def FILES(self):
""" """
Parses the request body and returns any files uploaded in the request. Parses the request body and returns any files uploaded in the request.
@ -278,7 +281,7 @@ class Request(object):
""" """
self._auth = value self._auth = value
@property @cached_property
def successful_authenticator(self): def successful_authenticator(self):
""" """
Return the instance of the authentication instance class that was used Return the instance of the authentication instance class that was used

View File

@ -9,6 +9,7 @@ import django
from django.core.handlers.wsgi import STATUS_CODE_TEXT from django.core.handlers.wsgi import STATUS_CODE_TEXT
from django.template.response import SimpleTemplateResponse from django.template.response import SimpleTemplateResponse
from django.utils import six from django.utils import six
from django.utils.functional import cached_property
class Response(SimpleTemplateResponse): class Response(SimpleTemplateResponse):
@ -40,7 +41,7 @@ class Response(SimpleTemplateResponse):
for name, value in six.iteritems(headers): for name, value in six.iteritems(headers):
self[name] = value self[name] = value
@property @cached_property
def rendered_content(self): def rendered_content(self):
renderer = getattr(self, 'accepted_renderer', None) renderer = getattr(self, 'accepted_renderer', None)
media_type = getattr(self, 'accepted_media_type', None) media_type = getattr(self, 'accepted_media_type', None)
@ -73,7 +74,7 @@ class Response(SimpleTemplateResponse):
return ret return ret
@property @cached_property
def status_text(self): def status_text(self):
""" """
Returns reason text corresponding to our HTTP response status code. Returns reason text corresponding to our HTTP response status code.