user_media.models: 17 total statements, 100.0% covered

Generated: Mon 2013-11-25 14:30 CET

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

Stats: 11 executed, 0 missed, 6 excluded, 44 ignored

  1. """Models for the ``django-user-media`` app."""
  2. import os
  3. import uuid
  4. from django.contrib.contenttypes import generic
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.db import models
  7. from django.utils.translation import ugettext_lazy as _
  8. def get_image_file_path(instance, filename):
  9. """Returns a unique filename for images."""
  10. ext = filename.split('.')[-1]
  11. filename = '%s.%s' % (uuid.uuid4(), ext)
  12. return os.path.join(
  13. 'user_media', str(instance.user.pk), 'images', filename)
  14. class UserMediaImage(models.Model):
  15. """
  16. An image that can be uploaded by a user.
  17. If the image belongs to a certain object that is owned by the user, it
  18. can be tied to that object using the generic foreign key. That object
  19. must have a foreign key to ``auth.User`` and that field must be called
  20. ``user``.
  21. :user: The user this image belongs to.
  22. :content_type: If this image belongs to a certain object (i.e. a Vehicle),
  23. this should be the object's ContentType.
  24. :object_id: If this image belongs to a certain object (i.e. a Vehicle),
  25. this should be the object's ID.
  26. :image: The uploaded image.
  27. :position: The position of the image in case of multiple ones.
  28. """
  29. user = models.ForeignKey(
  30. 'auth.User',
  31. verbose_name=_('User'),
  32. )
  33. content_type = models.ForeignKey(
  34. ContentType,
  35. null=True, blank=True,
  36. )
  37. object_id = models.PositiveIntegerField(
  38. null=True, blank=True
  39. )
  40. content_object = generic.GenericForeignKey('content_type', 'object_id')
  41. image = models.ImageField(
  42. upload_to=get_image_file_path,
  43. null=True, blank=True,
  44. verbose_name=_('Image'),
  45. )
  46. generic_position = generic.GenericRelation(
  47. 'generic_positions.ObjectPosition'
  48. )