Skip to content

Commit

Permalink
Remove old cropping information when image changes
Browse files Browse the repository at this point in the history
This refs WEB-3985
  • Loading branch information
laulaz committed Oct 25, 2023
1 parent d1e4849 commit bfc631d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/imio/directory/core/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from imio.smartweb.common.faceted.utils import configure_faceted
from imio.smartweb.common.interfaces import IAddress
from imio.smartweb.common.utils import geocode_object
from imio.smartweb.common.utils import remove_cropping
from plone import api
from zope.component import getMultiAdapter
from zope.globalrequest import getRequest
Expand Down Expand Up @@ -56,7 +57,9 @@ def modified_contact(obj, event):
if d.interface is IAddress and d.attributes:
# an address field has been changed
geocode_object(obj)
return
elif "ILeadImageBehavior.image" in d.attributes:
# we need to remove cropping information of previous image
remove_cropping(obj, "image", ["portrait_affiche", "paysage_affiche"])


def modified_entity(obj, event):
Expand Down
20 changes: 20 additions & 0 deletions src/imio/directory/core/tests/test_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from imio.directory.core.contents.contact.content import phone_constraint
from imio.directory.core.interfaces import IImioDirectoryCoreLayer
from imio.directory.core.testing import IMIO_DIRECTORY_CORE_FUNCTIONAL_TESTING
from imio.directory.core.tests.utils import make_named_image
from imio.smartweb.common.utils import geocode_object
from plone import api
from plone.api.exc import InvalidParameterError
from plone.app.contenttypes.behaviors.leadimage import ILeadImageBehavior
from plone.app.imagecropping import PAI_STORAGE_KEY
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.app.testing import TEST_USER_NAME
Expand All @@ -18,6 +21,7 @@
from plone.formwidget.geolocation.geolocation import Geolocation
from plone.i18n.utility import setLanguageBinding
from plone.namedfile.file import NamedBlobFile
from plone.namedfile.file import NamedBlobImage
from plone.restapi.testing import RelativeSession
from plone.testing.zope import Browser
from unittest import mock
Expand Down Expand Up @@ -463,3 +467,19 @@ def test_automaticaly_readd_container_entity_uid(self):
contact.reindexObject()
modified(contact)
self.assertIn(self.entity.UID(), contact.selected_entities)

def test_removing_old_cropping(self):
contact = api.content.create(
container=self.entity,
type="imio.directory.Contact",
id="contact",
)
contact.image = NamedBlobImage(**make_named_image())
view = contact.restrictedTraverse("@@crop-image")
view._crop(fieldname="image", scale="portrait_affiche", box=(1, 1, 200, 200))
annotation = IAnnotations(contact).get(PAI_STORAGE_KEY)
self.assertEqual(annotation, {"image_portrait_affiche": (1, 1, 200, 200)})

modified(contact, Attributes(ILeadImageBehavior, "ILeadImageBehavior.image"))
annotation = IAnnotations(contact).get(PAI_STORAGE_KEY)
self.assertEqual(annotation, {})

0 comments on commit bfc631d

Please sign in to comment.