Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django-filer backend #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions image_cropping/backends/flr.py
Original file line number Diff line number Diff line change
@@ -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