Changeset 7025
- Timestamp:
- 01/18/08 09:53:19 (6 months ago)
- Files:
-
- django/trunk/django/newforms/fields.py (modified) (1 diff)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (7 diffs)
- django/trunk/tests/modeltests/model_forms/test.png (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/fields.py
r7021 r7025 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 django/trunk/tests/modeltests/model_forms/models.py
r7021 r7025 63 63 file = models.FileField(upload_to=tempfile.gettempdir()) 64 64 65 def __unicode__(self): 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 65 72 def __unicode__(self): 66 73 return self.description … … 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'}) … … 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'}}) … … 739 746 u'.../test1.txt' 740 747 741 Edit an instance that already has the file defined in the model. This will not742 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) … … 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) … … 766 773 >>> instance.delete() 767 774 768 Test the non-required FileField775 # Test the non-required FileField 769 776 770 777 >>> f = TextFileForm(data={'description': u'Assistance'}) … … 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 """}
