From 8c534e377ebc3c79e06fa782e8846139043b4d64 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 6 May 2026 17:12:31 +0200 Subject: ✨ find criteria search - warehouse town of find's containers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/views.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'ishtar_common/views.py') diff --git a/ishtar_common/views.py b/ishtar_common/views.py index e219e5633..965c9b0d1 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -1046,21 +1046,30 @@ def autocomplete_department(request): return HttpResponse(data, content_type="text/plain") -def autocomplete_town(request): - if not request.GET.get("term"): - return HttpResponse(content_type="text/plain") - q = request.GET.get("term") - q = unicodedata.normalize("NFKD", q).encode("ascii", "ignore").decode() - query = Q() - for q in q.split(" "): - extra = Q(name__unaccent__icontains=q) - if settings.COUNTRY == "fr": - extra |= Q(numero_insee__istartswith=q) - query &= extra - limit = 20 - towns = models.Town.objects.filter(query).distinct()[:limit] - data = json.dumps([{"id": town.pk, "value": str(town)} for town in towns]) - return HttpResponse(data, content_type="text/plain") +def get_autocomplete_town(cached_label=True): + def func(request): + if not request.GET.get("term"): + return HttpResponse(content_type="text/plain") + q = request.GET.get("term") + q = unicodedata.normalize("NFKD", q).encode("ascii", "ignore").decode() + query = Q() + for q in q.split(" "): + extra = Q(name__unaccent__icontains=q) + if settings.COUNTRY == "fr": + extra |= Q(numero_insee__istartswith=q) + query &= extra + limit = 20 + towns = models.Town.objects.filter(query).distinct()[:limit] + + attr = "cached_label" if cached_label else "name" + data = json.dumps([{"id": town.pk, "value": getattr(town, attr)} + for town in towns]) + return HttpResponse(data, content_type="text/plain") + return func + + +autocomplete_town = get_autocomplete_town() +autocomplete_simple_town = get_autocomplete_town(cached_label=False) def autocomplete_advanced_town(request, department_id=None, state_id=None): -- cgit v1.2.3