Changeset 7814 for django/trunk/tests/modeltests/model_forms
- Timestamp:
- 07/01/08 10:10:51 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/tests/modeltests/model_forms/models.py
r7335 r7814 68 68 class ImageFile(models.Model): 69 69 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()) 71 77 72 78 def __unicode__(self): … … 76 82 >>> from django import newforms as forms 77 83 >>> from django.newforms.models import ModelForm 84 >>> from django.core.files.uploadedfile import SimpleUploadedFile 78 85 79 86 The bare bones, absolutely nothing custom, basic case. … … 793 800 # Upload a file and ensure it all works as expected. 794 801 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')}) 796 803 >>> f.is_valid() 797 804 True … … 802 809 u'...test1.txt' 803 810 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() 815 True 816 >>> type(f.cleaned_data['file']) 817 <class 'django.newforms.fields.UploadedFile'> 818 >>> instance = f.save() 819 >>> instance.file 820 u'...test1.txt' 821 804 822 # Edit an instance that already has the file defined in the model. This will not 805 823 # save the file again, but leave it exactly as it is. … … 815 833 816 834 # Delete the current file since this is not done by Django. 817 818 835 >>> os.unlink(instance.get_file_filename()) 819 836 820 837 # Override the file by uploading a new one. 821 838 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) 823 840 >>> f.is_valid() 824 841 True … … 827 844 u'...test2.txt' 828 845 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() 851 True 852 >>> instance = f.save() 853 >>> instance.file 854 u'...test2.txt' 855 856 # Delete the current file since this is not done by Django. 857 >>> os.unlink(instance.get_file_filename()) 858 829 859 >>> instance.delete() 830 860 … … 839 869 '' 840 870 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) 842 872 >>> f.is_valid() 843 873 True … … 845 875 >>> instance.file 846 876 u'...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() 884 True 885 >>> instance = f.save() 886 >>> instance.file 887 u'...test3.txt' 888 889 # Delete the current file since this is not done by Django. 890 >>> os.unlink(instance.get_file_filename()) 847 891 >>> instance.delete() 848 892 … … 859 903 >>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png")).read() 860 904 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)}) 862 906 >>> f.is_valid() 863 907 True … … 868 912 u'...test.png' 869 913 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() 919 True 920 >>> type(f.cleaned_data['image']) 921 <class 'django.newforms.fields.UploadedFile'> 922 >>> instance = f.save() 923 >>> instance.image 924 u'...test.png' 925 870 926 # Edit an instance that already has the image defined in the model. This will not 871 927 # save the image again, but leave it exactly as it is. … … 886 942 # Override the file by uploading a new one. 887 943 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) 889 945 >>> f.is_valid() 890 946 True … … 893 949 u'...test2.png' 894 950 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() 957 True 958 >>> instance = f.save() 959 >>> instance.image 960 u'...test2.png' 961 962 # Delete the current file since this is not done by Django. 963 >>> os.unlink(instance.get_image_filename()) 895 964 >>> instance.delete() 896 965 … … 905 974 '' 906 975 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() 978 True 979 >>> instance = f.save() 980 >>> instance.image 981 u'...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)}) 908 988 >>> f.is_valid() 909 989 True
