From 4d37b4506c31f65f683d1d55b7f6a0e0a1eb2f4b Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Sat, 2 Jan 2021 00:27:31 +0100 Subject: [PATCH] Fix minor issues with DAL --- machines/views_autocomplete.py | 2 ++ re2o/mixins.py | 8 +++---- re2o/views.py | 3 ++- static/css/autocomplete.css | 2 +- topologie/templates/topologie/topo_more.html | 2 +- topologie/views_autocomplete.py | 25 +++++++++++--------- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/machines/views_autocomplete.py b/machines/views_autocomplete.py index 076d13a2..94b55a59 100644 --- a/machines/views_autocomplete.py +++ b/machines/views_autocomplete.py @@ -95,9 +95,11 @@ class IpListAutocomplete(AutocompleteViewMixin): def filter_results(self): machine_type = self.forwarded.get("machine_type", None) self.query_set = self.query_set.filter(interface__isnull=True) + if machine_type: self.query_set = self.query_set.filter( ip_type__machinetype__id=machine_type ) + if self.q: self.query_set = self.query_set.filter(Q(ipv4__startswith=self.q)) diff --git a/re2o/mixins.py b/re2o/mixins.py index 9aa39081..471f7414 100644 --- a/re2o/mixins.py +++ b/re2o/mixins.py @@ -270,8 +270,8 @@ class AutocompleteModelMixin(autocomplete.ModelSelect2): """ See https://select2.org/configuration/options-api """ - # By default, only trigger autocompletion after 3 characters have been typed - # attrs["data-minimum-input-length"] = attrs.get("data-minimum-input-length", 3) + # Display the "x" button to clear the input by default + attrs["data-allow-clear"] = attrs.get("data-allow-clear", "true") # If there are less than 10 results, just show all of them (no need to autocomplete) attrs["data-minimum-results-for-search"] = attrs.get( "data-minimum-results-for-search", 10 @@ -294,8 +294,8 @@ class AutocompleteMultipleModelMixin(autocomplete.ModelSelect2Multiple): """ See https://select2.org/configuration/options-api """ - # By default, only trigger autocompletion after 3 characters have been typed - # attrs["data-minimum-input-length"] = attrs.get("data-minimum-input-length", 3) + # Display the "x" button to clear the input by default + attrs["data-allow-clear"] = attrs.get("data-allow-clear", "true") # If there are less than 10 results, just show all of them (no need to autocomplete) attrs["data-minimum-results-for-search"] = attrs.get( "data-minimum-results-for-search", 10 diff --git a/re2o/views.py b/re2o/views.py index ed7d2cff..aa8ddc4b 100644 --- a/re2o/views.py +++ b/re2o/views.py @@ -182,8 +182,8 @@ class AutocompleteViewMixin(LoginRequiredMixin, autocomplete.Select2QuerySetView query_filter = "name__icontains" # Override this if necessary def get_queryset(self): - can, reason, _permission, query_set = self.obj_type.can_list(self.request.user) + if query_set: self.query_set = query_set else: @@ -194,4 +194,5 @@ class AutocompleteViewMixin(LoginRequiredMixin, autocomplete.Select2QuerySetView else: if self.q: self.query_set = self.query_set.filter(**{self.query_filter: self.q}) + return self.query_set diff --git a/static/css/autocomplete.css b/static/css/autocomplete.css index dea68af3..41b355fa 100644 --- a/static/css/autocomplete.css +++ b/static/css/autocomplete.css @@ -39,7 +39,7 @@ See github.com/yourlabs/django-autocomplete-light/issues/1149 .select2-container--default .select2-selection--multiple .select2-selection__rendered { height: 100% !important; - display: inline !imoortant; + display: inline !important; overflow-x: hidden !important; overflow-y: auto !important; } diff --git a/topologie/templates/topologie/topo_more.html b/topologie/templates/topologie/topo_more.html index 74a5265a..be97aa12 100644 --- a/topologie/templates/topologie/topo_more.html +++ b/topologie/templates/topologie/topo_more.html @@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block content %} {% if topoform %} {% bootstrap_form_errors topoform %} - {{ topoform.media }} +{{ topoform.media }} {% endif %} {% if machineform %} {% bootstrap_form_errors machineform %} diff --git a/topologie/views_autocomplete.py b/topologie/views_autocomplete.py index e5c6ee6d..e1069b18 100644 --- a/topologie/views_autocomplete.py +++ b/topologie/views_autocomplete.py @@ -44,7 +44,7 @@ class RoomAutocomplete(AutocompleteViewMixin): # Precision on search to add annotations so search behaves more like users expect it to def filter_results(self): - # Suppose we have a dorm named Dorm, a building name B, and rooms from 001 - 999 + # Suppose we have a dorm named Dorm, a building named B, and rooms from 001 - 999 # Comments explain what we try to match self.query_set = self.query_set.annotate( full_name=Concat( @@ -87,7 +87,6 @@ class DormitoryAutocomplete(AutocompleteViewMixin): class BuildingAutocomplete(AutocompleteViewMixin): obj_type = Building - # Precision on search to add annotations so search behaves more like users expect it to def filter_results(self): # We want to be able to filter by dorm so it's easier self.query_set = self.query_set.annotate( @@ -108,10 +107,9 @@ class SwitchAutocomplete(AutocompleteViewMixin): class PortAutocomplete(AutocompleteViewMixin): obj_type = Port - # Precision on search to add annotations so search behaves more like users expect it to def filter_results(self): # We want to enter the switch name, not just the port number - # Because we're concatenating a CharField and an Integer, we have to sepcify the output_field + # Because we're concatenating a CharField and an Integer, we have to specify the output_field self.query_set = self.query_set.annotate( full_name=Concat( "switch__name", Value(" "), "port", output_field=CharField() @@ -133,23 +131,29 @@ class PortAutocomplete(AutocompleteViewMixin): class SwitchBayAutocomplete(AutocompleteViewMixin): obj_type = SwitchBay - # Precision on search to add annotations so search behaves more like users expect it to def filter_results(self): - # Comments explain what we try to match + # See RoomAutocomplete.filter_results self.query_set = self.query_set.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" + ), + dorm_full_colon_name=Concat( + "building__dormitory__name", + Value(" : "), + "building__name", + Value(" "), + "name", + ), ).all() if self.q: @@ -157,10 +161,9 @@ class SwitchBayAutocomplete(AutocompleteViewMixin): Q(full_name__icontains=self.q) | Q(dorm_name__icontains=self.q) | Q(dorm_full_name__icontains=self.q) + | Q(dorm_full_colon_name__icontains=self.q) ) - return qs - class PortProfileAutocomplete(AutocompleteViewMixin): obj_type = PortProfile