Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" The Request class is used as a wrapper around the standard request object.
The wrapped request then offers a richer API, in particular :
- content automatically parsed according to `Content-Type` header, and available as `request.DATA` - full support of PUT method, including support for file uploads - form overloading of HTTP method, content type and content """
""" Return True if the media type is a valid form media type. """ base_media_type == 'multipart/form-data')
""" Placeholder for unset attributes. Cannot use `None`, as that may be a valid value. """
""" Internal helper method to clone a request, replacing with a different HTTP method. Used for checking permissions against other methods. """ parsers=request.parsers, authenticators=request.authenticators, negotiator=request.negotiator, parser_context=request.parser_context)
""" Wrapper allowing to enhance a standard `HttpRequest` instance.
Kwargs: - request(HttpRequest). The original request instance. - parsers_classes(list/tuple). The parsers to use for parsing the request content. - authentication_classes(list/tuple). The authentications used to try authenticating the request's user. """
negotiator=None, parser_context=None):
def method(self): """ Returns the HTTP method.
This allows the `method` to be overridden by using a hidden `form` field on a form POST request. """
def content_type(self): """ Returns the content type header.
This should be used instead of `request.META.get('HTTP_CONTENT_TYPE')`, as it allows the content type to be overridden by using a hidden form field on a form POST request. """ self._load_method_and_content_type()
def stream(self): """ Returns an object that may be used to stream the request content. """
def QUERY_PARAMS(self): """ More semantically correct name for request.GET. """
def DATA(self): """ Parses the request body and returns the data.
Similar to usual behaviour of `request.POST`, except that it handles arbitrary parsers, and also works on methods other than POST (eg PUT). """
def FILES(self): """ Parses the request body and returns any files uploaded in the request.
Similar to usual behaviour of `request.FILES`, except that it handles arbitrary parsers, and also works on methods other than POST (eg PUT). """ self._load_data_and_files()
def user(self): """ Returns the user associated with the current request, as authenticated by the authentication classes provided to the request. """
def user(self, value): """ Sets the user on the current request. This is necessary to maintain compatilbility with django.contrib.auth where the user proprety is set in the login and logout functions. """
def auth(self): """ Returns any non-user authentication information associated with the request, such as an authentication token. """ self._authenticate()
def auth(self, value): """ Sets any non-user authentication information associated with the request, such as an authentication token. """
def successful_authenticator(self): """ Return the instance of the authentication instance class that was used to authenticate the request, or `None`. """ self._authenticate()
""" Parses the request content into self.DATA and self.FILES. """
""" Sets the method and content_type, and then check if they've been overridden. """ self.META.get('CONTENT_TYPE', ''))
# Allow X-HTTP-METHOD-OVERRIDE header self._method)
""" Return the content body of the request, as a stream. """ self.META.get('HTTP_CONTENT_LENGTH')))
else: self._stream = BytesIO(self.raw_post_data)
""" If this is a form POST request, then we need to check if the method and content/content_type have been overridden by setting them in hidden form fields or not. """
self._METHOD_PARAM or (self._CONTENT_PARAM and self._CONTENTTYPE_PARAM) )
# We only need to use form overloading on form POST requests. or self._request.method != 'POST' or not is_form_media_type(self._content_type)):
# At this point we're committed to parsing the request as form data.
# Method overloading - change the method and remove the param from the content. self._METHOD_PARAM in self._data):
# Content overloading - modify the content type, and force re-parse. self._CONTENTTYPE_PARAM and self._CONTENT_PARAM in self._data and self._CONTENTTYPE_PARAM in self._data):
""" Parse the request content, returning a two-tuple of (data, files)
May raise an `UnsupportedMediaType`, or `ParseError` exception. """
raise exceptions.UnsupportedMediaType(media_type)
# Parser classes may return the raw data, or a # DataAndFiles object. Unpack the result as required.
""" Attempt to authenticate the request using each authentication instance in turn. Returns a three-tuple of (authenticator, user, authtoken). """
""" Return a three-tuple of (authenticator, user, authtoken), representing an unauthenticated request.
By default this will be (None, AnonymousUser, None). """
else: self._user = None
self._auth = api_settings.UNAUTHENTICATED_TOKEN() else:
""" Proxy other attributes to the underlying HttpRequest object. """ |