mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-16 21:30:23 +00:00
AdminThumbnailView is now AdminThumbnail
I never liked that the "AdminThumbnailView" was supposed to be defined on the model, but never looked into it. This commit puts the definition back where it belongs: in the admin. Instead of requiring you to add a field (with view logic) to your model, you now just add a property to your admin class and specify that property in the `list_display` list.
This commit is contained in:
parent
067217e40e
commit
b9aa69e0c0
5 changed files with 55 additions and 75 deletions
26
README.rst
26
README.rst
|
|
@ -83,35 +83,25 @@ implement a ``process()`` method::
|
|||
Admin
|
||||
-----
|
||||
|
||||
ImageKit also contains a class named ``imagekit.models.AdminThumbnailView``
|
||||
ImageKit also contains a class named ``imagekit.admin.AdminThumbnail``
|
||||
for displaying specs (or even regular ImageFields) in the
|
||||
`Django admin change list`__. Like ``imagekit.models.ImageSpec``,
|
||||
AdminThumbnailView is used as a property on Django model classes::
|
||||
|
||||
from django.db import models
|
||||
from imagekit.models import ImageSpec, AdminThumbnailView
|
||||
from imagekit.processors import resize
|
||||
|
||||
class Photo(models.Model):
|
||||
original_image = models.ImageField(upload_to'photos')
|
||||
thumbnail = ImageSpec([resize.Crop(50, 50)], image_field='original_image')
|
||||
admin_thumbnail_view = AdminThumbnailView(image_field='thumbnail')
|
||||
|
||||
You can then add this property to the `list_display`__ field of your admin
|
||||
class::
|
||||
`Django admin change list`__. AdminThumbnail is used as a property on
|
||||
Django admin classes::
|
||||
|
||||
from django.contrib import admin
|
||||
from imagekit.admin import AdminThumbnail
|
||||
from .models import Photo
|
||||
|
||||
|
||||
class PhotoAdmin(admin.ModelAdmin):
|
||||
list_display = ('__str__', 'admin_thumbnail_view')
|
||||
list_display = ('__str__', 'admin_thumbnail')
|
||||
admin_thumbnail = AdminThumbnail(image_field='thumbnail')
|
||||
|
||||
|
||||
admin.site.register(Photo, PhotoAdmin)
|
||||
|
||||
AdminThumbnailView can even use a custom template. For more information, see
|
||||
``imagekit.models.AdminThumbnailView``.
|
||||
AdminThumbnail can even use a custom template. For more information, see
|
||||
``imagekit.admin.AdminThumbnail``.
|
||||
|
||||
|
||||
__ https://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-change-list
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ API Reference
|
|||
.. automodule:: imagekit.models
|
||||
:members:
|
||||
|
||||
|
||||
:mod:`processors` Module
|
||||
------------------------
|
||||
|
||||
|
|
@ -16,3 +17,10 @@ API Reference
|
|||
|
||||
.. automodule:: imagekit.processors.resize
|
||||
:members:
|
||||
|
||||
|
||||
:mod:`admin` Module
|
||||
--------------------
|
||||
|
||||
.. automodule:: imagekit.admin
|
||||
:members:
|
||||
|
|
|
|||
37
imagekit/admin.py
Normal file
37
imagekit/admin.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
|
||||
class AdminThumbnail(object):
|
||||
"""A convenience utility for adding thumbnails to the Django admin change
|
||||
list.
|
||||
|
||||
"""
|
||||
short_description = _('Thumbnail')
|
||||
allow_tags = True
|
||||
|
||||
def __init__(self, image_field, template=None):
|
||||
"""
|
||||
:param image_field: The name of the ImageField or ImageSpec on the model
|
||||
to use for the thumbnail.
|
||||
:param template: The template with which to render the thumbnail
|
||||
|
||||
"""
|
||||
self.image_field = image_field
|
||||
self.template = template
|
||||
|
||||
def __call__(self, obj):
|
||||
thumbnail = getattr(obj, self.image_field, None)
|
||||
|
||||
if not thumbnail:
|
||||
raise Exception('The property {0} is not defined on {1}.'.format(
|
||||
obj, self.image_field))
|
||||
|
||||
original_image = getattr(thumbnail, 'source_file', None) or thumbnail
|
||||
template = self.template or 'imagekit/admin/thumbnail.html'
|
||||
|
||||
return render_to_string(template, {
|
||||
'model': obj,
|
||||
'thumbnail': thumbnail,
|
||||
'original_image': original_image,
|
||||
})
|
||||
|
|
@ -9,8 +9,6 @@ from django.core.files.base import ContentFile
|
|||
from django.utils.encoding import force_unicode, smart_str
|
||||
from django.db import models
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.template.loader import render_to_string
|
||||
from django.db.models.fields.files import ImageFieldFile
|
||||
|
||||
|
||||
|
|
@ -299,58 +297,6 @@ def _post_delete_handler(sender, instance=None, **kwargs):
|
|||
spec_file._delete()
|
||||
|
||||
|
||||
class AdminThumbnailView(object):
|
||||
"""A convenience utility for adding thumbnails to the Django admin change
|
||||
list.
|
||||
|
||||
"""
|
||||
short_description = _('Thumbnail')
|
||||
allow_tags = True
|
||||
|
||||
def __init__(self, image_field, template=None):
|
||||
"""
|
||||
:param image_field: The name of the ImageField or ImageSpec on the model
|
||||
to use for the thumbnail.
|
||||
:param template: The template with which to render the thumbnail
|
||||
|
||||
"""
|
||||
self.image_field = image_field
|
||||
self.template = template
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self
|
||||
else:
|
||||
return BoundAdminThumbnailView(instance, self)
|
||||
|
||||
|
||||
class BoundAdminThumbnailView(AdminThumbnailView):
|
||||
def __init__(self, model_instance, unbound_field):
|
||||
super(BoundAdminThumbnailView, self).__init__(unbound_field.image_field,
|
||||
unbound_field.template)
|
||||
self.model_instance = model_instance
|
||||
|
||||
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, 'source_file', 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,
|
||||
})
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
"""Override AdminThumbnailView's implementation."""
|
||||
return self
|
||||
|
||||
|
||||
class ProcessedImageFieldFile(ImageFieldFile, _ImageSpecFileMixin):
|
||||
def save(self, name, content, save=True):
|
||||
new_filename = self.field.generate_filename(self.instance, name)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import Image
|
|||
from django.core.files.base import ContentFile
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from imagekit.models import ImageSpec, AdminThumbnailView
|
||||
from imagekit.models import ImageSpec
|
||||
from imagekit.processors.resize import Crop
|
||||
from imagekit.processors import Adjust
|
||||
|
||||
|
|
@ -14,8 +14,7 @@ class Photo(models.Model):
|
|||
original_image = models.ImageField(upload_to='photos')
|
||||
thumbnail = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), Crop(50, 50)],
|
||||
image_field='original_image', format='JPEG', quality=90)
|
||||
admin_thumbnail_view = AdminThumbnailView(image_field='thumbnail')
|
||||
|
||||
|
||||
|
||||
class IKTest(TestCase):
|
||||
""" Base TestCase class """
|
||||
|
|
|
|||
Loading…
Reference in a new issue