From d95cd8c7c7cd220f92f924e8b78f613ab9d1e0a7 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 6 Aug 2020 18:27:57 +0200 Subject: [PATCH] :art: Better autocomplete field --- apps/api/urls.py | 2 +- apps/member/views.py | 4 ++-- static/js/autocomplete_model.js | 6 ++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/api/urls.py b/apps/api/urls.py index 03d6bd68..b74efc88 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -54,7 +54,7 @@ class UserViewSet(ReadProtectedModelViewSet): serializer_class = UserSerializer filter_backends = [DjangoFilterBackend, SearchFilter] filterset_fields = ['id', 'username', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ] - search_fields = ['$username', '$first_name', '$last_name', ] + search_fields = ['$username', '$first_name', '$last_name', '$note__alias__name', '$note__alias__normalized_name', ] # This ViewSet is the only one that is accessible from all authenticated users! diff --git a/apps/member/views.py b/apps/member/views.py index 83304b06..ebcd9d7b 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -326,8 +326,8 @@ class ClubListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): qs = qs.filter( Q(name__iregex=pattern) - | Q(note__alias__name__iregex="^" + pattern) - | Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern)) + | Q(note__alias__name__iregex=pattern) + | Q(note__alias__normalized_name__iregex=Alias.normalize(pattern)) ) return qs diff --git a/static/js/autocomplete_model.js b/static/js/autocomplete_model.js index 9d9ba2d3..c4f9405b 100644 --- a/static/js/autocomplete_model.js +++ b/static/js/autocomplete_model.js @@ -10,6 +10,8 @@ $(document).ready(function () { if (!name_field) name_field = "name"; let input = target.val(); + target.addClass("is-invalid"); + target.removeClass("is-valid"); $("#" + prefix + "_reset").removeClass("d-none"); $.getJSON(api_url + (api_url.includes("?") ? "&" : "?") + "format=json&search=^" + input + api_url_suffix, function(objects) { @@ -27,6 +29,10 @@ $(document).ready(function () { target.val(obj[name_field]); $("#" + prefix + "_pk").val(obj.id); + results_list.html(""); + target.removeClass("is-invalid"); + target.addClass("is-valid"); + if (typeof autocompleted != 'undefined') autocompleted(obj, prefix) });