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