Mercurial > p > roundup > code
comparison test/test_cgi.py @ 5065:47ab150b7325
Allow multiple file uploads
If the html template specifies multiple="multiple" for a file upload
the user can attach multiple files and the form parser now handles this.
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Mon, 30 May 2016 17:23:35 +0200 |
| parents | 364c54991861 |
| children | e424987d294a |
comparison
equal
deleted
inserted
replaced
| 5064:46da0db55545 | 5065:47ab150b7325 |
|---|---|
| 22 | 22 |
| 23 class FileUpload: | 23 class FileUpload: |
| 24 def __init__(self, content, filename): | 24 def __init__(self, content, filename): |
| 25 self.content = content | 25 self.content = content |
| 26 self.filename = filename | 26 self.filename = filename |
| 27 | |
| 28 class FileList: | |
| 29 def __init__(self, name, *files): | |
| 30 self.name = name | |
| 31 self.files = files | |
| 32 def items (self): | |
| 33 for f in self.files: | |
| 34 yield (self.name, f) | |
| 27 | 35 |
| 28 def makeForm(args): | 36 def makeForm(args): |
| 29 form = cgi.FieldStorage() | 37 form = cgi.FieldStorage() |
| 30 for k,v in args.items(): | 38 for k,v in args.items(): |
| 31 if type(v) is type([]): | 39 if type(v) is type([]): |
| 260 def testFileUpload(self): | 268 def testFileUpload(self): |
| 261 file = FileUpload('foo', 'foo.txt') | 269 file = FileUpload('foo', 'foo.txt') |
| 262 self.assertEqual(self.parseForm({'content': file}, 'file'), | 270 self.assertEqual(self.parseForm({'content': file}, 'file'), |
| 263 ({('file', None): {'content': 'foo', 'name': 'foo.txt', | 271 ({('file', None): {'content': 'foo', 'name': 'foo.txt', |
| 264 'type': 'text/plain'}}, [])) | 272 'type': 'text/plain'}}, [])) |
| 273 | |
| 274 def testSingleFileUpload(self): | |
| 275 file = FileUpload('foo', 'foo.txt') | |
| 276 self.assertEqual(self.parseForm({'@file': file}, 'issue'), | |
| 277 ({('file', '-1'): {'content': 'foo', 'name': 'foo.txt', | |
| 278 'type': 'text/plain'}, | |
| 279 ('issue', None): {}}, | |
| 280 [('issue', None, 'files', [('file', '-1')])])) | |
| 281 | |
| 282 def testMultipleFileUpload(self): | |
| 283 f1 = FileUpload('foo', 'foo.txt') | |
| 284 f2 = FileUpload('bar', 'bar.txt') | |
| 285 f3 = FileUpload('baz', 'baz.txt') | |
| 286 files = FileList('@file', f1, f2, f3) | |
| 287 | |
| 288 self.assertEqual(self.parseForm(files, 'issue'), | |
| 289 ({('file', '-1'): {'content': 'foo', 'name': 'foo.txt', | |
| 290 'type': 'text/plain'}, | |
| 291 ('file', '-2'): {'content': 'bar', 'name': 'bar.txt', | |
| 292 'type': 'text/plain'}, | |
| 293 ('file', '-3'): {'content': 'baz', 'name': 'baz.txt', | |
| 294 'type': 'text/plain'}, | |
| 295 ('issue', None): {}}, | |
| 296 [ ('issue', None, 'files', [('file', '-1')]) | |
| 297 , ('issue', None, 'files', [('file', '-2')]) | |
| 298 , ('issue', None, 'files', [('file', '-3')]) | |
| 299 ])) | |
| 265 | 300 |
| 266 def testEditFileClassAttributes(self): | 301 def testEditFileClassAttributes(self): |
| 267 self.assertEqual(self.parseForm({'name': 'foo.txt', | 302 self.assertEqual(self.parseForm({'name': 'foo.txt', |
| 268 'type': 'application/octet-stream'}, | 303 'type': 'application/octet-stream'}, |
| 269 'file'), | 304 'file'), |
