Test coverage report: user_media.models

# user_media.models: 45 total statements, 100.0% covered

Generated: Mon 2014-08-04 18:55 CEST

Source file: /home/tobi/Projects/django-user-media/src/user_media/models.py

Stats: 35 executed, 0 missed, 10 excluded, 96 ignored

1. `"""Models for the ``django-user-media`` app."""`
2. `import glob`
3. `import os`
4. `import uuid`
5. ``
6. `from django.conf import settings`
7. `from django.contrib.contenttypes import generic`
8. `from django.contrib.contenttypes.models import ContentType`
9. `from django.db import models`
10. `from django.db.models.signals import post_delete`
11. `from django.dispatch import receiver`
12. `from django.utils.translation import ugettext_lazy as _`
13. ``
14. ``
15. `def get_image_file_path(instance, filename):`
16. `"""Returns a unique filename for images."""`
17. `ext = filename.split('.')[-1]`
18. `filename = '%s.%s' % (uuid.uuid4(), ext)`
19. `return os.path.join(`
20. `'user_media', str(instance.user.pk), 'images', filename)`
21. ``
22. ``
23. `class UserMediaImage(models.Model):`
24. `"""`
25. `An image that can be uploaded by a user.`
26. ``
27. `If the image belongs to a certain object that is owned by the user, it`
28. `can be tied to that object using the generic foreign key. That object`
29. `must have a foreign key to ``auth.User`` (or another user model) and that`
30. `field must be called ``user``.`
31. ``
32. `:user: The user this image belongs to.`
33. `:content_type: If this image belongs to a certain object (i.e. a Vehicle),`
34. `this should be the object's ContentType.`
35. `:object_id: If this image belongs to a certain object (i.e. a Vehicle),`
36. `this should be the object's ID.`
37. `:image: The uploaded image.`
38. `:position: The position of the image in case of multiple ones.`
39. `:thumb_x: Thumbnail starting point on the x-axis.`
40. `:thumb_x2: Thumbnail ending point on the x-axis.`
41. `:thumb_y: Thumbnail starting point on the y-axis.`
42. `:thumb_y2: Thumbnail ending point on the y-axis.`
43. `:thumb_w: Thumbnail width.`
44. `:thumb_h: Thumbnail height.`
45. ``
46. `"""`
47. `user = models.ForeignKey(`
48. `getattr(settings, 'AUTH_USER_MODEL', 'auth.User'),`
49. `verbose_name=_('User'),`
50. `)`
51. ``
52. `content_type = models.ForeignKey(`
53. `ContentType,`
54. `null=True, blank=True,`
55. `)`
56. ``
57. `object_id = models.PositiveIntegerField(`
58. `null=True, blank=True`
59. `)`
60. ``
61. `content_object = generic.GenericForeignKey('content_type', 'object_id')`
62. ``
63. `image = models.ImageField(`
64. `upload_to=get_image_file_path,`
65. `null=True, blank=True,`
66. `verbose_name=_('Image'),`
67. `)`
68. ``
69. `generic_position = generic.GenericRelation(`
70. `'generic_positions.ObjectPosition'`
71. `)`
72. ``
73. `thumb_x = models.PositiveIntegerField(`
74. `verbose_name=_('Thumbnail x'),`
75. `null=True, blank=True,`
76. `)`
77. ``
78. `thumb_x2 = models.PositiveIntegerField(`
79. `verbose_name=_('Thumbnail x2'),`
80. `null=True, blank=True,`
81. `)`
82. ``
83. `thumb_y = models.PositiveIntegerField(`
84. `verbose_name=_('Thumbnail y'),`
85. `null=True, blank=True,`
86. `)`
87. ``
88. `thumb_y2 = models.PositiveIntegerField(`
89. `verbose_name=_('Thumbnail y2'),`
90. `null=True, blank=True,`
91. `)`
92. ``
93. `thumb_w = models.PositiveIntegerField(`
94. `verbose_name=_('Thumbnail width'),`
95. `null=True, blank=True,`
96. `)`
97. ``
98. `thumb_h = models.PositiveIntegerField(`
99. `verbose_name=_('Thumbnail height'),`
100. `null=True, blank=True,`
101. `)`
102. ``
103. `@property`
104. `def box_coordinates(self):`
105. `"""Returns a thumbnail's coordinates."""`
106. `if self.thumb_x and self.thumb_y and self.thumb_x2 and self.thumb_y2:`
107. `return (`
108. `int(self.thumb_x),`
109. `int(self.thumb_y),`
110. `int(self.thumb_x2),`
111. `int(self.thumb_y2),`
112. `)`
113. `return False`
114. ``
115. `def large_size(self, as_string=True):`
116. `"""Returns a thumbnail's large size."""`
117. `size = getattr(settings, 'USER_MEDIA_THUMB_SIZE_LARGE', (150, 150))`
118. `if as_string:`
119. `return u'{}x{}'.format(size[0], size[1])`
120. `return size`
121. ``
122. `def small_size(self, as_string=True):`
123. `"""Returns a thumbnail's small size."""`
124. `size = getattr(settings, 'USER_MEDIA_THUMB_SIZE_SMALL', (95, 95))`
125. `if as_string:`
126. `return u'{}x{}'.format(size[0], size[1])`
127. `return size`
128. ``
129. ``
130. `@receiver(post_delete, sender=UserMediaImage)`
131. `def image_post_delete_handler(sender, instance, **kwargs):`
132. `"""`
133. `Makes sure that a an image is also deleted from the media directory.`
134. ``
135. `This should prevent a load of "dead" image files on disc.`
136. ``
137. `"""`
138. `for f in glob.glob('{}/{}*'.format(instance.image.storage.location,`
139. `instance.image.name)):`
140. `if not os.path.isdir(f):`
141. `instance.image.storage.delete(f)`