Skip to content

Commit

Permalink
Update bandi search filters (#64)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
daniele-andreotti authored Apr 11, 2024
1 parent 1ad9e2b commit b9dba3c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/design/plone/policy/restapi/bandi_search_filters/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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)
Expand All @@ -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"])
Expand Down
29 changes: 29 additions & 0 deletions src/design/plone/policy/tests/test_bandi_search_filters_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)

0 comments on commit b9dba3c

Please sign in to comment.