From b9dba3cd3cce206c68a2841a923caf2011bf9eb1 Mon Sep 17 00:00:00 2001 From: daniele-andreotti Date: Thu, 11 Apr 2024 16:48:21 +0200 Subject: [PATCH] Update bandi search filters (#64) * bandi-search-filters: if called on a specific folder returns only related offices of bandi inside that folder. * blacked * added test * fixed test * fixed tests --- CHANGES.rst | 3 +- .../restapi/bandi_search_filters/get.py | 12 ++++++-- .../tests/test_bandi_search_filters_api.py | 29 +++++++++++++++++++ 3 files changed, 40 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..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( { @@ -46,6 +45,13 @@ def reply(self): 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 +63,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"]) 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..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 @@ -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,7 +34,16 @@ 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" + ) + self.uo_public_1 = api.content.create( container=self.portal, type="UnitaOrganizzativa", title="UO 1" ) @@ -68,10 +78,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", + tipologia_bando=tipologia_bando, + 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 +132,13 @@ 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): + 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.assertIn(self.uo_public_1.UID(), offices)