user_media.models: 38 total statements, 100.0% covered

Generated: Wed 2014-02-05 09:19 CET

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