mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-04-20 13:10:58 +00:00
Added AdminThumbnailView field.
You're no longer restricted to just one, special-case admin thumbnail. Make as many as you want by adding AdminThumbnailView properties to your model and including them in your admin class's `list_display` tuple. You can also provide a custom template. Note that (because this change introduces templates to imagekit), imagekit is now required in INSTALLED_APPS. Ideally we could get this stuff out of the model, but we'll have to look into whether that's possible without making things really complicated.
This commit is contained in:
parent
501d3c7ad3
commit
544d5b874a
4 changed files with 47 additions and 22 deletions
44
imagekit/fields.py
Executable file
44
imagekit/fields.py
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
|
||||
class BoundAdminThumbnailView(object):
|
||||
short_description = _('Thumbnail')
|
||||
allow_tags = True
|
||||
|
||||
def __init__(self, model_instance, image_field, template=None):
|
||||
self.model_instance = model_instance
|
||||
self.image_field = image_field
|
||||
self.template = template
|
||||
|
||||
def __unicode__(self):
|
||||
thumbnail = getattr(self.model_instance, self.image_field, None)
|
||||
|
||||
if not thumbnail:
|
||||
raise Exception('The property {0} is not defined on {1}.'.format(
|
||||
self.model_instance, self.image_field))
|
||||
|
||||
original_image = getattr(thumbnail, '_imgfield', None) or thumbnail
|
||||
template = self.template or 'imagekit/admin/thumbnail.html'
|
||||
|
||||
return render_to_string(template, {
|
||||
'model': self.model_instance,
|
||||
'thumbnail': thumbnail,
|
||||
'original_image': original_image,
|
||||
})
|
||||
|
||||
|
||||
class AdminThumbnailView(object):
|
||||
def __init__(self, image_field, template=None):
|
||||
"""
|
||||
Keyword arguments:
|
||||
image_field -- the name of the ImageField or ImageSpec on the model to
|
||||
use for the thumbnail.
|
||||
template -- the template with which to render the thumbnail
|
||||
|
||||
"""
|
||||
self.image_field = image_field
|
||||
self.template = template
|
||||
|
||||
def __get__(self, obj, type=None):
|
||||
return BoundAdminThumbnailView(obj, self.image_field, self.template)
|
||||
|
|
@ -67,28 +67,9 @@ class ImageModel(models.Model):
|
|||
"""
|
||||
__metaclass__ = ImageModelBase
|
||||
|
||||
admin_thumbnail = defaults.DjangoAdminThumbnail()
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def admin_thumbnail_view(self):
|
||||
if not self._imgfields:
|
||||
return None
|
||||
prop = getattr(self, self._ik.admin_thumbnail_property, None)
|
||||
if prop is None:
|
||||
return 'The property "%s" has not been defined.' % \
|
||||
self._ik.admin_thumbnail_property
|
||||
else:
|
||||
if hasattr(self, 'get_absolute_url'):
|
||||
return u'<a href="%s"><img src="%s"></a>' % \
|
||||
(escape(self.get_absolute_url()), escape(prop.url))
|
||||
else:
|
||||
return u'<a href="%s"><img src="%s"></a>' % \
|
||||
(escape(self._get_imgfield(self).url), escape(prop.url))
|
||||
admin_thumbnail_view.short_description = _('Thumbnail')
|
||||
admin_thumbnail_view.allow_tags = True
|
||||
|
||||
class IKOptions:
|
||||
pass
|
||||
|
||||
|
|
|
|||
|
|
@ -10,9 +10,6 @@ class Options(object):
|
|||
|
||||
"""
|
||||
|
||||
admin_thumbnail_property = 'admin_thumbnail'
|
||||
"""The name of the spec to be used by the admin_thumbnail_view"""
|
||||
|
||||
default_image_field = None
|
||||
"""The name of the image field property on the model.
|
||||
Can be overridden on a per-spec basis by setting the image_field property on
|
||||
|
|
|
|||
3
imagekit/templates/imagekit/admin/thumbnail.html
Normal file
3
imagekit/templates/imagekit/admin/thumbnail.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<a href="{{ model.get_absolute_url|default:original_image.url }}">
|
||||
<img src="{{ thumbnail.url }}">
|
||||
</a>
|
||||
Loading…
Reference in a new issue