Guess filename.
authorKenneth Reitz <me@kennethreitz.com>
Sun, 13 Nov 2011 05:36:22 +0000 (00:36 -0500)
committerKenneth Reitz <me@kennethreitz.com>
Sun, 13 Nov 2011 05:36:22 +0000 (00:36 -0500)
Closes #192

requests/models.py
requests/utils.py

index e70d9db8aaa3ad94c6004d45854aaa0e30a7c264..79a15fc2b2a808b1d3296a2a1ca87cee4d4a497f 100644 (file)
@@ -28,7 +28,7 @@ from .exceptions import (
     Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)
 from .utils import (
     get_unicode_from_response, stream_decode_response_unicode,
-    decode_gzip, stream_decode_gzip)
+    decode_gzip, stream_decode_gzip, guess_filename)
 
 
 
@@ -346,7 +346,8 @@ class Request(object):
                     fields = dict(self.data)
 
                 for (k, v) in self.files.items():
-                    fields.update({k: (k, v.read())})
+                    fields.update({k: (guess_filename(k) or k, v.read())})
+
                 (body, content_type) = encode_multipart_formdata(fields)
             else:
                 pass
index bd7dead43b84867ee44770e4196294cfe3c0064d..0249e9d6c6b04a07d33885736f7aec03707d2d03 100644 (file)
@@ -20,6 +20,12 @@ import zlib
 from urllib2 import parse_http_list as _parse_list_header
 
 
+def guess_filename(obj):
+    """Tries to guess the filename of the given object."""
+    name = getattr(obj, 'name', None)
+    if name and name[0] != '<' and name[-1] != '>':
+        return name
+
 # From mitsuhiko/werkzeug (used with permission).
 def parse_list_header(value):
     """Parse lists as described by RFC 2068 Section 2.