Skip to content

Commit

Permalink
WEB-3932 : Transform contact section to contactS section
Browse files Browse the repository at this point in the history
  • Loading branch information
boulch committed Sep 4, 2023
1 parent 49dafbf commit d9a7dd1
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 241 deletions.
18 changes: 13 additions & 5 deletions src/imio/smartweb/core/contents/sections/contact/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
class ISectionContact(ISection):
"""Marker interface and Dexterity Python Schema for SectionContact"""

directives.widget(related_contact=SelectFieldWidget)
related_contact = schema.Choice(
title=_("Related contact"),
directives.widget(related_contacts=SelectFieldWidget)
related_contacts = schema.List(
title=_("Related contacts"),
description=_(
"Select a contact. If you can't find the contact you want, make sure "
"Select contacts. If you can't find contacts you want, make sure "
"""it exists in the directory and that its "state" is published."""
),
source="imio.smartweb.vocabulary.RemoteContacts",
value_type=schema.Choice(vocabulary="imio.smartweb.vocabulary.RemoteContacts"),
required=True,
)

Expand Down Expand Up @@ -61,6 +61,14 @@ class ISectionContact(ISection):
values=[1, 3, 4],
)

nb_contact_by_line = schema.Choice(
title=_("Maximum number of contacts by line"),
description=_("Maximum number of contacts by line (on PC)"),
required=True,
default=4,
values=[1, 2, 3, 4],
)

image_scale = schema.Choice(
title=_("Image scale for images (only for gallery mode)"),
default="preview",
Expand Down
371 changes: 188 additions & 183 deletions src/imio/smartweb/core/contents/sections/contact/view.pt

Large diffs are not rendered by default.

50 changes: 33 additions & 17 deletions src/imio/smartweb/core/contents/sections/contact/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from imio.smartweb.core.utils import hash_md5
from imio.smartweb.locales import SmartwebMessageFactory as _
from plone import api
from plone.memoize.view import memoize
from zope.i18n import translate
from zope.i18nmessageid import MessageFactory

Expand All @@ -21,47 +20,63 @@
class ContactView(HashableJsonSectionView):
"""Contact Section view"""

_current_contact = None

@property
@memoize
def contact(self):
url = "{}/@search?UID={}&fullobjects=1".format(
DIRECTORY_URL, self.context.related_contact
)
def contacts(self):
print("contacts!!!")
if self.context.related_contacts is None:
return
uids = "&UID=".join(self.context.related_contacts)
url = "{}/@search?UID={}&fullobjects=1".format(DIRECTORY_URL, uids)
current_lang = api.portal.get_current_language()[:2]
if current_lang != "fr":
url = f"{url}&translated_in_{current_lang}=1"
self.json_data = get_json(url)
self.refresh_modification_date()
if self.json_data is None or len(self.json_data.get("items", [])) == 0: # NOQA
return
return self.json_data.get("items")[0]
self._current_contact = self.json_data.get("items")[0]
return batch_results(
self.json_data.get("items"), self.context.nb_contact_by_line
)

@property
def current_contact(self):
if self._current_contact is None:
self.contacts
return self._current_contact

def set_current_contact(self, current_contact):
self._current_contact = current_contact
return self._current_contact

@property
def contact_type_class(self):
contact = self.contact
contact = self.current_contact
if contact is None:
return ""
contact_type = contact.get("type").get("token")
return "contact-type-{}".format(contact_type)

@property
def description(self):
contact = self.contact
contact = self.current_contact
if contact is None:
return ""
description = rich_description(contact.get("description"))
return description

def logo(self):
contact = self.contact
contact = self.current_contact
if contact is None or contact.get("logo") is None:
return ""
modified_hash = hash_md5(contact["modified"])
logo = f"{contact['@id']}/@@images/logo/medium?cache_key={modified_hash}"
return logo

def leadimage(self):
contact = self.contact
contact = self.current_contact
if contact is None or contact.get("image") is None:
return ""
modified_hash = hash_md5(contact["modified"])
Expand All @@ -70,8 +85,9 @@ def leadimage(self):

def data_geojson(self):
"""Return the contact geolocation as GeoJSON string."""
contact = self.current_contact
current_lang = api.portal.get_current_language()[:2]
coordinates = self.contact.get("geolocation")
coordinates = contact.get("geolocation")
longitude = coordinates.get("longitude")
latitude = coordinates.get("latitude")
link_text = translate(_("Itinerary"), target_language=current_lang)
Expand All @@ -94,7 +110,7 @@ def data_geojson(self):

@property
def images(self):
contact = self.contact
contact = self.current_contact
if contact is None:
return
contact_url = contact["@id"]
Expand Down Expand Up @@ -134,7 +150,7 @@ def days(self):
)

