From 3bb2c43914f3c67fd7534842df2273918c8f0198 Mon Sep 17 00:00:00 2001 From: jackahl Date: Wed, 4 May 2022 16:25:05 +0200 Subject: [PATCH 1/5] include token in vocabulary terms --- plone/app/querystring/registryreader.py | 6 +++--- plone/app/querystring/tests/testRegistryReader.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plone/app/querystring/registryreader.py b/plone/app/querystring/registryreader.py index fa5a783..49c93b4 100644 --- a/plone/app/querystring/registryreader.py +++ b/plone/app/querystring/registryreader.py @@ -93,13 +93,13 @@ def getVocabularyValues(self, values): title = translate(item.title, context=self.request) else: title = item.title - translated.append((title, item.value)) + translated.append((title, item.value, item.token)) translated = sorted( translated, key=lambda x: normalizeString(safe_unicode(x[0])) ) - for (title, value) in translated: - field['values'][value] = {'title': title} + for (title, value, token) in translated: + field['values'][value] = {'title': title, 'token': token} return values diff --git a/plone/app/querystring/tests/testRegistryReader.py b/plone/app/querystring/tests/testRegistryReader.py index 4f8ea15..d041da5 100644 --- a/plone/app/querystring/tests/testRegistryReader.py +++ b/plone/app/querystring/tests/testRegistryReader.py @@ -77,7 +77,7 @@ def test_get_vocabularies(self): result = reader.getVocabularyValues(result) vocabulary_result = result.get( 'plone.app.querystring.field.reviewState.values') - self.assertEqual(vocabulary_result, {'foo': {'title': u'bar'}}) + self.assertEqual(vocabulary_result, {'foo': {'title': u'bar', 'token': 'foo'}}) def test_map_operations_clean(self): """tests if mapOperations is getting all operators correctly""" From 9c604ffd154929e539f33a6e62d8b1c8422cb203 Mon Sep 17 00:00:00 2001 From: jackahl Date: Wed, 4 May 2022 16:26:09 +0200 Subject: [PATCH 2/5] update changelog --- news/106.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/106.feature diff --git a/news/106.feature b/news/106.feature new file mode 100644 index 0000000..20de572 --- /dev/null +++ b/news/106.feature @@ -0,0 +1 @@ +- include vocabulary term token in termlist used to configure the querystring search [jackahl] (#106) From 12ad333805081de59b175c89c162fadc66c8e3aa Mon Sep 17 00:00:00 2001 From: jackahl Date: Wed, 4 May 2022 17:00:14 +0200 Subject: [PATCH 3/5] fix changelog entry --- news/106.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/106.feature b/news/106.feature index 20de572..6362e80 100644 --- a/news/106.feature +++ b/news/106.feature @@ -1 +1 @@ -- include vocabulary term token in termlist used to configure the querystring search [jackahl] (#106) +include vocabulary term token in termlist used to configure the querystring search [jackahl] (#106) From 68637cbc2e2d6c6c839875697131fdeba5480e9f Mon Sep 17 00:00:00 2001 From: jackahl Date: Thu, 5 May 2022 18:34:32 +0200 Subject: [PATCH 4/5] enable support to use vocabulary token in querystring-search --- news/108.feature | 1 + plone/app/querystring/configure.zcml | 4 +++ plone/app/querystring/querymodifiers.py | 33 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 news/108.feature diff --git a/news/108.feature b/news/108.feature new file mode 100644 index 0000000..e29a473 --- /dev/null +++ b/news/108.feature @@ -0,0 +1 @@ +add lookup to process vocabulary tokens being used as query parameters. Using values is also still supported. [jackahl] diff --git a/plone/app/querystring/configure.zcml b/plone/app/querystring/configure.zcml index c9b8f79..0abd2fb 100644 --- a/plone/app/querystring/configure.zcml +++ b/plone/app/querystring/configure.zcml @@ -63,4 +63,8 @@ component=".querymodifiers.modify_query_to_enforce_navigation_root" name="1000" /> + diff --git a/plone/app/querystring/querymodifiers.py b/plone/app/querystring/querymodifiers.py index f8adbc4..190361a 100644 --- a/plone/app/querystring/querymodifiers.py +++ b/plone/app/querystring/querymodifiers.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- from plone.app.querystring.interfaces import IQueryModifier from zope.interface import provider +from zope.component import getUtility +from plone.registry.interfaces import IRegistry +from zope.schema.interfaces import IVocabularyFactory @provider(IQueryModifier) @@ -24,4 +27,34 @@ def modify_query_to_enforce_navigation_root(query): 'o': 'plone.app.querystring.operation.string.path', 'v': '/', }) + + return query + +@provider(IQueryModifier) +def vocabulary_value_look_up(query): + registry = getUtility(IRegistry) + + for query_param in query: + query_field_name = query_param.get('i') + query_vocabulary_name = registry.get(f"plone.app.querystring.field.{query_field_name}.vocabulary") + if query_vocabulary_name: + query_terms = query_param.get('v') + factory = getUtility(IVocabularyFactory, query_vocabulary_name) + vocabulary = factory(None) + new_terms = [] + for query_term in query_terms: + try: + vocabulary_term = vocabulary.getTerm(query_term) + except: + for term in vocabulary: + if term.token == query_term: + new_terms.append(term.value) + break + query_param['v']= new_terms + query_vocabulary_name = None + return query + + + + From 9e9c243419f18df9624b6b5c121d7254c3f93981 Mon Sep 17 00:00:00 2001 From: jackahl Date: Fri, 6 May 2022 12:41:25 +0200 Subject: [PATCH 5/5] fix bug in querystring search when only 1 value is supplied --- plone/app/querystring/querymodifiers.py | 27 +++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/plone/app/querystring/querymodifiers.py b/plone/app/querystring/querymodifiers.py index 190361a..b88649b 100644 --- a/plone/app/querystring/querymodifiers.py +++ b/plone/app/querystring/querymodifiers.py @@ -37,20 +37,35 @@ def vocabulary_value_look_up(query): for query_param in query: query_field_name = query_param.get('i') query_vocabulary_name = registry.get(f"plone.app.querystring.field.{query_field_name}.vocabulary") + if query_vocabulary_name: + query_terms = query_param.get('v') factory = getUtility(IVocabularyFactory, query_vocabulary_name) vocabulary = factory(None) - new_terms = [] - for query_term in query_terms: + if type(query_terms) is list: + new_terms = [] + for query_term in query_terms: + try: + vocabulary_term = vocabulary.getTerm(query_term) + new_terms.append(query_term) + + except: + for term in vocabulary: + if term.token == query_term: + new_terms.append(term.value) + break + query_param['v']= new_terms + elif type(query_terms) is str: try: - vocabulary_term = vocabulary.getTerm(query_term) + vocabulary_term = vocabulary.getTerm(query_terms) except: for term in vocabulary: - if term.token == query_term: - new_terms.append(term.value) + if term.token == query_terms: + query_terms=term.value break - query_param['v']= new_terms + query_param['v']= query_terms + query_vocabulary_name = None return query