Skip to content

Commit

Permalink
enable sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Jul 19, 2024
1 parent 8a6dfee commit 3bddff2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Parametri:
- **type** (obbligatorio): il nome dell'indice in catalogo della tassonomia
- **value**: un eventuale valore per filtrare l'indice
- **portal_type**: un filtro su uno specifico portal_type
- **sort_on**: permette di ordinare i risultati in base ad un determinato indice
- **sort_order**: permette di scegliere l'ordinamento da usare

Le tassonomie (*type*) utilizzabili sono limitate:

Expand Down
21 changes: 19 additions & 2 deletions src/iosanita/policy/restapi/services/search_tassonomie/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

ALLOWED_TAXONOMIES = ["parliamo_di", "a_chi_si_rivolge_tassonomia"]

BASE_FILTERS = [
"portal_type",
"sort_on",
"sort_order",
"fullobjects",
"b_start",
"b_size",
]


class SearchTassonomieGet(Service):
def reply(self):
Expand All @@ -37,15 +46,23 @@ def reply(self):
)
pc = api.portal.get_tool(name="portal_catalog")
query = {}

# add standard query filters
for query_index in BASE_FILTERS:
value = self.request.form.get(query_index, "")
if value:
query[query_index] = value

# then filter by taxonomy
all_values = pc.uniqueValuesFor(index)

if value:
query[index] = value
else:
# return all
query[index] = all_values
if portal_type:
query["portal_type"] = portal_type

# and do search
brains = api.content.find(**query)
batch = HypermediaBatch(self.request, brains)
results = {}
Expand Down
17 changes: 17 additions & 0 deletions src/iosanita/policy/tests/test_search_tassonomie.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ def test_passing_type_show_all_results(self):
self.assertEqual(res["items"][0]["title"], self.news.title)
self.assertEqual(res["items"][1]["title"], self.event.title)

def test_passing_sort_order_return_sorted_results(self):
res = self.api_session.get(
"@search-tassonomie?type=parliamo_di&sort_on=sortable_title"
).json()

self.assertEqual(res["items_total"], 2)
self.assertEqual(res["items"][0]["title"], self.event.title)
self.assertEqual(res["items"][1]["title"], self.news.title)

res = self.api_session.get(
"@search-tassonomie?type=parliamo_di&sort_on=sortable_title&sort_order=descending"
).json()

self.assertEqual(res["items_total"], 2)
self.assertEqual(res["items"][0]["title"], self.news.title)
self.assertEqual(res["items"][1]["title"], self.event.title)

def test_passing_type_and_value_show_filtered_results(self):
res = self.api_session.get(
"@search-tassonomie?type=parliamo_di&value=Alimentazione"
Expand Down

0 comments on commit 3bddff2

Please sign in to comment.