From 68d918de2ca98ad5738e769f658fd4698ca71d38 Mon Sep 17 00:00:00 2001 From: Daniele Andreotti Date: Wed, 10 Apr 2024 16:47:14 +0200 Subject: [PATCH 1/5] bandi-search-filters: if called on a specific folder returns only related offices of bandi inside that folder. --- CHANGES.rst | 3 ++- .../plone/policy/restapi/bandi_search_filters/get.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 12d3f42..f8e41e6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,8 @@ Changelog 4.0.9 (unreleased) ------------------ -- Nothing changed yet. +- @bandi-search-filters: if called on a specific folder returns only related offices of bandi inside that folder. + [daniele] 4.0.8 (2024-02-21) diff --git a/src/design/plone/policy/restapi/bandi_search_filters/get.py b/src/design/plone/policy/restapi/bandi_search_filters/get.py index 681333a..7893296 100644 --- a/src/design/plone/policy/restapi/bandi_search_filters/get.py +++ b/src/design/plone/policy/restapi/bandi_search_filters/get.py @@ -45,7 +45,9 @@ def reply(self): found = [x for x in subjects if x["UID"] == sub] if not found: subjects.append({"UID": sub, "title": sub}) - + + for office_relation in bando.ufficio_responsabile: + offices.append({"UID": office_relation.to_object.UID(), "title": office_relation.to_object.title}) else: for subject in pc.uniqueValuesFor("Subject_bando"): res = api.content.find(Subject_bando=subject) @@ -57,8 +59,8 @@ def reply(self): {"UID": item, "title": voc_tipologie.getTerm(item).title} ) - office_uids = pc.uniqueValuesFor("ufficio_responsabile_bando") - offices = [{"UID": x.UID, "title": x.Title} for x in pc(UID=office_uids)] + office_uids = pc.uniqueValuesFor("ufficio_responsabile_bando") + offices = [{"UID": x.UID, "title": x.Title} for x in pc(UID=office_uids)] subjects.sort(key=lambda x: x["title"]) offices.sort(key=lambda x: x["title"]) From b8253c59a19c40b44804d9f8d196d7ca1d152ca3 Mon Sep 17 00:00:00 2001 From: Daniele Andreotti Date: Wed, 10 Apr 2024 16:48:05 +0200 Subject: [PATCH 2/5] blacked --- .../plone/policy/restapi/bandi_search_filters/get.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/design/plone/policy/restapi/bandi_search_filters/get.py b/src/design/plone/policy/restapi/bandi_search_filters/get.py index 7893296..dda7976 100644 --- a/src/design/plone/policy/restapi/bandi_search_filters/get.py +++ b/src/design/plone/policy/restapi/bandi_search_filters/get.py @@ -45,9 +45,14 @@ def reply(self): found = [x for x in subjects if x["UID"] == sub] if not found: subjects.append({"UID": sub, "title": sub}) - + for office_relation in bando.ufficio_responsabile: - offices.append({"UID": office_relation.to_object.UID(), "title": office_relation.to_object.title}) + offices.append( + { + "UID": office_relation.to_object.UID(), + "title": office_relation.to_object.title, + } + ) else: for subject in pc.uniqueValuesFor("Subject_bando"): res = api.content.find(Subject_bando=subject) From 55c6813db4444bb4e67baba07886b716760e1f5f Mon Sep 17 00:00:00 2001 From: Daniele Andreotti Date: Wed, 10 Apr 2024 17:39:45 +0200 Subject: [PATCH 3/5] added test --- .../tests/test_bandi_search_filters_api.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/design/plone/policy/tests/test_bandi_search_filters_api.py b/src/design/plone/policy/tests/test_bandi_search_filters_api.py index b09a5f1..7c5a73d 100644 --- a/src/design/plone/policy/tests/test_bandi_search_filters_api.py +++ b/src/design/plone/policy/tests/test_bandi_search_filters_api.py @@ -34,6 +34,11 @@ def setUp(self): setRoles(self.portal, TEST_USER_ID, ["Manager"]) intids = getUtility(IIntIds) + + self.bando_dir = api.content.create( + container=self.portal type="Folder", title="Bandi test folder" + ) + self.uo_public_1 = api.content.create( container=self.portal, type="UnitaOrganizzativa", title="UO 1" ) @@ -68,10 +73,19 @@ def setUp(self): ufficio_responsabile=[RelationValue(intids.getId(self.uo_public_1))], ) + self.bando_in_folder = api.content.create( + container=self.bando_dir, + type="Bando", + title="Bando in folder", + subject=("foo", "baz"), + ufficio_responsabile=[RelationValue(intids.getId(self.uo_public_1))], + ) + api.content.transition(obj=self.uo_public_1, transition="publish") api.content.transition(obj=self.uo_public_2, transition="publish") api.content.transition(obj=self.bando_public_1, transition="publish") api.content.transition(obj=self.bando_public_2, transition="publish") + api.content.transition(obj=self.bando_in_folder, transition="publish") commit() @@ -113,3 +127,12 @@ def test_endpoint_return_list_of_subjects_based_on_permissions(self): subjects = [x["UID"] for x in response["subjects"]] self.assertEqual(len(subjects), 2) self.assertEqual(subjects, ["bar", "foo"]) + + def test_endpoint_return_related_office_for_bando_by_path(self): + response = self.api_session.get("/@bandi-search-filters").json() + + self.assertIn("offices", response) + offices = [x["UID"] for x in response["offices"]] + + self.assertEqual(offices, ["bar", "baz", "foo"]) + From 4cb2124700be015371861a63c41b136b4bbd7b34 Mon Sep 17 00:00:00 2001 From: Daniele Andreotti Date: Thu, 11 Apr 2024 13:00:41 +0200 Subject: [PATCH 4/5] fixed test --- .../policy/restapi/bandi_search_filters/get.py | 1 - .../policy/tests/test_bandi_search_filters_api.py | 15 +++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/design/plone/policy/restapi/bandi_search_filters/get.py b/src/design/plone/policy/restapi/bandi_search_filters/get.py index dda7976..ebe735f 100644 --- a/src/design/plone/policy/restapi/bandi_search_filters/get.py +++ b/src/design/plone/policy/restapi/bandi_search_filters/get.py @@ -33,7 +33,6 @@ def reply(self): for brain in brains: bando = brain.getObject() found = [x for x in tipologie if x["UID"] == bando.tipologia_bando] - if not found: tipologie.append( { diff --git a/src/design/plone/policy/tests/test_bandi_search_filters_api.py b/src/design/plone/policy/tests/test_bandi_search_filters_api.py index 7c5a73d..f2d64ea 100644 --- a/src/design/plone/policy/tests/test_bandi_search_filters_api.py +++ b/src/design/plone/policy/tests/test_bandi_search_filters_api.py @@ -12,6 +12,7 @@ from z3c.relationfield import RelationValue from zope.intid.interfaces import IIntIds from plone import api +from redturtle.bandi.vocabularies import TipologiaBandoVocabularyFactory import unittest @@ -33,10 +34,14 @@ def setUp(self): setRoles(self.portal, TEST_USER_ID, ["Manager"]) + voc_tipologie = TipologiaBandoVocabularyFactory(self.portal) + keys = voc_tipologie.by_value.keys() + tipologia_bando = tuple(keys)[0] + intids = getUtility(IIntIds) self.bando_dir = api.content.create( - container=self.portal type="Folder", title="Bandi test folder" + container=self.portal, type="Folder", title="Bandi test folder" ) self.uo_public_1 = api.content.create( @@ -78,6 +83,7 @@ def setUp(self): type="Bando", title="Bando in folder", subject=("foo", "baz"), + tipologia_bando=tipologia_bando, ufficio_responsabile=[RelationValue(intids.getId(self.uo_public_1))], ) @@ -129,10 +135,11 @@ def test_endpoint_return_list_of_subjects_based_on_permissions(self): self.assertEqual(subjects, ["bar", "foo"]) def test_endpoint_return_related_office_for_bando_by_path(self): - response = self.api_session.get("/@bandi-search-filters").json() + path = "/".join(self.bando_dir.getPhysicalPath()).replace("/plone/", "/") + string_request = path + "/@bandi-search-filters" + response = self.api_session.get(string_request).json() self.assertIn("offices", response) offices = [x["UID"] for x in response["offices"]] - self.assertEqual(offices, ["bar", "baz", "foo"]) - + self.assertIn(self.uo_public_1.UID(), offices) From 37c1fc666c92322341acff47b549f12b4374bdb4 Mon Sep 17 00:00:00 2001 From: Daniele Andreotti Date: Thu, 11 Apr 2024 15:08:01 +0200 Subject: [PATCH 5/5] fixed tests --- src/design/plone/policy/tests/test_bandi_search_filters_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/design/plone/policy/tests/test_bandi_search_filters_api.py b/src/design/plone/policy/tests/test_bandi_search_filters_api.py index f2d64ea..2839f86 100644 --- a/src/design/plone/policy/tests/test_bandi_search_filters_api.py +++ b/src/design/plone/policy/tests/test_bandi_search_filters_api.py @@ -82,7 +82,6 @@ def setUp(self): container=self.bando_dir, type="Bando", title="Bando in folder", - subject=("foo", "baz"), tipologia_bando=tipologia_bando, ufficio_responsabile=[RelationValue(intids.getId(self.uo_public_1))], )