Test coverage report: user_media.models

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

Generated: Tue 2014-07-15 16:34 CEST

Source file: /home/dkaufhold/projects/django-user-media/src/user_media/models.py

Stats: 31 executed, 0 missed, 7 excluded, 86 ignored

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