Skip to content

Commit

Permalink
Fix lost cropped images scales after content modification
Browse files Browse the repository at this point in the history
This refs IMIO #14901
This is already fixed in Plone 5 but not in Plone 4
See collective/plone.app.imagecropping#21
  • Loading branch information
laulaz committed Feb 15, 2017
1 parent 63b2850 commit 56233bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog
0.8.48 (unreleased)
-------------------

- Fix lost cropped images scales on a content after a modification : #14901
This is already fixed in Plone 5 but not in Plone 4.
See https://github.com/collective/plone.app.imagecropping/issues/21
[laulaz]

- Fix typo for css class.
[bsuttor]

Expand Down
4 changes: 4 additions & 0 deletions cpskin/core/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler=".events.set_lat_lng" />

<subscriber for="plone.app.imagecropping.interfaces.IImageCroppingMarker
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler=".events.apply_crops_after_modify" />

<adapter
for="imio.dashboard.content.pod_template.IDashboardPODTemplate
plone.app.contenttypes.interfaces.ICollection"
Expand Down
28 changes: 28 additions & 0 deletions cpskin/core/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# -*- coding: utf-8 -*-
from plone.app.imagecropping import PAI_STORAGE_KEY
from plone.app.imagecropping.interfaces import IImageCroppingUtils
from zope.annotation.interfaces import IAnnotations
from zope.component import getMultiAdapter
from zope.globalrequest import getRequest

from cpskin.core.utils import get_address_from_obj
from cpskin.core.utils import has_lat_lng
from cpskin.core.utils import set_coord
Expand All @@ -13,3 +19,25 @@ def set_lat_lng(obj, event):
return
request = obj.REQUEST
set_coord(obj, request)


def apply_crops_after_modify(obj, event):
"""
Bug fixed here : cropped images scales on a content are lost after a
modification of this content (#14901).
This is already fixed in Plone 5 but not in Plone 4 :
https://github.com/collective/plone.app.imagecropping/issues/21
To fix this, we need to re-generate all the crops of an object just after
it's modification.
"""
crops = IAnnotations(obj).get(PAI_STORAGE_KEY)
if not crops:
return
croputils = IImageCroppingUtils(obj)
request = getRequest()
cropper = getMultiAdapter((obj, request), name='crop-image')
for fieldname in croputils.image_field_names():
for crop_key in crops:
if crop_key.startswith(fieldname):
scalename = crop_key[len(fieldname) + 1:]
cropper._crop(fieldname, scalename, crops[crop_key])
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'phonenumbers',
'collective.contact.facetednav',
'collective.dexteritytextindexer',
'plone.app.imagecropping',
],
extras_require={
'test': [
Expand Down

0 comments on commit 56233bc

Please sign in to comment.