mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 13:14:30 +03:00
removed useless stuff, request.POST and FILES not used + some doc
This commit is contained in:
parent
899233bf99
commit
26e10d0e3f
|
@ -24,42 +24,18 @@ class StandardContentMixin(ContentMixin):
|
||||||
return None
|
return None
|
||||||
return (request.META.get('CONTENT_TYPE', None), request.raw_post_data)
|
return (request.META.get('CONTENT_TYPE', None), request.raw_post_data)
|
||||||
|
|
||||||
from django.core.files.base import File
|
|
||||||
class SocketFile(File):
|
|
||||||
# Only forward access is allowed
|
|
||||||
def __init__(self, socket, size):
|
|
||||||
super(SocketFile, self).__init__(socket)
|
|
||||||
self._size = int(size)
|
|
||||||
self._pos = 0
|
|
||||||
|
|
||||||
def read(self, num_bytes=None):
|
|
||||||
if num_bytes is None:
|
|
||||||
num_bytes = self._size - self._pos
|
|
||||||
else:
|
|
||||||
num_bytes = min(num_bytes, self._size - self._pos)
|
|
||||||
self._pos += num_bytes
|
|
||||||
return self.file.read(num_bytes)
|
|
||||||
|
|
||||||
def tell(self):
|
|
||||||
return self._pos
|
|
||||||
|
|
||||||
def seek(self, position):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class OverloadedContentMixin(ContentMixin):
|
class OverloadedContentMixin(ContentMixin):
|
||||||
"""HTTP request content behaviour that also allows arbitrary content to be tunneled in form data."""
|
"""HTTP request content behaviour that also allows arbitrary content to be tunneled in form data."""
|
||||||
|
|
||||||
#TODO: test PUT
|
"""The name to use for the content override field in the POST form. Set this to *None* to desactivate content overloading."""
|
||||||
#TODO: rewrite cleaner
|
|
||||||
|
|
||||||
"""The name to use for the content override field in the POST form."""
|
|
||||||
CONTENT_PARAM = '_content'
|
CONTENT_PARAM = '_content'
|
||||||
|
|
||||||
"""The name to use for the content-type override field in the POST form."""
|
"""The name to use for the content-type override field in the POST form. Taken into account only if content overloading is activated."""
|
||||||
CONTENTTYPE_PARAM = '_contenttype'
|
CONTENTTYPE_PARAM = '_contenttype'
|
||||||
|
|
||||||
def determine_content(self, request):
|
def determine_content(self, request):
|
||||||
"""If the request contains content return a tuple of (content_type, content) otherwise return None.
|
"""If the request contains content, returns a tuple of (content_type, content) otherwise returns None.
|
||||||
Note that content_type may be None if it is unset."""
|
Note that content_type may be None if it is unset."""
|
||||||
if not request.META.get('CONTENT_LENGTH', None) and not request.META.get('TRANSFER_ENCODING', None):
|
if not request.META.get('CONTENT_LENGTH', None) and not request.META.get('TRANSFER_ENCODING', None):
|
||||||
return None # TODO : Breaks, because determine_content should return a tuple.
|
return None # TODO : Breaks, because determine_content should return a tuple.
|
||||||
|
@ -68,14 +44,11 @@ class OverloadedContentMixin(ContentMixin):
|
||||||
if (request.method == 'POST' and self.CONTENT_PARAM and
|
if (request.method == 'POST' and self.CONTENT_PARAM and
|
||||||
request.POST.get(self.CONTENT_PARAM, None) is not None):
|
request.POST.get(self.CONTENT_PARAM, None) is not None):
|
||||||
|
|
||||||
# Set content type if form contains a none empty FORM_PARAM_CONTENTTYPE field
|
# Set content type if form contains a non-empty CONTENTTYPE_PARAM field
|
||||||
content_type = None
|
content_type = None
|
||||||
if self.CONTENTTYPE_PARAM and request.POST.get(self.CONTENTTYPE_PARAM, None):
|
if self.CONTENTTYPE_PARAM and request.POST.get(self.CONTENTTYPE_PARAM, None):
|
||||||
content_type = request.POST.get(self.CONTENTTYPE_PARAM, None)
|
content_type = request.POST.get(self.CONTENTTYPE_PARAM, None)
|
||||||
|
|
||||||
return (content_type, request.POST[self.CONTENT_PARAM])
|
return (content_type, request.POST[self.CONTENT_PARAM])
|
||||||
elif request.method == 'PUT':
|
|
||||||
f = SocketFile(request.environ['wsgi.input'], request.META['CONTENT_LENGTH'])
|
|
||||||
return (content_type, f.read())
|
|
||||||
else:
|
else:
|
||||||
return (content_type, request.raw_post_data)
|
return (content_type, request.raw_post_data)
|
||||||
|
|
|
@ -114,13 +114,7 @@ class FormParser(BaseParser, DataFlatener):
|
||||||
EMPTY_VALUE = 'EMPTY'
|
EMPTY_VALUE = 'EMPTY'
|
||||||
|
|
||||||
def parse(self, input):
|
def parse(self, input):
|
||||||
request = self.resource.request
|
|
||||||
|
|
||||||
if request.method == 'PUT':
|
|
||||||
data = parse_qs(input)
|
data = parse_qs(input)
|
||||||
elif request.method == 'POST':
|
|
||||||
# Django has already done the form parsing for us.
|
|
||||||
data = dict(request.POST.iterlists())
|
|
||||||
|
|
||||||
# Flatening data and removing EMPTY_VALUEs from the lists
|
# Flatening data and removing EMPTY_VALUEs from the lists
|
||||||
data = self.flatten_data(data)
|
data = self.flatten_data(data)
|
||||||
|
@ -150,14 +144,9 @@ class MultipartParser(BaseParser, DataFlatener):
|
||||||
def parse(self, input):
|
def parse(self, input):
|
||||||
request = self.resource.request
|
request = self.resource.request
|
||||||
|
|
||||||
if request.method == 'PUT':
|
|
||||||
upload_handlers = request._get_upload_handlers()
|
upload_handlers = request._get_upload_handlers()
|
||||||
django_mpp = DjangoMPParser(request.META, StringIO(input), upload_handlers)
|
django_mpp = DjangoMPParser(request.META, StringIO(input), upload_handlers)
|
||||||
data, files = django_mpp.parse()
|
data, files = django_mpp.parse()
|
||||||
elif request.method == 'POST':
|
|
||||||
# Django has already done the form parsing for us.
|
|
||||||
data = request.POST
|
|
||||||
files = request.FILES
|
|
||||||
|
|
||||||
# Flatening data, files and combining them
|
# Flatening data, files and combining them
|
||||||
data = self.flatten_data(data)
|
data = self.flatten_data(data)
|
||||||
|
|
|
@ -34,44 +34,6 @@ def url_resolves(url):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# From piston
|
|
||||||
def coerce_put_post(request):
|
|
||||||
"""
|
|
||||||
Django doesn't particularly understand REST.
|
|
||||||
In case we send data over PUT, Django won't
|
|
||||||
actually look at the data and load it. We need
|
|
||||||
to twist its arm here.
|
|
||||||
|
|
||||||
The try/except abominiation here is due to a bug
|
|
||||||
in mod_python. This should fix it.
|
|
||||||
"""
|
|
||||||
if request.method != 'PUT':
|
|
||||||
return
|
|
||||||
|
|
||||||
# Bug fix: if _load_post_and_files has already been called, for
|
|
||||||
# example by middleware accessing request.POST, the below code to
|
|
||||||
# pretend the request is a POST instead of a PUT will be too late
|
|
||||||
# to make a difference. Also calling _load_post_and_files will result
|
|
||||||
# in the following exception:
|
|
||||||
# AttributeError: You cannot set the upload handlers after the upload has been processed.
|
|
||||||
# The fix is to check for the presence of the _post field which is set
|
|
||||||
# the first time _load_post_and_files is called (both by wsgi.py and
|
|
||||||
# modpython.py). If it's set, the request has to be 'reset' to redo
|
|
||||||
# the query value parsing in POST mode.
|
|
||||||
if hasattr(request, '_post'):
|
|
||||||
del request._post
|
|
||||||
del request._files
|
|
||||||
|
|
||||||
try:
|
|
||||||
request.method = "POST"
|
|
||||||
request._load_post_and_files()
|
|
||||||
request.method = "PUT"
|
|
||||||
except AttributeError:
|
|
||||||
request.META['REQUEST_METHOD'] = 'POST'
|
|
||||||
request._load_post_and_files()
|
|
||||||
request.META['REQUEST_METHOD'] = 'PUT'
|
|
||||||
|
|
||||||
request.PUT = request.POST
|
|
||||||
|
|
||||||
# From http://www.koders.com/python/fidB6E125C586A6F49EAC38992CF3AFDAAE35651975.aspx?s=mdef:xml
|
# From http://www.koders.com/python/fidB6E125C586A6F49EAC38992CF3AFDAAE35651975.aspx?s=mdef:xml
|
||||||
#class object_dict(dict):
|
#class object_dict(dict):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user