From 05a0d59215feef29a8178ff4f5d69e4dc818b42a Mon Sep 17 00:00:00 2001 From: Mauro Amico Date: Mon, 25 Sep 2023 09:55:37 +0200 Subject: [PATCH] fix: case insensitive fiscalcode (#94) * fix: case insensitive fiscalcode * black * zprettu * changelog * fix test --- CHANGES.rst | 7 +++++ src/redturtle/prenotazioni/configure.zcml | 15 ---------- .../prenotazioni/indexers/configure.zcml | 17 ++++++++++- .../{indexes.py => indexers/prenotazione.py} | 7 +++++ .../prenotazioni/indexers/searchable_text.py | 28 ------------------- .../tests/test_prenotazioni_search.py | 18 ++++++++++++ 6 files changed, 48 insertions(+), 44 deletions(-) rename src/redturtle/prenotazioni/{indexes.py => indexers/prenotazione.py} (88%) delete mode 100644 src/redturtle/prenotazioni/indexers/searchable_text.py diff --git a/CHANGES.rst b/CHANGES.rst index 4ed7739b..c719202e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,13 @@ Changelog 2.0.1 (unreleased) ------------------ +- Rimosso searchabletext di prenotazioni doppio + [mamico] + +- Aggiunto indexer per fiscalcode uppercase per + fare ricerche case insensitive + [mamico] + - Remove Contributor from the package permissions map [folix-01] diff --git a/src/redturtle/prenotazioni/configure.zcml b/src/redturtle/prenotazioni/configure.zcml index 20abbfff..02c24264 100644 --- a/src/redturtle/prenotazioni/configure.zcml +++ b/src/redturtle/prenotazioni/configure.zcml @@ -61,19 +61,4 @@ name="redturtle.prenotazioni-hiddenprofiles" /> - - - - - - diff --git a/src/redturtle/prenotazioni/indexers/configure.zcml b/src/redturtle/prenotazioni/indexers/configure.zcml index bf2d9284..1a108911 100644 --- a/src/redturtle/prenotazioni/indexers/configure.zcml +++ b/src/redturtle/prenotazioni/indexers/configure.zcml @@ -4,8 +4,23 @@ > + + + + + + diff --git a/src/redturtle/prenotazioni/indexes.py b/src/redturtle/prenotazioni/indexers/prenotazione.py similarity index 88% rename from src/redturtle/prenotazioni/indexes.py rename to src/redturtle/prenotazioni/indexers/prenotazione.py index 11f50495..e639083e 100644 --- a/src/redturtle/prenotazioni/indexes.py +++ b/src/redturtle/prenotazioni/indexers/prenotazione.py @@ -47,3 +47,10 @@ def Date(obj): Set as booking_date """ return obj.Date() + + +@indexer(IPrenotazione) +def fiscalcode(obj): + """upper-ize fiscalcode for case insensitive search""" + if obj.fiscalcode: + return obj.fiscalcode.upper() diff --git a/src/redturtle/prenotazioni/indexers/searchable_text.py b/src/redturtle/prenotazioni/indexers/searchable_text.py deleted file mode 100644 index efa1fb3f..00000000 --- a/src/redturtle/prenotazioni/indexers/searchable_text.py +++ /dev/null @@ -1,28 +0,0 @@ -# -*- coding: utf-8 -*- -from plone import api -from plone.indexer import indexer -from redturtle.prenotazioni.content.prenotazione import IPrenotazione - -import six - - -@indexer(IPrenotazione) -def prenotazione(context, **kw): - """Reindex prenotazione""" - parts = set( - ( - context.SearchableText(), - context.REQUEST.form.get("cmfeditions_version_comment", ""), - ) - ) - view = api.content.get_view("contenthistory", context, context.REQUEST) - rh = view.revisionHistory() - for item in rh[:-1]: - comments = item.get("comments", "") - if comments: - if isinstance(comments, six.text_type): - comments = comments.encode("utf8") - parts.add(comments) - - searchable_text = " ".join(sorted(set(" ".join(parts).split()))) - return searchable_text diff --git a/src/redturtle/prenotazioni/tests/test_prenotazioni_search.py b/src/redturtle/prenotazioni/tests/test_prenotazioni_search.py index 3f2955e8..b8f73638 100644 --- a/src/redturtle/prenotazioni/tests/test_prenotazioni_search.py +++ b/src/redturtle/prenotazioni/tests/test_prenotazioni_search.py @@ -198,6 +198,24 @@ def test_search_by_fiscalcode(self): self.assertIn(self.prenotazione_fscode.UID(), result_uids) self.assertNotIn(self.prenotazione_no_fscode.UID(), result_uids) + def test_search_by_fiscalcode_case_insensitive(self): + # ABCDEF12G34H567I -> AbCdEf12G34H567i + camelcase_fiscalcode = "".join( + [ + c.upper() if i % 2 == 0 else c.lower() + for i, c in enumerate(self.testing_fiscal_code) + ] + ) + result_uids = [ + i["booking_id"] + for i in self.api_session.get( + f"{self.portal.absolute_url()}/@bookings/{camelcase_fiscalcode}" # noqa: E501 + ).json()["items"] + ] + + self.assertIn(self.prenotazione_fscode.UID(), result_uids) + self.assertNotIn(self.prenotazione_no_fscode.UID(), result_uids) + def test_search_by_fiscalcode_traverse(self): res = self.api_session.get( f"{self.portal.absolute_url()}/@bookings/{self.testing_fiscal_code}"