def get_itinerary_link(self):
contact = self.contact
contact = self.current_contact
if contact is None:
return
address_parts = [
Expand All @@ -153,7 +169,7 @@ def get_itinerary_link(self):

def get_opening_informations(self, a_date=None):
current_date = a_date or date.today()
contact = self.contact
contact = self.current_contact
if contact is None:
return
schedule = contact.get("schedule")
Expand Down Expand Up @@ -220,7 +236,7 @@ def formatted_schedule(self, schedule):
return formatted_schedule(schedule)

def is_empty_schedule(self):
contact = self.contact
contact = self.current_contact
if contact is None:
return
schedule = contact.get("schedule") or {}
Expand All @@ -231,7 +247,7 @@ def is_empty_schedule(self):
return True

def formatted_address(self):
contact = self.contact
contact = self.current_contact
street_parts = [
contact.get("street"),
contact.get("number") and str(contact.get("number")) or "",
Expand Down
2 changes: 1 addition & 1 deletion src/imio/smartweb/core/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<metadata>
<version>1043</version>
<version>1044</version>
<dependencies>
<dependency>profile-plone.app.dexterity:default</dependency>
<dependency>profile-plone.app.imagecropping:default</dependency>
Expand Down
69 changes: 34 additions & 35 deletions src/imio/smartweb/core/tests/test_section_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def test_contact(self, m):
view = queryMultiAdapter((self.page, self.request), name="full_view")
self.assertIn("My contact", view())
contact_view = queryMultiAdapter((contact, self.request), name="view")
self.assertIsNone(contact_view.contact)
self.assertIsNone(contact_view.contacts)
authentic_contact_uid = "2dc381f0fb584381b8e4a19c84f53b35"
contact.related_contact = authentic_contact_uid
contact.related_contacts = [authentic_contact_uid]
contact_search_url = (
"http://localhost:8080/Plone/@search?UID={}&fullobjects=1".format(
authentic_contact_uid
Expand All @@ -68,18 +68,18 @@ def test_contact(self, m):
authentic_contact_uid
)
m.get(contact_search_url, exc=requests.exceptions.ConnectTimeout)
clear_cache(self.request)
self.assertIsNone(contact_view.contact)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.contacts)
m.get(contact_search_url, status_code=404)
clear_cache(self.request)
self.assertIsNone(contact_view.contact)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.contacts)
m.get(contact_search_url, text=json.dumps(self.json_no_contact))
clear_cache(self.request)
self.assertIsNone(contact_view.contact)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.contacts)
self.assertEqual(contact_view.contact_type_class, "")
m.get(contact_search_url, text=json.dumps(self.json_contact))
clear_cache(self.request)
self.assertIsNotNone(contact_view.contact)
contact_view.set_current_contact(None)
self.assertIsNotNone(contact_view.contacts)
self.assertEqual(contact_view.contact_type_class, "contact-type-organization")
self.assertNotIn("contact_titles", view())
self.assertIn("contact_address", view())
Expand Down Expand Up @@ -114,29 +114,28 @@ def test_contact(self, m):

contact.visible_blocks = ["titles", "gallery"]
m.get(contact_images_url, text=json.dumps(self.json_contact_images))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIn("contact_gallery", view())
self.assertEqual(len(contact_view.images[0]), 2)

m.get(contact_search_url, exc=requests.exceptions.ConnectTimeout)
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.images)
m.get(contact_search_url, status_code=404)
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.images)
m.get(contact_search_url, text=json.dumps(self.json_no_contact))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.images)

m.get(contact_search_url, text=json.dumps(self.json_contact))
m.get(contact_images_url, text=json.dumps(self.json_no_image))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.images)
m.get(contact_images_url, status_code=404)
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.images)
m.get(contact_images_url, exc=requests.exceptions.ConnectTimeout)
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.images)

def test_toggle_title_visibility(self):
Expand Down Expand Up @@ -195,14 +194,14 @@ def test_opening_informations(self, m):
contact_view = queryMultiAdapter((contact, self.request), name="view")
self.assertIsNone(contact_view.get_opening_informations())
authentic_contact_uid = "2dc381f0fb584381b8e4a19c84f53b35"
contact.related_contact = authentic_contact_uid
contact.related_contacts = [authentic_contact_uid]
contact_search_url = (
"http://localhost:8080/Plone/@search?UID={}&fullobjects=1".format(
authentic_contact_uid
)
)
m.get(contact_search_url, text=json.dumps(self.json_contact))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNotNone(contact_view.get_opening_informations())

today = datetime.now()
Expand All @@ -214,22 +213,22 @@ def test_opening_informations(self, m):
{"date": today_str, "title": "Exceptional closure !"}
]
m.get(contact_search_url, text=json.dumps(self.json_contact))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNotNone(contact_view.get_opening_informations())

today = datetime.now().strftime("%Y-%m-%d")
self.json_contact["items"][0]["multi_schedule"][0]["dates"] = [
{"end_date": tomorrow_str, "start_date": yesterday_str}
]
m.get(contact_search_url, text=json.dumps(self.json_contact))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNotNone(contact_view.get_opening_informations())

