Django

Code

Changeset 7025

Show
Ignore:
Timestamp:
01/18/08 09:53:19 (6 months ago)
Author:
jacob
Message:

Fixed #6302 for ImageFields?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/fields.py

    r7021 r7025  
    467467        if f is None: 
    468468            return None 
     469        elif not data and initial: 
     470            return initial 
    469471        from PIL import Image 
    470472        from cStringIO import StringIO 
  • django/trunk/tests/modeltests/model_forms/models.py

    r7021 r7025  
    6363    file = models.FileField(upload_to=tempfile.gettempdir()) 
    6464 
     65    def __unicode__(self): 
     66        return self.description 
     67         
     68class ImageFile(models.Model): 
     69    description = models.CharField(max_length=20) 
     70    image = models.FileField(upload_to=tempfile.gettempdir()) 
     71     
    6572    def __unicode__(self): 
    6673        return self.description 
     
    719726...         model = TextFile 
    720727 
    721 Test conditions when files is either not given or empty. 
     728# Test conditions when files is either not given or empty. 
    722729 
    723730>>> f = TextFileForm(data={'description': u'Assistance'}) 
     
    728735False 
    729736 
    730 Upload a file and ensure it all works as expected. 
     737# Upload a file and ensure it all works as expected. 
    731738 
    732739>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test1.txt', 'content': 'hello world'}}) 
     
    739746u'.../test1.txt' 
    740747 
    741 Edit an instance that already has the file defined in the model. This will not 
    742 save the file again, but leave it exactly as it is. 
     748# Edit an instance that already has the file defined in the model. This will not 
     749# save the file again, but leave it exactly as it is. 
    743750 
    744751>>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance) 
     
    751758u'.../test1.txt' 
    752759 
    753 Delete the current file since this is not done by Django. 
     760# Delete the current file since this is not done by Django. 
    754761 
    755762>>> os.unlink(instance.get_file_filename()) 
    756763 
    757 Override the file by uploading a new one. 
     764# Override the file by uploading a new one. 
    758765 
    759766>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test2.txt', 'content': 'hello world'}}, instance=instance) 
     
    766773>>> instance.delete() 
    767774 
    768 Test the non-required FileField 
     775# Test the non-required FileField 
    769776 
    770777>>> f = TextFileForm(data={'description': u'Assistance'}) 
     
    783790u'.../test3.txt' 
    784791>>> instance.delete() 
     792 
     793# ImageField ################################################################### 
     794 
     795# ImageField and FileField are nearly identical, but they differ slighty when 
     796# it comes to validation. This specifically tests that #6302 is fixed for 
     797# both file fields and image fields. 
     798 
     799>>> class ImageFileForm(ModelForm): 
     800...     class Meta: 
     801...         model = ImageFile 
     802 
     803>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png")).read() 
     804 
     805>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': {'filename': 'test.png', 'content': image_data}}) 
     806>>> f.is_valid() 
     807True 
     808>>> type(f.cleaned_data['image']) 
     809<class 'django.newforms.fields.UploadedFile'> 
     810>>> instance = f.save() 
     811>>> instance.image 
     812u'.../test.png' 
     813 
     814# Edit an instance that already has the image defined in the model. This will not 
     815# save the image again, but leave it exactly as it is. 
     816 
     817>>> f = ImageFileForm(data={'description': u'Look, it changed'}, instance=instance) 
     818>>> f.is_valid() 
     819True 
     820>>> f.cleaned_data['image'] 
     821u'.../test.png' 
     822>>> instance = f.save() 
     823>>> instance.image 
     824u'.../test.png' 
     825 
     826# Delete the current image since this is not done by Django. 
     827 
     828>>> os.unlink(instance.get_image_filename()) 
     829 
     830# Override the file by uploading a new one. 
     831 
     832>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': {'filename': 'test2.png', 'content': image_data}}, instance=instance) 
     833>>> f.is_valid() 
     834True 
     835>>> instance = f.save() 
     836>>> instance.image 
     837u'.../test2.png' 
     838 
     839>>> instance.delete() 
     840 
     841# Test the non-required ImageField 
     842 
     843>>> f = ImageFileForm(data={'description': u'Test'}) 
     844>>> f.fields['image'].required = False 
     845>>> f.is_valid() 
     846True 
     847>>> instance = f.save() 
     848>>> instance.image 
     849'' 
     850 
     851>>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': {'filename': 'test3.png', 'content': image_data}}, instance=instance) 
     852>>> f.is_valid() 
     853True 
     854>>> instance = f.save() 
     855>>> instance.image 
     856u'.../test3.png' 
     857>>> instance.delete() 
     858 
    785859"""}