From a9426e4ede27ff683295fa40930c578274e445fd Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 28 Apr 2021 12:10:32 +0200 Subject: Container autocomplete: allow any order in container types --- archaeological_warehouse/views.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'archaeological_warehouse') diff --git a/archaeological_warehouse/views.py b/archaeological_warehouse/views.py index c6072a4df..54390aca9 100644 --- a/archaeological_warehouse/views.py +++ b/archaeological_warehouse/views.py @@ -18,6 +18,7 @@ # See the file COPYING for details. import json +import itertools from unidecode import unidecode from django.core.urlresolvers import reverse @@ -185,29 +186,29 @@ def autocomplete_container(request, warehouse_id=None): query &= Q(location__name__icontains=splitted[idx]) # group by container type, ref tuple # can be in any order - for positions in (type_positions, list(reversed(type_positions))): + for positions in itertools.permutations(type_positions): groups = [] for idx, (container_type_id, pos_inf, pos_sup) in enumerate(positions): - if len(positions) == idx + 1: # last + next_positions = [p[1] for p in positions + if p[0] != container_type_id and p[1] > pos_sup] + if not next_positions: value = " ".join(splitted[pos_sup:]) else: - value = " ".join(splitted[pos_sup:positions[idx + 1][1]]) + value = " ".join(splitted[pos_sup:min(next_positions)]) if value: groups.append((container_type_id, value)) - if groups: - for gp in (groups, reversed(groups)): - query = base_query - for idx, g in enumerate(gp): - base_key = "parent__" * idx - key1 = base_key + "container_type_id" - key2 = base_key + "reference__unaccent__iexact" - query &= Q(**{key1: g[0], key2: g[1]}) - ids = {c["id"] for c in containers} - containers += list( - models.Container.objects.filter(query) - .exclude(pk__in=ids) - .values("id", "cached_label")[:limit] - ) + query = base_query + for idx, g in enumerate(groups): + base_key = "parent__" * idx + key1 = base_key + "container_type_id" + key2 = base_key + "reference__unaccent__iexact" + query &= Q(**{key1: g[0], key2: g[1]}) + ids = {c["id"] for c in containers} + containers += list( + models.Container.objects.filter(query) + .exclude(pk__in=ids) + .values("id", "cached_label")[:limit] + ) if len(splitted) > 1 and len(containers) < 15: # group to do a "type" "reference" search -- cgit v1.2.3