Django

Code

Show
Ignore:
Timestamp:
07/10/08 15:47:18 (6 months ago)
Author:
brosner
Message:

newforms-admin: Merged from trunk up to [7877].

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin

    • Property svnmerge-integrated changed from /django/trunk:1-4345,4350-4357,4359-4365,4371-4372,4374-4377,4380-4386,4388,4390-4391,4400-4402,4404-4408,4410,4412-4419,4426-4427,4430-4432,4434,4441,4443-4444,4446-4447,4450,4452-4453,4455-4458,4476,4503,4546,4564-4569,4580-4586,4617,4630,4641-6390,6392-7852 to /django/trunk:1-4345,4350-4357,4359-4365,4371-4372,4374-4377,4380-4386,4388,4390-4391,4400-4402,4404-4408,4410,4412-4419,4426-4427,4430-4432,4434,4441,4443-4444,4446-4447,4450,4452-4453,4455-4458,4476,4503,4546,4564-4569,4580-4586,4617,4630,4641-6390,6392-7880
  • django/branches/newforms-admin/django/test/client.py

    r7830 r7881  
    9191    lines = [] 
    9292    to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) 
     93 
     94    # Not by any means perfect, but good enough for our purposes. 
     95    is_file = lambda thing: hasattr(thing, "read") and callable(thing.read) 
     96 
     97    # Each bit of the multipart form data could be either a form value or a 
     98    # file, or a *list* of form values and/or files. Remember that HTTP field 
     99    # names can be duplicated! 
    93100    for (key, value) in data.items(): 
    94         if isinstance(value, file): 
    95             lines.extend([ 
    96                 '--' + boundary, 
    97                 'Content-Disposition: form-data; name="%s"; filename="%s"' \ 
    98                     % (to_str(key), to_str(os.path.basename(value.name))), 
    99                 'Content-Type: application/octet-stream', 
    100                 '', 
    101                 value.read() 
    102             ]) 
    103         else: 
    104             if not isinstance(value, basestring) and is_iterable(value): 
    105                 for item in value: 
     101        if is_file(value): 
     102            lines.extend(encode_file(boundary, key, value)) 
     103        elif not isinstance(value, basestring) and is_iterable(value): 
     104            for item in value: 
     105                if is_file(item): 
     106                    lines.extend(encode_file(boundary, key, item)) 
     107                else: 
    106108                    lines.extend([ 
    107109                        '--' + boundary, 
     
    110112                        to_str(item) 
    111113                    ]) 
    112             else: 
    113                 lines.extend([ 
    114                     '--' + boundary, 
    115                     'Content-Disposition: form-data; name="%s"' % to_str(key), 
    116                     '', 
    117                     to_str(value) 
    118                 ]) 
     114        else: 
     115            lines.extend([ 
     116                '--' + boundary, 
     117                'Content-Disposition: form-data; name="%s"' % to_str(key), 
     118                '', 
     119                to_str(value) 
     120            ]) 
    119121 
    120122    lines.extend([ 
     
    124126    return '\r\n'.join(lines) 
    125127 
     128def encode_file(boundary, key, file): 
     129    to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) 
     130    return [ 
     131        '--' + boundary, 
     132        'Content-Disposition: form-data; name="%s"; filename="%s"' \ 
     133            % (to_str(key), to_str(os.path.basename(file.name))), 
     134        'Content-Type: application/octet-stream', 
     135        '', 
     136        file.read() 
     137    ] 
     138     
    126139class Client: 
    127140    """