Django

Code

Show
Ignore:
Timestamp:
07/01/08 10:10:51 (2 months ago)
Author:
jacob
Message:

Fixed #2070: refactored Django's file upload capabilities.

A description of the new features can be found in the new upload handling documentation; the executive summary is that Django will now happily handle uploads of large files without issues.

This changes the representation of uploaded files from dictionaries to bona fide objects; see BackwardsIncompatibleChanges for details.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/modeltests/model_forms/models.py

    r7335 r7814  
    6868class ImageFile(models.Model): 
    6969    description = models.CharField(max_length=20) 
    70     image = models.FileField(upload_to=tempfile.gettempdir()) 
     70    try: 
     71        # If PIL is available, try testing PIL. 
     72        # Otherwise, it's equivalent to TextFile above. 
     73        import Image 
     74        image = models.ImageField(upload_to=tempfile.gettempdir()) 
     75    except ImportError: 
     76        image = models.FileField(upload_to=tempfile.gettempdir()) 
    7177 
    7278    def __unicode__(self): 
     
    7682>>> from django import newforms as forms 
    7783>>> from django.newforms.models import ModelForm 
     84>>> from django.core.files.uploadedfile import SimpleUploadedFile 
    7885 
    7986The bare bones, absolutely nothing custom, basic case. 
     
    793800# Upload a file and ensure it all works as expected. 
    794801 
    795 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test1.txt', 'content': 'hello world'}}) 
     802>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test1.txt', 'hello world')}) 
    796803>>> f.is_valid() 
    797804True 
     
    802809u'...test1.txt' 
    803810 
     811>>> os.unlink(instance.get_file_filename()) 
     812 
     813>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test1.txt', 'hello world')}) 
     814>>> f.is_valid() 
     815True 
     816>>> type(f.cleaned_data['file']) 
     817<class 'django.newforms.fields.UploadedFile'> 
     818>>> instance = f.save() 
     819>>> instance.file 
     820u'...test1.txt' 
     821 
    804822# Edit an instance that already has the file defined in the model. This will not 
    805823# save the file again, but leave it exactly as it is. 
     
    815833 
    816834# Delete the current file since this is not done by Django. 
    817  
    818835>>> os.unlink(instance.get_file_filename()) 
    819836 
    820837# Override the file by uploading a new one. 
    821838 
    822 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test2.txt', 'content': 'hello world'}}, instance=instance) 
     839>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test2.txt', 'hello world')}, instance=instance) 
    823840>>> f.is_valid() 
    824841True 
     
    827844u'...test2.txt' 
    828845 
     846# Delete the current file since this is not done by Django. 
     847>>> os.unlink(instance.get_file_filename()) 
     848 
     849>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test2.txt', 'hello world')}) 
     850>>> f.is_valid() 
     851True 
     852>>> instance = f.save() 
     853>>> instance.file 
     854u'...test2.txt' 
     855 
     856# Delete the current file since this is not done by Django. 
     857>>> os.unlink(instance.get_file_filename()) 
     858 
    829859>>> instance.delete() 
    830860 
     
    839869'' 
    840870 
    841 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test3.txt', 'content': 'hello world'}}, instance=instance) 
     871>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test3.txt', 'hello world')}, instance=instance) 
    842872>>> f.is_valid() 
    843873True 
     
    845875>>> instance.file 
    846876u'...test3.txt' 
     877 
     878# Delete the current file since this is not done by Django. 
     879>>> os.unlink(instance.get_file_filename()) 
     880>>> instance.delete() 
     881 
     882>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test3.txt', 'hello world')}) 
     883>>> f.is_valid() 
     884True 
     885>>> instance = f.save() 
     886>>> instance.file 
     887u'...test3.txt' 
     888 
     889# Delete the current file since this is not done by Django. 
     890>>> os.unlink(instance.get_file_filename()) 
    847891>>> instance.delete() 
    848892 
     
    859903>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png")).read() 
    860904 
    861 >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': {'filename': 'test.png', 'content': image_data}}) 
     905>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)}) 
    862906>>> f.is_valid() 
    863907True 
     
    868912u'...test.png' 
    869913 
     914# Delete the current file since this is not done by Django. 
     915>>> os.unlink(instance.get_image_filename()) 
     916 
     917>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)}) 
     918>>> f.is_valid() 
     919True 
     920>>> type(f.cleaned_data['image']) 
     921<class 'django.newforms.fields.UploadedFile'> 
     922>>> instance = f.save() 
     923>>> instance.image 
     924u'...test.png' 
     925 
    870926# Edit an instance that already has the image defined in the model. This will not 
    871927# save the image again, but leave it exactly as it is. 
     
    886942# Override the file by uploading a new one. 
    887943 
    888 >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': {'filename': 'test2.png', 'content': image_data}}, instance=instance) 
     944>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)}, instance=instance) 
    889945>>> f.is_valid() 
    890946True 
     
    893949u'...test2.png' 
    894950 
     951# Delete the current file since this is not done by Django. 
     952>>> os.unlink(instance.get_image_filename()) 
     953>>> instance.delete() 
     954 
     955>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data)}) 
     956>>> f.is_valid() 
     957True 
     958>>> instance = f.save() 
     959>>> instance.image 
     960u'...test2.png' 
     961 
     962# Delete the current file since this is not done by Django. 
     963>>> os.unlink(instance.get_image_filename()) 
    895964>>> instance.delete() 
    896965 
     
    905974'' 
    906975 
    907 >>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': {'filename': 'test3.png', 'content': image_data}}, instance=instance) 
     976>>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance) 
     977>>> f.is_valid() 
     978True 
     979>>> instance = f.save() 
     980>>> instance.image 
     981u'...test3.png' 
     982 
     983# Delete the current file since this is not done by Django. 
     984>>> os.unlink(instance.get_image_filename()) 
     985>>> instance.delete() 
     986 
     987>>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}) 
    908988>>> f.is_valid() 
    909989True