From 94f357939957dc8a5de453224913dbecdc4dc9db Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 28 Oct 2024 11:43:19 +0100 Subject: ✨ search: add "id:1234" syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/views_item.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'ishtar_common/views_item.py') diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 64dee8872..23752c8a9 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -1571,6 +1571,26 @@ def _construct_query(relation_types, dct, or_reqs, and_reqs, excluded_relation=F return query +RE_ID = re.compile(r"id\:(\d+)") +ID_REPLACE_KEYS = ["__cached_label", "__label", "__iexact", "__icontains"] + + +def _manage_direct_id(dct): + """ + Manage "id:1234" syntax + """ + for k, v in list(dct.items()): + m = RE_ID.match(v) + if not m: + continue + dct.pop(k) + pk = int(m.groups()[0]) + new_k = k[:] + for rep_key in ID_REPLACE_KEYS: + new_k = new_k.replace(rep_key, "") + dct[new_k] = pk + + def _manage_default_search( dct, request, model, default_name, my_base_request, my_relative_session_names ): @@ -2414,6 +2434,10 @@ def get_item( if updated_excluded: excluded_dct.update(updated_excluded) + # manage direct ID 'id:1234' syntax - used by "{USER}" search + _manage_direct_id(dct) + _manage_direct_id(excluded_dct) + query = _construct_query(relation_types, dct, or_reqs, and_reqs) exc_query = None if excluded_dct or exc_and_reqs or exc_or_reqs or exc_relation_types: -- cgit v1.2.3