Skip to content

Commit

Permalink
Merge pull request #6 from IMIO/WEB-3985
Browse files Browse the repository at this point in the history
New portrait / paysage scales & logic
  • Loading branch information
laulaz authored Oct 25, 2023
2 parents b631844 + 9125b60 commit 05aba17
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/imio/directory/core/contents/contact/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class ContactCroppingProvider(BaseCroppingProvider):
def get_scales(self, fieldname, request=None):
if fieldname == "image":
# scale used for lead image field
return ["vignette"]
return [
"portrait_affiche",
"paysage_affiche",
]
return []


Expand Down
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
33 changes: 25 additions & 8 deletions src/imio/directory/core/tests/test_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
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.dexterity.behaviors.metadata import IBasic
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 +22,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 @@ -424,14 +429,6 @@ def test_js_bundles(self):
self.assertEqual(len(bundles), 2)
self.assertListEqual(bundles, ["spotlightjs", "flexbin"])

def test_subscriber_to_select_current_entity(self):
contact = api.content.create(
container=self.entity,
type="imio.directory.Contact",
title="My contact",
)
self.assertEqual(contact.selected_entities, [self.entity.UID()])

def test_referrer_entities(self):
setRoles(self.portal, TEST_USER_ID, ["Manager"])
intids = getUtility(IIntIds)
Expand Down Expand Up @@ -471,3 +468,23 @@ 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(IBasic, "IBasic.title"))
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, {})
7 changes: 5 additions & 2 deletions src/imio/directory/core/tests/test_cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ def setUp(self):
def test_cropping_adapter(self):
adapter = ICropping(self.contact, alternate=None)
self.assertIsNotNone(adapter)
self.assertEqual(adapter.get_scales("image", self.request), ["vignette"])
self.assertEqual(
adapter.get_scales("image", self.request),
["portrait_affiche", "paysage_affiche"],
)
self.assertEqual(adapter.get_scales("logo", self.request), [])

def test_cropping_view(self):
cropping_view = getMultiAdapter(
(self.contact, self.request), name="croppingeditor"
)
self.assertEqual(len(list(cropping_view._scales("image"))), 1)
self.assertEqual(len(list(cropping_view._scales("image"))), 2)
self.assertEqual(len(list(cropping_view._scales("logo"))), 0)
9 changes: 2 additions & 7 deletions src/imio/directory/core/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
# -*- coding: utf-8 -*-

from imio.directory.core.testing import IMIO_DIRECTORY_CORE_INTEGRATION_TESTING
from imio.directory.core.tests.utils import make_named_image
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.namedfile.file import NamedBlobImage
from zope.component import getMultiAdapter

import os
import unittest


def image(filename):
file_path = os.path.join(os.path.dirname(__file__), filename)
return NamedBlobImage(data=open(file_path, "rb").read(), filename=file_path)


class TestUtils(unittest.TestCase):
layer = IMIO_DIRECTORY_CORE_INTEGRATION_TESTING

Expand Down Expand Up @@ -59,7 +54,7 @@ def test_export_to_vcard(self):
str(view.export_contact_to_vcard()),
"BEGIN:VCARD\r\nVERSION:3.0\r\nADR:1;;My street;;;5000;Belgium\r\nEMAIL;TYPE=home:test@imio.be\r\nFN:contact\r\nTEL;TYPE=cell:+32496111111\r\nURL;TYPE=website:https://www.imio.be\r\nEND:VCARD\r\n",
)
contact.logo = image("resources/logo.png")
contact.logo = NamedBlobImage(**make_named_image())
self.assertIn(
"PHOTO;ENCODING=B;TYPE=IMAGE/JPEG:", view.export_contact_to_vcard()
)
Expand Down
7 changes: 7 additions & 0 deletions src/imio/directory/core/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ def get_json(json_filename):
) as json_file:
result = json.load(json_file)
return result


def make_named_image(filename="logo.png"):
path = os.path.join(os.path.dirname(__file__), f"resources/{filename}")
with open(path, "rb") as f:
image_data = f.read()
return {"filename": filename, "data": image_data}

0 comments on commit 05aba17

Please sign in to comment.