Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query string token search #109

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/106.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include vocabulary term token in termlist used to configure the querystring search [jackahl] (#106)
1 change: 1 addition & 0 deletions news/108.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add lookup to process vocabulary tokens being used as query parameters. Using values is also still supported. [jackahl]
4 changes: 4 additions & 0 deletions plone/app/querystring/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@
component=".querymodifiers.modify_query_to_enforce_navigation_root"
name="1000"
/>
<utility
component=".querymodifiers.vocabulary_value_look_up"
name="1001"
/>
</configure>
48 changes: 48 additions & 0 deletions plone/app/querystring/querymodifiers.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -24,4 +27,49 @@ 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)
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_terms)
except:
for term in vocabulary:
if term.token == query_terms:
query_terms=term.value
break
query_param['v']= query_terms

query_vocabulary_name = None

return query




6 changes: 3 additions & 3 deletions plone/app/querystring/registryreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion plone/app/querystring/tests/testRegistryReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down