Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6302 for ImageFields.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7025 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 755278253ee792fc8a7589081261c6f0a66e1423 1 parent 12efef0
Jacob Kaplan-Moss authored January 18, 2008
2  django/newforms/fields.py
@@ -466,6 +466,8 @@ def clean(self, data, initial=None):
466 466
         f = super(ImageField, self).clean(data, initial)
467 467
         if f is None:
468 468
             return None
  469
+        elif not data and initial:
  470
+            return initial
469 471
         from PIL import Image
470 472
         from cStringIO import StringIO
471 473
         try:
88  tests/modeltests/model_forms/models.py
@@ -64,6 +64,13 @@ class TextFile(models.Model):
64 64
 
65 65
     def __unicode__(self):
66 66
         return self.description
  67
+        
  68
+class ImageFile(models.Model):
  69
+    description = models.CharField(max_length=20)
  70
+    image = models.FileField(upload_to=tempfile.gettempdir())
  71
+    
  72
+    def __unicode__(self):
  73
+        return self.description
67 74
 
68 75
 __test__ = {'API_TESTS': """
69 76
 >>> from django import newforms as forms
@@ -718,7 +725,7 @@ def __unicode__(self):
718 725
 ...     class Meta:
719 726
 ...         model = TextFile
720 727
 
721  
-Test conditions when files is either not given or empty.
  728
+# Test conditions when files is either not given or empty.
722 729
 
723 730
 >>> f = TextFileForm(data={'description': u'Assistance'})
724 731
 >>> f.is_valid()
@@ -727,7 +734,7 @@ def __unicode__(self):
727 734
 >>> f.is_valid()
728 735
 False
729 736
 
730  
-Upload a file and ensure it all works as expected.
  737
+# Upload a file and ensure it all works as expected.
731 738
 
732 739
 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test1.txt', 'content': 'hello world'}})
733 740
 >>> f.is_valid()
@@ -738,8 +745,8 @@ def __unicode__(self):
738 745
 >>> instance.file
739 746
 u'.../test1.txt'
740 747
 
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.
743 750
 
744 751
 >>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance)
745 752
 >>> f.is_valid()
@@ -750,11 +757,11 @@ def __unicode__(self):
750 757
 >>> instance.file
751 758
 u'.../test1.txt'
752 759
 
753  
-Delete the current file since this is not done by Django.
  760
+# Delete the current file since this is not done by Django.
754 761
 
755 762
 >>> os.unlink(instance.get_file_filename())
756 763
 
757  
-Override the file by uploading a new one.
  764
+# Override the file by uploading a new one.
758 765
 
759 766
 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test2.txt', 'content': 'hello world'}}, instance=instance)
760 767
 >>> f.is_valid()
@@ -765,7 +772,7 @@ def __unicode__(self):
765 772
 
766 773
 >>> instance.delete()
767 774
 
768  
-Test the non-required FileField
  775
+# Test the non-required FileField
769 776
 
770 777
 >>> f = TextFileForm(data={'description': u'Assistance'})
771 778
 >>> f.fields['file'].required = False
@@ -782,4 +789,71 @@ def __unicode__(self):
782 789
 >>> instance.file
783 790
 u'.../test3.txt'
784 791
 >>> 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()
  807
+True
  808
+>>> type(f.cleaned_data['image'])
  809
+<class 'django.newforms.fields.UploadedFile'>
  810
+>>> instance = f.save()
  811
+>>> instance.image
  812
+u'.../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()
  819
+True
  820
+>>> f.cleaned_data['image']
  821
+u'.../test.png'
  822
+>>> instance = f.save()
  823
+>>> instance.image
  824
+u'.../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()
  834
+True
  835
+>>> instance = f.save()
  836
+>>> instance.image
  837
+u'.../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()
  846
+True
  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()
  853
+True
  854
+>>> instance = f.save()
  855
+>>> instance.image
  856
+u'.../test3.png'
  857
+>>> instance.delete()
  858
+
785 859
 """}
BIN  tests/modeltests/model_forms/test.png

0 notes on commit 7552782

Please sign in to comment.
Something went wrong with that request. Please try again.