From a8edc17c1895f135caeb9e4f8d3587dc618e84b9 Mon Sep 17 00:00:00 2001 From: Mathias Leimgruber Date: Fri, 1 Sep 2023 11:23:34 -0400 Subject: [PATCH] Make sure the hash key changes after cropping an image. --- CHANGES.rst | 4 +++ src/plone/app/imagecropping/storage.py | 7 +++++ .../app/imagecropping/tests/test_cropping.py | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 463cff00..9d90e496 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -18,6 +18,10 @@ Bug fixes: [petschki] (#0) +- Make sure the hash key changes after cropping an image. + [mathias.leimgruber] + + Internal: diff --git a/src/plone/app/imagecropping/storage.py b/src/plone/app/imagecropping/storage.py index 4da83ace..0c684863 100644 --- a/src/plone/app/imagecropping/storage.py +++ b/src/plone/app/imagecropping/storage.py @@ -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 @@ -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): diff --git a/src/plone/app/imagecropping/tests/test_cropping.py b/src/plone/app/imagecropping/tests/test_cropping.py index 057394ab..870ace54 100644 --- a/src/plone/app/imagecropping/tests/test_cropping.py +++ b/src/plone/app/imagecropping/tests/test_cropping.py @@ -9,6 +9,7 @@ from zope.annotation.interfaces import IAnnotations from zope.lifecycleevent import ObjectModifiedEvent +import transaction import unittest @@ -84,6 +85,33 @@ 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"""