user_media.models: 16 total statements, 100.0% covered

Generated: Thu 2013-03-14 13:13 SGT

Source file: /Users/martin/Repos/django-user-media/user_media/models.py

Stats: 10 executed, 0 missed, 6 excluded, 40 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. """
  28. user = models.ForeignKey(
  29. 'auth.User',
  30. verbose_name=_('User'),
  31. )
  32. content_type = models.ForeignKey(
  33. ContentType,
  34. null=True, blank=True,
  35. )
  36. object_id = models.PositiveIntegerField(
  37. null=True, blank=True
  38. )
  39. content_object = generic.GenericForeignKey('content_type', 'object_id')
  40. image = models.ImageField(
  41. upload_to=get_image_file_path,
  42. null=True, blank=True,
  43. verbose_name=_('Image'),
  44. )