|
1 import os |
|
2 import tempfile |
|
3 |
|
4 # Try to import PIL in either of the two ways it can end up installed. |
|
5 # Checking for the existence of Image is enough for CPython, but for PyPy, |
|
6 # you need to check for the underlying modules. |
|
7 |
|
8 try: |
|
9 from PIL import Image, _imaging |
|
10 except ImportError: |
|
11 try: |
|
12 import Image, _imaging |
|
13 except ImportError: |
|
14 Image = None |
|
15 |
|
16 from django.core.files.storage import FileSystemStorage |
|
17 from django.db import models |
|
18 from django.db.models.fields.files import ImageFieldFile, ImageField |
|
19 |
|
20 |
|
21 class Foo(models.Model): |
|
22 a = models.CharField(max_length=10) |
|
23 d = models.DecimalField(max_digits=5, decimal_places=3) |
|
24 |
|
25 def get_foo(): |
|
26 return Foo.objects.get(id=1) |
|
27 |
|
28 class Bar(models.Model): |
|
29 b = models.CharField(max_length=10) |
|
30 a = models.ForeignKey(Foo, default=get_foo) |
|
31 |
|
32 class Whiz(models.Model): |
|
33 CHOICES = ( |
|
34 ('Group 1', ( |
|
35 (1,'First'), |
|
36 (2,'Second'), |
|
37 ) |
|
38 ), |
|
39 ('Group 2', ( |
|
40 (3,'Third'), |
|
41 (4,'Fourth'), |
|
42 ) |
|
43 ), |
|
44 (0,'Other'), |
|
45 ) |
|
46 c = models.IntegerField(choices=CHOICES, null=True) |
|
47 |
|
48 class BigD(models.Model): |
|
49 d = models.DecimalField(max_digits=38, decimal_places=30) |
|
50 |
|
51 class BigS(models.Model): |
|
52 s = models.SlugField(max_length=255) |
|
53 |
|
54 class BigInt(models.Model): |
|
55 value = models.BigIntegerField() |
|
56 null_value = models.BigIntegerField(null = True, blank = True) |
|
57 |
|
58 class Post(models.Model): |
|
59 title = models.CharField(max_length=100) |
|
60 body = models.TextField() |
|
61 |
|
62 class NullBooleanModel(models.Model): |
|
63 nbfield = models.NullBooleanField() |
|
64 |
|
65 class BooleanModel(models.Model): |
|
66 bfield = models.BooleanField() |
|
67 string = models.CharField(max_length=10, default='abc') |
|
68 |
|
69 ############################################################################### |
|
70 # ImageField |
|
71 |
|
72 # If PIL available, do these tests. |
|
73 if Image: |
|
74 class TestImageFieldFile(ImageFieldFile): |
|
75 """ |
|
76 Custom Field File class that records whether or not the underlying file |
|
77 was opened. |
|
78 """ |
|
79 def __init__(self, *args, **kwargs): |
|
80 self.was_opened = False |
|
81 super(TestImageFieldFile, self).__init__(*args,**kwargs) |
|
82 def open(self): |
|
83 self.was_opened = True |
|
84 super(TestImageFieldFile, self).open() |
|
85 |
|
86 class TestImageField(ImageField): |
|
87 attr_class = TestImageFieldFile |
|
88 |
|
89 # Set up a temp directory for file storage. |
|
90 temp_storage_dir = tempfile.mkdtemp() |
|
91 temp_storage = FileSystemStorage(temp_storage_dir) |
|
92 temp_upload_to_dir = os.path.join(temp_storage.location, 'tests') |
|
93 |
|
94 class Person(models.Model): |
|
95 """ |
|
96 Model that defines an ImageField with no dimension fields. |
|
97 """ |
|
98 name = models.CharField(max_length=50) |
|
99 mugshot = TestImageField(storage=temp_storage, upload_to='tests') |
|
100 |
|
101 class PersonWithHeight(models.Model): |
|
102 """ |
|
103 Model that defines an ImageField with only one dimension field. |
|
104 """ |
|
105 name = models.CharField(max_length=50) |
|
106 mugshot = TestImageField(storage=temp_storage, upload_to='tests', |
|
107 height_field='mugshot_height') |
|
108 mugshot_height = models.PositiveSmallIntegerField() |
|
109 |
|
110 class PersonWithHeightAndWidth(models.Model): |
|
111 """ |
|
112 Model that defines height and width fields after the ImageField. |
|
113 """ |
|
114 name = models.CharField(max_length=50) |
|
115 mugshot = TestImageField(storage=temp_storage, upload_to='tests', |
|
116 height_field='mugshot_height', |
|
117 width_field='mugshot_width') |
|
118 mugshot_height = models.PositiveSmallIntegerField() |
|
119 mugshot_width = models.PositiveSmallIntegerField() |
|
120 |
|
121 class PersonDimensionsFirst(models.Model): |
|
122 """ |
|
123 Model that defines height and width fields before the ImageField. |
|
124 """ |
|
125 name = models.CharField(max_length=50) |
|
126 mugshot_height = models.PositiveSmallIntegerField() |
|
127 mugshot_width = models.PositiveSmallIntegerField() |
|
128 mugshot = TestImageField(storage=temp_storage, upload_to='tests', |
|
129 height_field='mugshot_height', |
|
130 width_field='mugshot_width') |
|
131 |
|
132 class PersonTwoImages(models.Model): |
|
133 """ |
|
134 Model that: |
|
135 * Defines two ImageFields |
|
136 * Defines the height/width fields before the ImageFields |
|
137 * Has a nullalble ImageField |
|
138 """ |
|
139 name = models.CharField(max_length=50) |
|
140 mugshot_height = models.PositiveSmallIntegerField() |
|
141 mugshot_width = models.PositiveSmallIntegerField() |
|
142 mugshot = TestImageField(storage=temp_storage, upload_to='tests', |
|
143 height_field='mugshot_height', |
|
144 width_field='mugshot_width') |
|
145 headshot_height = models.PositiveSmallIntegerField( |
|
146 blank=True, null=True) |
|
147 headshot_width = models.PositiveSmallIntegerField( |
|
148 blank=True, null=True) |
|
149 headshot = TestImageField(blank=True, null=True, |
|
150 storage=temp_storage, upload_to='tests', |
|
151 height_field='headshot_height', |
|
152 width_field='headshot_width') |
|
153 |
|
154 ############################################################################### |