|
ImageModel
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. FieldsThe 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: imageType: ImageField Stores the file path of the original image. date_takenType: 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_countType: PositiveIntegerField The number of times a PhotoSize with increment_count set to true has been requested for this image. crop_fromType: CharField The position that this image will be cropped from if required.
effectType: ForeignKey Explicitly applies a specific PhotoEffect to this image. This setting will override any effect applied to the PhotoSize requested. Commonly Used MethodsFor the following methods the text "SIZE" should be replaced with the "name" of the desired PhotoSize. get_SIZE_photosizeReturns the PhotoSize model with that name. >>> myphoto.get_thumbnail_photosize()
<PhotoSize: thumbnail> get_SIZE_sizeReturns 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_urlReturns 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_filenameReturns the path to the file on disk. The file will NOT be generated if it does not already exists. Admin IntegrationWhen 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)ExampleHere 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>
| |||||||||
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?
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.
My newer library django-imagekit http://bitbucket.org/jdriscoll/django-imagekit) was designed for this use-case.
Is this comment beeing considered in photologue development?
I would really love to have the upload_to feature.
Thanks
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?