Skip to content

Commit

Permalink
Revert "Fixes #111 for master and make it Plone 6 only"
Browse files Browse the repository at this point in the history
  • Loading branch information
petschki authored Jul 11, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 15ec49d commit c8bc778
Showing 4 changed files with 229 additions and 297 deletions.
2 changes: 0 additions & 2 deletions news/111.bugfix

This file was deleted.

128 changes: 58 additions & 70 deletions plone/app/querystring/querybuilder.py
Original file line number Diff line number Diff line change
@@ -12,15 +12,24 @@
from zope.i18n import translate
from zope.i18nmessageid import MessageFactory
from zope.publisher.browser import BrowserView
from Products.CMFPlone.browser.search import munge_search_term

import json
import logging


logger = logging.getLogger("plone.app.querystring")
_ = MessageFactory("plone")
logger = logging.getLogger('plone.app.querystring')
_ = MessageFactory('plone')

BAD_CHARS = ('?', '-', '+', '*')


def quote_chars(s):
# We need to quote parentheses when searching text indices
if '(' in s:
s = s.replace('(', '"("')
if ')' in s:
s = s.replace(')', '")"')
return s

class ContentListingView(BrowserView):
"""BrowserView for displaying query results"""
@@ -30,25 +39,16 @@ def __call__(self, **kw):


class QueryBuilder(BrowserView):
"""This view is used by the javascripts,
fetching configuration or results"""
""" This view is used by the javascripts,
fetching configuration or results"""

def __init__(self, context, request):
super(QueryBuilder, self).__init__(context, request)
self._results = None

def __call__(
self,
query,
batch=False,
b_start=0,
b_size=30,
sort_on=None,
sort_order=None,
limit=0,
brains=False,
custom_query=None,
):
def __call__(self, query, batch=False, b_start=0, b_size=30,
sort_on=None, sort_order=None, limit=0, brains=False,
custom_query=None):
"""Create a zope catalog query and return results.
:param query: The querystring to be parsed into a zope catalog query.
@@ -95,45 +95,32 @@ def __call__(
sort_order=sort_order,
limit=limit,
brains=brains,
custom_query=custom_query,
)
custom_query=custom_query)
return self._results

def html_results(self, query):
"""html results, used for in the edit screen of a collection,
used in the live update results"""
used in the live update results"""
options = dict(original_context=self.context)
results = self(
query,
sort_on=self.request.get("sort_on", None),
sort_order=self.request.get("sort_order", None),
limit=10,
)
results = self(query, sort_on=self.request.get('sort_on', None),
sort_order=self.request.get('sort_order', None),
limit=10)

return getMultiAdapter(
(results, self.request), name="display_query_results"
(results, self.request),
name='display_query_results'
)(**options)

def _makequery(
self,
query=None,
batch=False,
b_start=0,
b_size=30,
sort_on=None,
sort_order=None,
limit=0,
brains=False,
custom_query=None,
):
def _makequery(self, query=None, batch=False, b_start=0, b_size=30,
sort_on=None, sort_order=None, limit=0, brains=False,
custom_query=None):
"""Parse the (form)query and return using multi-adapter"""
query_modifiers = getUtilitiesFor(IQueryModifier)
for name, modifier in sorted(query_modifiers, key=itemgetter(0)):
query = modifier(query)

parsedquery = queryparser.parseFormquery(
self.context, query, sort_on, sort_order
)
self.context, query, sort_on, sort_order)

index_modifiers = getUtilitiesFor(IParsedQueryIndexModifier)
for name, modifier in index_modifiers:
@@ -147,37 +134,36 @@ def _makequery(
parsedquery[new_name] = query

# Check for valid indexes
catalog = getToolByName(self.context, "portal_catalog")
valid_indexes = [
index for index in parsedquery if index in catalog.indexes()
]
catalog = getToolByName(self.context, 'portal_catalog')
valid_indexes = [index for index in parsedquery
if index in catalog.indexes()]

# We'll ignore any invalid index, but will return an empty set if none
# of the indexes are valid.
if not valid_indexes:
logger.warning(
"Using empty query because there are no valid indexes used."
)
"Using empty query because there are no valid indexes used.")
parsedquery = {}

empty_query = not parsedquery # store emptiness

if batch:
parsedquery["b_start"] = b_start
parsedquery["b_size"] = b_size
parsedquery['b_start'] = b_start
parsedquery['b_size'] = b_size
elif limit:
parsedquery["sort_limit"] = limit
parsedquery['sort_limit'] = limit

if "path" not in parsedquery:
parsedquery["path"] = {"query": ""}
if 'path' not in parsedquery:
parsedquery['path'] = {'query': ''}

if isinstance(custom_query, dict) and custom_query:
# Update the parsed query with an extra query dictionary. This may
# override the parsed query. The custom_query is a dictonary of
# index names and their associated query values.
for key in custom_query:
if isinstance(parsedquery.get(key), dict) and isinstance(
custom_query.get(key), dict
if (
isinstance(parsedquery.get(key), dict)
and isinstance(custom_query.get(key), dict)
):
parsedquery[key].update(custom_query[key])
continue
@@ -189,11 +175,8 @@ def _makequery(
results = []
if not empty_query:
results = catalog(**parsedquery)
if (
getattr(results, "actual_result_count", False)
and limit
and results.actual_result_count > limit
):
if getattr(results, 'actual_result_count', False) and limit\
and results.actual_result_count > limit:
results.actual_result_count = limit

if not brains:
@@ -206,28 +189,33 @@ def number_of_results(self, query):
"""Get the number of results"""
results = self(query, sort_on=None, sort_order=None, limit=1)
return translate(
_(
u"batch_x_items_matching_your_criteria",
default=u"${number} items matching your search terms.",
mapping={"number": results.actual_result_count},
),
context=self.request,
_(u"batch_x_items_matching_your_criteria",
default=u"${number} items matching your search terms.",
mapping={'number': results.actual_result_count}),
context=self.request
)

def filter_query(self, query):
text = query.get("SearchableText", None)
text = query.get('SearchableText', None)
if isinstance(text, dict):
text = text.get("query", "")
text = text.get('query', '')
if text:
query["SearchableText"] = munge_search_term(text)
query['SearchableText'] = self.munge_search_term(text)
return query

def munge_search_term(self, q):
for char in BAD_CHARS:
q = q.replace(char, ' ')
r = q.split()
r = " AND ".join(r)
r = quote_chars(r) + '*'
return r


class RegistryConfiguration(BrowserView):
def __call__(self):
registry = getUtility(IRegistry)
reader = getMultiAdapter(
(registry, self.request), IQuerystringRegistryReader
)
(registry, self.request), IQuerystringRegistryReader)
data = reader()
return json.dumps(data)
Loading

0 comments on commit c8bc778

Please sign in to comment.