summaryrefslogtreecommitdiff
path: root/ishtar_common/views_item.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2020-12-03 13:51:03 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-28 12:15:22 +0100
commitbff83627dd1555278d00a553f8dd64df81b0478c (patch)
tree7eab66ca6156ce4fb315fb41e6782f6042408d45 /ishtar_common/views_item.py
parent3e80e0a84ede2ea2362d3602c5e7f984e8863fbb (diff)
downloadIshtar-bff83627dd1555278d00a553f8dd64df81b0478c.tar.bz2
Ishtar-bff83627dd1555278d00a553f8dd64df81b0478c.zip
Search: do not include empty result to "*" searches (refs #4930)
Diffstat (limited to 'ishtar_common/views_item.py')
-rw-r--r--ishtar_common/views_item.py93
1 files changed, 45 insertions, 48 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py
index d93db355b..83d326ee9 100644
--- a/ishtar_common/views_item.py
+++ b/ishtar_common/views_item.py
@@ -564,11 +564,9 @@ def _parse_query_string(string, query_parameters, current_dct, exc_dct,
dct[k] = dct[k].replace('"', '')
# distinct query wait for a query
_manage_clean_search_field(dct)
- if excluded:
- extra_distinct_q[-1] = ~Q(**dct)
- else:
- extra_distinct_q[-1] = Q(**dct)
- return u""
+ extra_distinct_q[-1] = \
+ ~Q(**dct) if excluded else Q(**dct)
+ return ""
for reserved_char in FORBIDDEN_CHAR:
string = string.replace(reserved_char, u"")
if len(string) != 1:
@@ -576,9 +574,9 @@ def _parse_query_string(string, query_parameters, current_dct, exc_dct,
string = string.replace(reserved_char, u"")
if not string:
return ""
- if string.endswith(u'*'):
+ if string.endswith('*'):
if len(string.strip()) == 1:
- return u""
+ return ""
string = string[:-1] + ':*'
elif string not in ("&", "|", "!", "-"):
# like search by default
@@ -644,7 +642,7 @@ def _parse_parentheses_groups(groups, query_parameters, current_dct=None,
extra_distinct_q)
if not groups: # empty list
return "", current_dct, exc_dct, extra_distinct_q
- query = u"("
+ query = "("
previous_sep, has_item = None, False
for item in groups:
q, current_dct, exc_dct, extra_distinct_q = _parse_parentheses_groups(
@@ -652,16 +650,13 @@ def _parse_parentheses_groups(groups, query_parameters, current_dct=None,
q = q.strip()
if not q:
continue
- if q in (u"|", u"&"):
+ if q in ("|", "&"):
if previous_sep or not has_item:
continue # multiple sep is not relevant
previous_sep = q
continue
if has_item:
- if previous_sep:
- query += previous_sep
- else:
- query += u" & "
+ query += previous_sep or " & "
query += q
has_item = True
previous_sep = None
@@ -685,19 +680,19 @@ def _search_manage_search_vector(model, dct, exc_dct, distinct_queries,
distinct_queries += extra_distinct_q
exc_dct.update(extra_exc_dct)
+ if not search_query:
+ return dct, exc_dct, distinct_queries
+ # remove inside parenthesis
+ search_query = search_query.replace('(', '').replace(')', '').strip()
if search_query:
- # remove inside parenthesis
- search_query = \
- search_query.replace('(', '').replace(')', '').strip()
- if search_query:
- if 'extras' not in dct:
- dct['extras'] = []
- dct['extras'].append(
- {'where': [model._meta.db_table +
- ".search_vector @@ (to_tsquery(%s, %s)) = true"],
- 'params': [settings.ISHTAR_SEARCH_LANGUAGE,
- search_query]}
- )
+ if 'extras' not in dct:
+ dct['extras'] = []
+ dct['extras'].append(
+ {'where': [model._meta.db_table +
+ ".search_vector @@ (to_tsquery(%s, %s)) = true"],
+ 'params': [settings.ISHTAR_SEARCH_LANGUAGE,
+ search_query]}
+ )
return dct, exc_dct, distinct_queries
@@ -743,14 +738,11 @@ def _manage_many_counted_fields(fields, reversed_fields, dct, excluded_dct):
for k in bool_fields:
if k not in dct:
continue
- elif dct[k] == u"1":
+ elif dct[k] == "1":
dct.pop(k)
continue
- dct[k] = dct[k].replace(u'"', u'')
- if dct[k] in [u"2", u"yes", str(_(u"Yes")).lower()]:
- dct[k] = True
- else:
- dct[k] = None
+ dct[k] = dct[k].replace('"', '')
+ dct[k] = True if dct[k] in ["2", "yes", str(_("Yes")).lower()] else None
if reversed_fields and k in reversed_fields:
dct[k] = True if not dct[k] else None
if dct[k]:
@@ -1008,19 +1000,25 @@ def _manage_hierarchic_fields(model, dct, and_reqs):
break
-def _manage_clean_search_field(dct):
- for k in dct.keys():
+def _manage_clean_search_field(dct, exclude=None):
+ for k in list(dct.keys()):
# clean quoted search field
- if type(dct[k]) == str:
- dct[k] = dct[k].replace(u'"', '')
- dct[k] = _clean_type_val(dct[k])
- if '*' in dct[k] and k.endswith('__iexact'):
- value = dct.pop(k).strip()
- if value.startswith("*"):
- value = value[1:]
- if value.endswith("*"):
- value = value[:-1]
- dct[k[:-len('__iexact')] + '__icontains'] = value
+ if type(dct[k]) != str:
+ continue
+ dct[k] = dct[k].replace('"', '')
+ dct[k] = _clean_type_val(dct[k])
+ if '*' not in dct[k] or not k.endswith('__iexact'):
+ continue
+ value = dct.pop(k).strip()
+ if value.startswith("*"):
+ value = value[1:]
+ if value.endswith("*"):
+ value = value[:-1]
+ base_key = k[:-len('__iexact')]
+ if value:
+ dct[base_key + '__icontains'] = value
+ elif exclude is not None:
+ exclude[base_key + '__exact'] = ""
def _manage_relation_types(relation_types, dct, query, or_reqs):
@@ -1069,7 +1067,7 @@ def _manage_relation_types(relation_types, dct, query, or_reqs):
def _construct_query(relation_types, dct, or_reqs, and_reqs):
# manage multi value not already managed
- for key in dct.keys():
+ for key in list(dct.keys()):
if type(dct[key]) == str and u";" in dct[key]:
values = [v for v in dct[key].split(u';') if v]
if not values:
@@ -1256,8 +1254,7 @@ def _get_data_from_query(items, query_table_cols, extra_request_keys,
if hasattr(items.model, "locked"):
values.append("locked")
values.append("lock_user_id")
- data_list = items.values_list(*values)
- return data_list
+ return items.values_list(*values)
def _get_data_from_query_old(items, query_table_cols, request,
@@ -1712,8 +1709,8 @@ def get_item(model, func_name, default_name, extra_request_keys=None,
if 'exc_and_reqs' in dct:
exc_and_reqs += dct.pop('exc_and_reqs')
- _manage_clean_search_field(dct)
- _manage_clean_search_field(excluded_dct)
+ _manage_clean_search_field(dct, excluded_dct)
+ _manage_clean_search_field(excluded_dct, dct)
query = _construct_query(relation_types, dct, or_reqs, and_reqs)
exc_query = None