Generated: Wed 2013-11-27 14:17 CET
Source file: /home/tobi/Projects/django-user-media/src/user_media/models.py
Stats: 11 executed, 0 missed, 6 excluded, 44 ignored
"""Models for the ``django-user-media`` app."""import osimport uuidfrom django.contrib.contenttypes import genericfrom django.contrib.contenttypes.models import ContentTypefrom django.db import modelsfrom django.utils.translation import ugettext_lazy as _def get_image_file_path(instance, filename): """Returns a unique filename for images.""" ext = filename.split('.')[-1] filename = '%s.%s' % (uuid.uuid4(), ext) return os.path.join( 'user_media', str(instance.user.pk), 'images', filename)class UserMediaImage(models.Model): """ An image that can be uploaded by a user. If the image belongs to a certain object that is owned by the user, it can be tied to that object using the generic foreign key. That object must have a foreign key to ``auth.User`` and that field must be called ``user``. :user: The user this image belongs to. :content_type: If this image belongs to a certain object (i.e. a Vehicle), this should be the object's ContentType. :object_id: If this image belongs to a certain object (i.e. a Vehicle), this should be the object's ID. :image: The uploaded image. :position: The position of the image in case of multiple ones. """ user = models.ForeignKey( 'auth.User', verbose_name=_('User'), ) content_type = models.ForeignKey( ContentType, null=True, blank=True, ) object_id = models.PositiveIntegerField( null=True, blank=True ) content_object = generic.GenericForeignKey('content_type', 'object_id') image = models.ImageField( upload_to=get_image_file_path, null=True, blank=True, verbose_name=_('Image'), ) generic_position = generic.GenericRelation( 'generic_positions.ObjectPosition' )