summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-07-07 22:06:32 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-07-07 22:06:32 +0000
commit9dabd1f8ff7760f28bd1dcfa8b83ae6b1c0b79aa (patch)
tree2d87553e045e57e83fd18279a2fffb503b61b044 /django
parent72544950e198c08ac49ba9d878ea7440313b13a7 (diff)
Fixed #7651: uploading multiple files with the same name now work. Also, in order to test the problem the test client now handles uploading multiple files at once. Patch from Mike Axiak.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7858 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/http/multipartparser.py1
-rw-r--r--django/test/client.py51
2 files changed, 33 insertions, 19 deletions
diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
index 8bed5681cf..fc48aa9e7b 100644
--- a/django/http/multipartparser.py
+++ b/django/http/multipartparser.py
@@ -136,6 +136,7 @@ class MultiPartParser(object):
# since we cannot be sure a file is complete until
# we hit the next boundary/part of the multipart content.
self.handle_file_complete(old_field_name, counters)
+ old_field_name = None
try:
disposition = meta_data['content-disposition'][1]
diff --git a/django/test/client.py b/django/test/client.py
index 87731043a7..47c12a4ca1 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -90,32 +90,34 @@ def encode_multipart(boundary, data):
"""
lines = []
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
+
+ # Not by any means perfect, but good enough for our purposes.
+ is_file = lambda thing: hasattr(thing, "read") and callable(thing.read)
+
+ # Each bit of the multipart form data could be either a form value or a
+ # file, or a *list* of form values and/or files. Remember that HTTP field
+ # names can be duplicated!
for (key, value) in data.items():
- if isinstance(value, file):
- lines.extend([
- '--' + boundary,
- 'Content-Disposition: form-data; name="%s"; filename="%s"' \
- % (to_str(key), to_str(os.path.basename(value.name))),
- 'Content-Type: application/octet-stream',
- '',
- value.read()
- ])
- else:
- if not isinstance(value, basestring) and is_iterable(value):
- for item in value:
+ if is_file(value):
+ lines.extend(encode_file(boundary, key, value))
+ elif not isinstance(value, basestring) and is_iterable(value):
+ for item in value:
+ if is_file(item):
+ lines.extend(encode_file(boundary, key, item))
+ else:
lines.extend([
'--' + boundary,
'Content-Disposition: form-data; name="%s"' % to_str(key),
'',
to_str(item)
])
- else:
- lines.extend([
- '--' + boundary,
- 'Content-Disposition: form-data; name="%s"' % to_str(key),
- '',
- to_str(value)
- ])
+ else:
+ lines.extend([
+ '--' + boundary,
+ 'Content-Disposition: form-data; name="%s"' % to_str(key),
+ '',
+ to_str(value)
+ ])
lines.extend([
'--' + boundary + '--',
@@ -123,6 +125,17 @@ def encode_multipart(boundary, data):
])
return '\r\n'.join(lines)
+def encode_file(boundary, key, file):
+ to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
+ return [
+ '--' + boundary,
+ 'Content-Disposition: form-data; name="%s"; filename="%s"' \
+ % (to_str(key), to_str(os.path.basename(file.name))),
+ 'Content-Type: application/octet-stream',
+ '',
+ file.read()
+ ]
+
class Client:
"""
A class that can act as a client for testing purposes.