aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/parsers.py
diff options
context:
space:
mode:
authorTom Christie2013-05-08 20:07:51 +0100
committerTom Christie2013-05-08 20:07:51 +0100
commit429e078eee63a120c408946cf7c1460d4ca9e9b4 (patch)
treeacc98187b15a2a60a74529e43f59f8161b4f7f7f /rest_framework/parsers.py
parent246a376dcf6b62aa6ce42ef8dc69313a58fb7e72 (diff)
downloaddjango-rest-framework-429e078eee63a120c408946cf7c1460d4ca9e9b4.tar.bz2
Allow None filename on uploaded files
Diffstat (limited to 'rest_framework/parsers.py')
-rw-r--r--rest_framework/parsers.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py
index 614531a1..25be2e6a 100644
--- a/rest_framework/parsers.py
+++ b/rest_framework/parsers.py
@@ -219,7 +219,7 @@ class FileUploadParser(BaseParser):
Returns a DataAndFiles object.
`.data` will be None (we expect request body to be a file content).
- `.files` will be a `QueryDict` containing one 'file' elemnt - a parsed file.
+ `.files` will be a `QueryDict` containing one 'file' element.
"""
parser_context = parser_context or {}
@@ -229,9 +229,13 @@ class FileUploadParser(BaseParser):
upload_handlers = request.upload_handlers
filename = self.get_filename(stream, media_type, parser_context)
- content_type = meta.get('HTTP_CONTENT_TYPE', meta.get('CONTENT_TYPE', ''))
+ # Note that this code is extracted from Django's handling of
+ # file uploads in MultiPartParser.
+ content_type = meta.get('HTTP_CONTENT_TYPE',
+ meta.get('CONTENT_TYPE', ''))
try:
- content_length = int(meta.get('HTTP_CONTENT_LENGTH', meta.get('CONTENT_LENGTH', 0)))
+ content_length = int(meta.get('HTTP_CONTENT_LENGTH',
+ meta.get('CONTENT_LENGTH', 0)))
except (ValueError, TypeError):
content_length = None
@@ -245,6 +249,7 @@ class FileUploadParser(BaseParser):
if result is not None:
return DataAndFiles(None, {'file': result[1]})
+ # This is the standard case.
possible_sizes = [x.chunk_size for x in upload_handlers if x.chunk_size]
chunk_size = min([2 ** 31 - 4] + possible_sizes)
chunks = ChunkIter(stream, chunk_size)
@@ -252,7 +257,8 @@ class FileUploadParser(BaseParser):
for handler in upload_handlers:
try:
- handler.new_file(None, filename, content_type, content_length, encoding)
+ handler.new_file(None, filename, content_type,
+ content_length, encoding)
except StopFutureHandlers:
break
@@ -262,14 +268,14 @@ class FileUploadParser(BaseParser):
chunk = handler.receive_data_chunk(chunk, counters[i])
counters[i] += chunk_length
if chunk is None:
- # If the chunk received by the handler is None, then don't continue.
break
for i, handler in enumerate(upload_handlers):
file_obj = handler.file_complete(counters[i])
if file_obj:
return DataAndFiles(None, {'file': file_obj})
- raise ParseError("FileUpload parse error - none of upload handlers can handle the stream")
+ raise ParseError("FileUpload parse error - "
+ "none of upload handlers can handle the stream")
def get_filename(self, stream, media_type, parser_context):
"""
@@ -286,4 +292,4 @@ class FileUploadParser(BaseParser):
disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'])
return disposition[1]['filename']
except (AttributeError, KeyError):
- raise ParseError("Filename must be set in Content-Disposition header.")
+ pass