From 3d4ccf95e91b4a6acbe054d149d34daf43aec9c0 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 5 May 2023 18:38:52 +0200 Subject: Fix precise_town search --- ishtar_common/views_item.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'ishtar_common/views_item.py') diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index b2b924992..31a96f270 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -1066,7 +1066,33 @@ def _manage_hierarchic_fields(model, dct, and_reqs): # TODO: improve query with "IN ()"? continue - if ( + if req.endswith("precise_town_id__cached_label"): + val = _clean_type_val(dct.pop(req)).strip('"') + if val.startswith('"') and val.endswith('"'): + val = val[1:-1] + vals = [v.replace('"', "") for v in val.split(";")] + req = req[:-len("__cached_label")] + town_ids = [] + for val in vals: + q = models.Town.objects.filter(cached_label__iexact=val).values_list( + "id", flat=True) + if not q.count(): + continue + town_id = q.all()[0] + town_ids.append(town_id) + reqs = Q(**{req: val}) + for rel_query in ("parents__", "children__"): + for idx in range(HIERARCHIC_LEVELS): + k = rel_query * (idx + 1) + "pk" + q = models.Town.objects.filter( + **{k: town_id}).values_list("id", flat=True) + if not q.count(): + break + town_ids += list(q.all()) + main_req = Q(**{req + "__in": town_ids}) + and_reqs.append(main_req) + + elif ( req.endswith("town__pk") or req.endswith("towns__pk") or req.endswith("town__cached_label__iexact") -- cgit v1.2.3