diff options
Diffstat (limited to 'archaeological_warehouse/views.py')
| -rw-r--r-- | archaeological_warehouse/views.py | 31 | 
1 files changed, 24 insertions, 7 deletions
diff --git a/archaeological_warehouse/views.py b/archaeological_warehouse/views.py index e79e291ec..95fd4a159 100644 --- a/archaeological_warehouse/views.py +++ b/archaeological_warehouse/views.py @@ -129,6 +129,16 @@ def autocomplete_container(request, warehouse_id=None):      base_query = Q()      if warehouse_id:          base_query = Q(location_id=warehouse_id) +        try: +            w = models.Warehouse.objects.get(id=warehouse_id) +        except models.Warehouse.DoesNotExist: +            return HttpResponse(content_type="text/plain") +        # remove warehouse name if warehouse already set +        lower_term = unidecode(term).lower() +        warehouse_name = unidecode(w.name).lower() +        if warehouse_name in lower_term: +            idx = lower_term.index(warehouse_name) +            term = (term[:idx] + term[idx + len(warehouse_name) :]).strip()      query = base_query      # exact index      try: @@ -145,8 +155,9 @@ def autocomplete_container(request, warehouse_id=None):      )      limit = 15 - len(containers)      splitted = [s.lower() for s in term.split(" ") if s and s != "|"] -    unaccent_splitted = [unidecode(s).lower() for s in term.split(" ") -                         if s and s != "|"] +    unaccent_splitted = [ +        unidecode(s).lower() for s in term.split(" ") if s and s != "|" +    ]      if limit > 0 and len(splitted) > 1:          type_positions = []  # container_type ID, pos inf, pos sup @@ -155,6 +166,7 @@ def autocomplete_container(request, warehouse_id=None):              for c in models.ContainerType.objects.values_list("id", "label")          ]          for container_type_id, value in container_types: +            value = unidecode(value).lower()              if value in unidecode(term).lower():  # container_type is in search q                  values = [unidecode(v.lower()) for v in value.split(" ") if v]                  # verify that all term match in splitted @@ -190,12 +202,15 @@ def autocomplete_container(request, warehouse_id=None):              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] +                    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)]) +                        value = " ".join(splitted[pos_sup : min(next_positions)])                      if value:                          groups.append((container_type_id, value))                  query = base_query @@ -260,8 +275,10 @@ def autocomplete_container(request, warehouse_id=None):              containers += list(                  models.Container.objects.filter(query)                  .exclude(pk__in=ids) -                .values("id", "cached_label")[:limit] -            ) +                .values("id", "cached_label") +                .order_by("-index", "id")[:limit] +            )  # order by set to display static container first (no index) and +            # first created in db      data = json.dumps(          [              {"id": container["id"], "value": container["cached_label"]}  | 
