PageRenderTime 147ms CodeModel.GetById 128ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/regressiontests/model_fields/models.py

https://code.google.com/p/mango-py/
Python | 160 lines | 102 code | 26 blank | 32 comment | 1 complexity | dcc8116d0e6cb750ada7eb767d81da98 MD5 | raw file
  1import os
  2import 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
  8try:
  9    from PIL import Image, _imaging
 10except ImportError:
 11    try:
 12        import Image, _imaging
 13    except ImportError:
 14        Image = None
 15
 16from django.core.files.storage import FileSystemStorage
 17from django.db import models
 18from django.db.models.fields.files import ImageFieldFile, ImageField
 19
 20
 21class Foo(models.Model):
 22    a = models.CharField(max_length=10)
 23    d = models.DecimalField(max_digits=5, decimal_places=3)
 24
 25def get_foo():
 26    return Foo.objects.get(id=1)
 27
 28class Bar(models.Model):
 29    b = models.CharField(max_length=10)
 30    a = models.ForeignKey(Foo, default=get_foo)
 31
 32class 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
 48class BigD(models.Model):
 49    d = models.DecimalField(max_digits=38, decimal_places=30)
 50
 51class BigS(models.Model):
 52    s = models.SlugField(max_length=255)
 53
 54class BigInt(models.Model):
 55    value = models.BigIntegerField()
 56    null_value = models.BigIntegerField(null = True, blank = True)
 57
 58class Post(models.Model):
 59    title = models.CharField(max_length=100)
 60    body = models.TextField()
 61
 62class NullBooleanModel(models.Model):
 63    nbfield = models.NullBooleanField()
 64
 65class BooleanModel(models.Model):
 66    bfield = models.BooleanField()
 67    string = models.CharField(max_length=10, default='abc')
 68
 69###############################################################################
 70# FileField
 71
 72class Document(models.Model):
 73    myfile = models.FileField(upload_to='unused')
 74
 75###############################################################################
 76# ImageField
 77
 78# If PIL available, do these tests.
 79if Image:
 80    class TestImageFieldFile(ImageFieldFile):
 81        """
 82        Custom Field File class that records whether or not the underlying file
 83        was opened.
 84        """
 85        def __init__(self, *args, **kwargs):
 86            self.was_opened = False
 87            super(TestImageFieldFile, self).__init__(*args,**kwargs)
 88        def open(self):
 89            self.was_opened = True
 90            super(TestImageFieldFile, self).open()
 91
 92    class TestImageField(ImageField):
 93        attr_class = TestImageFieldFile
 94
 95    # Set up a temp directory for file storage.
 96    temp_storage_dir = tempfile.mkdtemp()
 97    temp_storage = FileSystemStorage(temp_storage_dir)
 98    temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
 99
100    class Person(models.Model):
101        """
102        Model that defines an ImageField with no dimension fields.
103        """
104        name = models.CharField(max_length=50)
105        mugshot = TestImageField(storage=temp_storage, upload_to='tests')
106
107    class PersonWithHeight(models.Model):
108        """
109        Model that defines an ImageField with only one dimension field.
110        """
111        name = models.CharField(max_length=50)
112        mugshot = TestImageField(storage=temp_storage, upload_to='tests',
113                                 height_field='mugshot_height')
114        mugshot_height = models.PositiveSmallIntegerField()
115
116    class PersonWithHeightAndWidth(models.Model):
117        """
118        Model that defines height and width fields after the ImageField.
119        """
120        name = models.CharField(max_length=50)
121        mugshot = TestImageField(storage=temp_storage, upload_to='tests',
122                                 height_field='mugshot_height',
123                                 width_field='mugshot_width')
124        mugshot_height = models.PositiveSmallIntegerField()
125        mugshot_width = models.PositiveSmallIntegerField()
126
127    class PersonDimensionsFirst(models.Model):
128        """
129        Model that defines height and width fields before the ImageField.
130        """
131        name = models.CharField(max_length=50)
132        mugshot_height = models.PositiveSmallIntegerField()
133        mugshot_width = models.PositiveSmallIntegerField()
134        mugshot = TestImageField(storage=temp_storage, upload_to='tests',
135                                 height_field='mugshot_height',
136                                 width_field='mugshot_width')
137
138    class PersonTwoImages(models.Model):
139        """
140        Model that:
141        * Defines two ImageFields
142        * Defines the height/width fields before the ImageFields
143        * Has a nullalble ImageField
144        """
145        name = models.CharField(max_length=50)
146        mugshot_height = models.PositiveSmallIntegerField()
147        mugshot_width = models.PositiveSmallIntegerField()
148        mugshot = TestImageField(storage=temp_storage, upload_to='tests',
149                                 height_field='mugshot_height',
150                                 width_field='mugshot_width')
151        headshot_height = models.PositiveSmallIntegerField(
152                blank=True, null=True)
153        headshot_width = models.PositiveSmallIntegerField(
154                blank=True, null=True)
155        headshot = TestImageField(blank=True, null=True,
156                                  storage=temp_storage, upload_to='tests',
157                                  height_field='headshot_height',
158                                  width_field='headshot_width')
159
160###############################################################################