From b1fcfc306a2e8a2bbca2cde6ba045413762ad798 Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 28 Dec 2020 18:26:39 +0100 Subject: [PATCH] Add autocomplete on switchs/ap edit/creation forms --- topologie/forms.py | 12 +++++++++-- topologie/templates/topologie/topo_more.html | 2 ++ topologie/urls.py | 1 + topologie/views_autocomplete.py | 21 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/topologie/forms.py b/topologie/forms.py index 2687d65a..fc7a4106 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -171,7 +171,7 @@ class AddAccessPointForm(NewMachineForm): class EditAccessPointForm(EditMachineForm): """Form used to edit access points.""" - class Meta: + class Meta(EditMachineForm.Meta): model = AccessPoint fields = "__all__" @@ -179,9 +179,17 @@ class EditAccessPointForm(EditMachineForm): class EditSwitchForm(EditMachineForm): """Form used to edit switches.""" - class Meta: + class Meta(EditMachineForm.Meta): model = Switch fields = "__all__" + widgets = { + "switchbay": AutocompleteModelMixin( + url="/topologie/switchbay-autocomplete", + ), + "user": AutocompleteModelMixin( + url="/users/user-autocomplete", + ), + } class NewSwitchForm(NewMachineForm): diff --git a/topologie/templates/topologie/topo_more.html b/topologie/templates/topologie/topo_more.html index b0208568..74a5265a 100644 --- a/topologie/templates/topologie/topo_more.html +++ b/topologie/templates/topologie/topo_more.html @@ -31,9 +31,11 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block content %} {% if topoform %} {% bootstrap_form_errors topoform %} + {{ topoform.media }} {% endif %} {% if machineform %} {% bootstrap_form_errors machineform %} +{{ machineform.media }} {% endif %} {% if domainform %} {% bootstrap_form_errors domainform %} diff --git a/topologie/urls.py b/topologie/urls.py index c5ac95eb..05688fd3 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -177,4 +177,5 @@ urlpatterns = [ url(r'^switch-autocomplete/$', views_autocomplete.SwitchAutocomplete.as_view(), name='switch-autocomplete',), url(r'^port-autocomplete/$', views_autocomplete.PortAutocomplete.as_view(), name='profile-autocomplete',), url(r'^portprofile-autocomplete/$', views_autocomplete.PortProfileAutocomplete.as_view(), name='portprofile-autocomplete',), + url(r'^switchbay-autocomplete/$', views_autocomplete.SwitchBayAutocomplete.as_view(), name='switchbay-autocomplete',), ] diff --git a/topologie/views_autocomplete.py b/topologie/views_autocomplete.py index 36d03c3e..bef31fdb 100644 --- a/topologie/views_autocomplete.py +++ b/topologie/views_autocomplete.py @@ -41,6 +41,7 @@ from .models import ( Switch, PortProfile, Port, + SwitchBay, ) from re2o.mixins import AutocompleteViewMixin @@ -128,6 +129,26 @@ class PortAutocomplete(AutocompleteViewMixin): return qs +class SwitchBayAutocomplete(AutocompleteViewMixin): + obj_type = SwitchBay + + def get_queryset(self): + # Comments explain what we try to match + qs = self.obj_type.objects.annotate( + full_name=Concat("building__name", Value(" "), "name"), # Match when the user searches "" + dorm_name=Concat("building__dormitory__name", Value(" "), "name"), # Match "Dorm Local Sud" + dorm_full_name=Concat("building__dormitory__name", Value(" "), "building__name", Value(" "), "name"), # Match "Dorm J Local Sud" + ).all() + + if self.q: + qs = qs.filter( + Q(full_name__icontains=self.q) + | Q(dorm_name__icontains=self.q) + | Q(dorm_full_name__icontains=self.q) + ) + + return qs + class PortProfileAutocomplete(AutocompleteViewMixin): obj_type = PortProfile