My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ImageModel  
Updated Jul 10, 2009 by justin.d...@gmail.com

The ImageModel abstract base class encapsulates all of the core functionality of the Photologue Photo model. Django models that inherit from this class will be augmented with the basic fields and behavior required for them to provide image resizing and cacheing, view counts, enchancements and effects, reflections, watermarks, rotation, etc.

Fields

The following fields will be added to any model inheriting from the ImageModel base class. These field names will become reserved and can not be overridden in your subclass:

image

Type: ImageField

Stores the file path of the original image.

date_taken

Type: DateTimeField

The date the photo was originally taken. Photologue will attempt to determine this from the images EXIF data but will fall back to the date the model was created.

view_count

Type: PositiveIntegerField

The number of times a PhotoSize with increment_count set to true has been requested for this image.

crop_from

Type: CharField

The position that this image will be cropped from if required.

top
left center right
bottom

effect

Type: ForeignKey

Explicitly applies a specific PhotoEffect to this image. This setting will override any effect applied to the PhotoSize requested.

Commonly Used Methods

For the following methods the text "SIZE" should be replaced with the "name" of the desired PhotoSize.

get_SIZE_photosize

Returns the PhotoSize model with that name.

    >>> myphoto.get_thumbnail_photosize()
    <PhotoSize: thumbnail>    

get_SIZE_size

Returns a tuple containing the actual width and height of the image file on disk in pixels. The file will be generated and cached if not already available.

    >>> myphoto.get_thumbnail_size()
    (150, 75)

get_SIZE_url

Returns a relative URL for the size specified. The file will be generated and cached if not already available.

    >>> myphoto.get_thumbnail_url()
    u'/media/photologue/photos/cache/myfile_thumbnail.jpg'

This is also the method you should use to insert sized photos into your templates:

    {% for photo in photos %}
        <img src="{{ photo.get_thumbnail_url }}">
    {% endfor %}

get_SIZE_filename

Returns the path to the file on disk. The file will NOT be generated if it does not already exists.

Admin Integration

When you inherit from the ImageModel class you get Photologues admin thumbnails for free. Just add "admin_thumbnail" to the "list_display" property of your ModelAdmin class:

    from myapp.models import MyPhoto
    
    class MyPhotoAdmin(admin.ModelAdmin):
        list_display = ['title', 'admin_thumbnail']
        
    admin.site.register(MyPhoto, MyPhotoAdmin)

Example

Here is a (very) simple, but real world, example of a custom model that inherits from the ImageModel abstract class.

    from django.contrib.auth.models import User
    from photologue.models import ImageModel
    
    class UserPortrait(ImageModel):
        user = models.OneToOneField(User, primary_key=True)        

Now we can create a new PhotoSize through the shell or admin interface:

    >>> from photologue.models import PhotoSize
    >>> PhotoSize.objects.create(name='avatar', width=60, height=60, crop=True)

All that's left is to add our new user portraits in a template

    <div id="user_info">
        <h1>{{ user.get_full_name }}</h1>
        <img id="user_avatar" src="{{ user.userportrait.get_avatar_url }}">
    </div>
Comment by taoy...@gmail.com, Aug 13, 2009

I create my models inherit ImageModel named Avatar

Avatar._meta.get_field('image').upload_to = 'Avatar'

but it can't change the path save avatar image. My app has two model inherit ImageModel, so I want to save the picture different folder, what can I do?

Comment by themax...@gmail.com, Jan 8, 2010

I suggest to change photologue/models.py in the folowing way:

if PHOTOLOGUE_PATH is not None:
    if callable(PHOTOLOGUE_PATH):
        get_storage_path = PHOTOLOGUE_PATH
    else:
        parts = PHOTOLOGUE_PATH.split('.')
        module_name = '.'.join(parts[:-1])
        module = __import__(module_name)
        get_storage_path = getattr(module, parts[-1])
else:
    def get_storage_path(instance, filename):
        dirname = 'photos'
        if hasattr(instance, 'upload_to'):
            if callable(instance.upload_to):
                dirname = instance.upload_to()
            else: dirname = instance.upload_to
        return os.path.join(PHOTOLOGUE_DIR, 
                        os.path.normpath( force_unicode(datetime.now().strftime(smart_str(dirname))) ), 
                        filename)

It allow to do the following in derived models:

class NewsImage(ImageModel):
    news = models.OneToOneField(News, primary_key=True)
    class Meta:
        verbose_name = u'Иллюстрация новости'
        verbose_name_plural = u'Иллюстрации новостей'
    def upload_to(self):
        return 'news/%Y/%m/%d'

It is also possible to define 'upload_to' attribute.

Comment by project member justin.d...@gmail.com, Jan 12, 2010

My newer library django-imagekit http://bitbucket.org/jdriscoll/django-imagekit) was designed for this use-case.

Comment by filipa.a...@gmail.com, Apr 15, 2011

Is this comment beeing considered in photologue development?

I would really love to have the upload_to feature.

Thanks

Comment by nai...@gmail.com, Dec 21, 2011

I'd really like to have a way to add a (many-to-many) field to an ImageModel, but still have Photo inherit from it and use it in a photologue gallery. Would it be possible to add something similar to the "ZINNIA_ENTRY_BASE_MODEL" used in the Zinnia django blog? In Zinnia we can set that base model in settings.py to something like MyBlogEntry?, define a MyBlogEntry? that inherits from EntryAbstractClass?, and then Entry automatically inherits from MyBlogEntry? as per the setting.

If we could have a setting such as PHOTOLOGUE_PHOTO_BASE_MODEL=MyPhoto?, define a MyPhoto?(ImageModel), and have Photo inherit from MyPhoto? as per the setting, then we would be able to add fields to the Photo and override PhotoAdmin? as needed.

Sure I could use imagekit, but then I would need to write the gallery part myself.

Or is there a better way?


Sign in to add a comment
Powered by Google Project Hosting