self.json_contact["items"][0]["multi_schedule"][0]["dates"] = [
{"end_date": yesterday_str, "start_date": yesterday_str}
]
m.get(contact_search_url, text=json.dumps(self.json_contact))
clear_cache(self.request)
contact_view.set_current_contact(None)
self.assertIsNotNone(contact_view.get_opening_informations())

# {'afternoonend': '', 'afternoonstart': '', 'comment': 'vendredi : apéro à midi', 'morningend': '11:00', 'morningstart': '08:30'}
Expand Down Expand Up @@ -408,14 +407,14 @@ def test_formatted_with_multi_schedule(self, m):
contact_view = queryMultiAdapter((contact, self.request), name="view")
self.assertIsNone(contact_view.get_opening_informations())
authentic_contact_uid = "2dc381f0fb584381b8e4a19c84f53b35"
contact.related_contact = authentic_contact_uid
contact.related_contacts = [authentic_contact_uid]
contact_search_url = (
"http://localhost:8080/Plone/@search?UID={}&fullobjects=1".format(
authentic_contact_uid
)
)
m.get(contact_search_url, text=json.dumps(self.json_contact))
clear_cache(self.request)
contact_view.set_current_contact(None)
with freeze_time("2021-06-30 12:20:00"):
schedule = contact_view.get_opening_informations()
self.assertEqual(
Expand Down Expand Up @@ -455,14 +454,14 @@ def test_empty_schedule(self, m):
contact_view = queryMultiAdapter((contact, self.request), name="view")
self.assertIsNone(contact_view.get_opening_informations())
authentic_contact_uid = "2dc381f0fb584381b8e4a19c84f53b35"
contact.related_contact = authentic_contact_uid
contact.related_contacts = [authentic_contact_uid]
contact_search_url = (
"http://localhost:8080/Plone/@search?UID={}&fullobjects=1".format(
authentic_contact_uid
)
)
m.get(contact_search_url, text=json.dumps(json_contact_empty_schedule))
clear_cache(self.request)
contact_view.set_current_contact(None)
view = queryMultiAdapter((self.page, self.request), name="full_view")
is_empty = contact_view.is_empty_schedule()
self.assertEqual(is_empty, True)
Expand All @@ -475,7 +474,7 @@ def test_empty_schedule(self, m):
"comments": "",
}
m.get(contact_search_url, text=json.dumps(json_contact_empty_schedule))
clear_cache(self.request)
contact_view.set_current_contact(None)
is_empty = contact_view.is_empty_schedule()
self.assertEqual(is_empty, False)
self.assertIn('class="schedule"', view())
Expand All @@ -489,7 +488,7 @@ def test_leadimage_is_in_portrait_mode(self, m):
)
view = queryMultiAdapter((self.page, self.request), name="full_view")
authentic_contact_uid = "2dc381f0fb584381b8e4a19c84f53b35"
contact.related_contact = authentic_contact_uid
contact.related_contacts = [authentic_contact_uid]
contact_search_url = (
"http://localhost:8080/Plone/@search?UID={}&fullobjects=1".format(
authentic_contact_uid
Expand All @@ -509,7 +508,7 @@ def test_contact_modified(self, m):
title="My contact",
)
authentic_contact_uid = "2dc381f0fb584381b8e4a19c84f53b35"
contact.related_contact = authentic_contact_uid
contact.related_contacts = [authentic_contact_uid]
contact_search_url = (
"http://localhost:8080/Plone/@search?UID={}&fullobjects=1".format(
authentic_contact_uid
Expand All @@ -521,23 +520,23 @@ def test_contact_modified(self, m):
annotations = IAnnotations(contact)
self.assertIsNone(annotations.get(SECTION_ITEMS_HASH_KEY))

self.assertIsNotNone(contact_view.contact)
self.assertIsNotNone(contact_view.contacts)
hash_1 = annotations.get(SECTION_ITEMS_HASH_KEY)
self.assertIsNotNone(hash_1)
first_modification = self.page.ModificationDate()

sleep(1)
clear_cache(self.request)
contact_view.set_current_contact(None)
m.get(contact_search_url, text=json.dumps(self.json_no_contact))
self.assertIsNone(contact_view.contact)
self.assertIsNone(contact_view.contacts)
next_modification = self.page.ModificationDate()
hash_2 = annotations.get(SECTION_ITEMS_HASH_KEY)
self.assertNotEqual(hash_1, hash_2)
self.assertNotEqual(first_modification, next_modification)

sleep(1)
clear_cache(self.request)
self.assertIsNone(contact_view.contact)
contact_view.set_current_contact(None)
self.assertIsNone(contact_view.contacts)
last_modification = self.page.ModificationDate()
hash_3 = annotations.get(SECTION_ITEMS_HASH_KEY)
self.assertEqual(hash_2, hash_3)
Expand Down

0 comments on commit d9a7dd1

Please sign in to comment.