diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-04-28 12:10:32 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-04-28 12:18:07 +0200 |
commit | e9587499cdd7190aa4d1c3722353490ea346469a (patch) | |
tree | 2df5ad0561d81e7bceb4090d9d4451e915b7740c /archaeological_warehouse | |
parent | 380d95bb09f99d4da590010436d9d6bdd352f940 (diff) | |
download | Ishtar-e9587499cdd7190aa4d1c3722353490ea346469a.tar.bz2 Ishtar-e9587499cdd7190aa4d1c3722353490ea346469a.zip |
Container autocomplete: allow any order in container types
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r-- | archaeological_warehouse/views.py | 35 |
1 files changed, 18 insertions, 17 deletions
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 |