From cb56db0fb3ee2174a48bd1dfd00c9c8e197a19a9 Mon Sep 17 00:00:00 2001 From: bee-keeper Date: Tue, 21 Jun 2016 21:24:47 +0100 Subject: [PATCH] django-filer backend --- image_cropping/backends/flr.py | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 image_cropping/backends/flr.py diff --git a/image_cropping/backends/flr.py b/image_cropping/backends/flr.py new file mode 100644 index 00000000..52a3a6af --- /dev/null +++ b/image_cropping/backends/flr.py @@ -0,0 +1,59 @@ +""" +Backend for django-filer + +https://github.com/divio/django-filer + +Usage: Subsititute FilerImageField for CroppableFilerImageField +""" + +from filer.fields.image import ( + AdminImageWidget, + AdminImageFormField, FilerImageField, +) +from filer.models import File +from easy_thumbnails.files import get_thumbnailer +from easy_thumbnails.source_generators import pil_image + +from ..widgets import CropWidget +from .base import ImageBackend + + +class FilerCropWidget(AdminImageWidget, CropWidget): + + def render(self, name, value, attrs=None): + if value: + file_obj = File.objects.get(pk=value) + attrs = attrs or {} + attrs.update({ + 'class': 'crop-thumb', + 'data-thumbnail-url': + file_obj.thumbnails['admin_sidebar_preview'], + 'data-field-name': name, + 'data-org-width': file_obj.width, + 'data-org-height': file_obj.height, + 'style': 'display:none', + + }) + return super(FilerCropWidget, self).render(name, value, attrs) + + +class CroppableFormField(AdminImageFormField): + widget = FilerCropWidget + + +class CroppableFilerImageField(FilerImageField): + default_form_class = CroppableFormField + + +class FilerBackend(ImageBackend): + version_suffix = 'crop' + + WIDGETS = dict(ImageBackend.WIDGETS) + WIDGETS['CroppableFilerImageField'] = FilerCropWidget + + def get_thumbnail_url(self, image_path, thumbnail_options): + thumb = get_thumbnailer(image_path) + return thumb.get_thumbnail(thumbnail_options).url + + def get_size(self, image): + return pil_image(image).size