diff options
Diffstat (limited to 'archaeological_warehouse/views.py')
| -rw-r--r-- | archaeological_warehouse/views.py | 47 | 
1 files changed, 24 insertions, 23 deletions
| diff --git a/archaeological_warehouse/views.py b/archaeological_warehouse/views.py index 54390aca9..e79e291ec 100644 --- a/archaeological_warehouse/views.py +++ b/archaeological_warehouse/views.py @@ -186,29 +186,30 @@ 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 itertools.permutations(type_positions): -            groups = [] -            for idx, (container_type_id, pos_inf, pos_sup) in enumerate(positions): -                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:min(next_positions)]) -                if value: -                    groups.append((container_type_id, value)) -            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 type_positions: +            for positions in itertools.permutations(type_positions): +                groups = [] +                for idx, (container_type_id, pos_inf, pos_sup) in enumerate(positions): +                    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:min(next_positions)]) +                    if value: +                        groups.append((container_type_id, value)) +                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 | 
