Skip to content

Commit

Permalink
Make sure the hash key changes after cropping an image.
Browse files Browse the repository at this point in the history
  • Loading branch information
maethu committed Sep 1, 2023
1 parent d19e808 commit e64c9b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Bug fixes:
[petschki] (#0)


- Make sure the hash key changes after cropping an image.
[mathias.leimgruber]


Internal:


Expand Down
7 changes: 7 additions & 0 deletions src/plone/app/imagecropping/storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from Acquisition import aq_base
from persistent.dict import PersistentDict
from plone.app.imagecropping import PAI_STORAGE_KEY
from plone.app.imagecropping.events import CroppingInfoChangedEvent
Expand Down Expand Up @@ -44,6 +45,12 @@ def store(self, fieldname, scale, box):
self.remove(fieldname, scale)
key = self._key(fieldname, scale)
self._storage[key] = box

context = aq_base(self.context)
field = getattr(context, fieldname, None)
if field is not None:
field._p_changed = True # Force a new hash key

notify(CroppingInfoChangedEvent(self.context))

def read(self, fieldname, scale):
Expand Down
29 changes: 29 additions & 0 deletions src/plone/app/imagecropping/tests/test_cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from zope.annotation.interfaces import IAnnotations
from zope.lifecycleevent import ObjectModifiedEvent

import transaction
import unittest


Expand Down Expand Up @@ -84,6 +85,34 @@ def test_accessing_images(self):
"imagescaling does not return cropped image",
)


def test_create_new_key_hash_for_copped_images(self):
"""Even though the origunal image did not change,
the cache key needs to change, otherwise cache proxies and browser
don't about the 'new' cropped image.
Since the original image does not change, we need to force update
the modification (_p_mtime) on the field
Hint: the has key has the modification time included"""

view = self.img.restrictedTraverse("@@crop-image")
view._crop(fieldname="image", scale="thumb", box=(14, 14, 218, 218))
transaction.commit() # needed in order to have a _p_mtime on objects

# another use-case: call plone.app.imaging's ImageScaling view
thumb2 = self.img.restrictedTraverse("@@images")
tag_thumb2 = thumb2.tag(scale="thumb")

view._crop(fieldname="image", scale="thumb", box=(14, 14, 100, 100))
transaction.commit()

thumb3 = self.img.restrictedTraverse("@@images")
tag_thumb3 = thumb3.tag(scale="thumb")

self.assertNotEqual(tag_thumb2, tag_thumb3)


def test_image_formats(self):
"""make sure the scales have the same format as the original image"""

Expand Down

0 comments on commit e64c9b9

Please sign in to comment.