From 89ca6d231fccf9e23f7b7b0cb3f58bf329e3613a Mon Sep 17 00:00:00 2001 From: chirac Date: Wed, 30 Dec 2020 19:22:43 +0100 Subject: [PATCH] Replace get_queryset with filter_results --- machines/views_autocomplete.py | 22 +++++++++------------ topologie/views_autocomplete.py | 34 ++++++++++++++++----------------- users/views_autocomplete.py | 13 ++++++------- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/machines/views_autocomplete.py b/machines/views_autocomplete.py index e68a11c9..ef259da3 100644 --- a/machines/views_autocomplete.py +++ b/machines/views_autocomplete.py @@ -46,7 +46,7 @@ from .models import ( IpList ) -from re2o.mixins import AutocompleteViewMixin +from re2o.views import AutocompleteViewMixin from re2o.acl import ( can_view_all, @@ -84,29 +84,25 @@ class OuverturePortListAutocomplete(AutocompleteViewMixin): class InterfaceAutocomplete(AutocompleteViewMixin): obj_type = Interface - def get_queryset(self): - qs = self.obj_type.objects.all() - + # Precision on search to add annotations so search behaves more like users expect it to + def filter_results(self): if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(domain__name__icontains=self.q) | Q(machine__name__icontains=self.q) ) - return qs - class IpListAutocomplete(AutocompleteViewMixin): obj_type = IpList - def get_queryset(self): + # Precision on search to add annotations so search behaves more like users expect it to + def filter_results(self): machine_type = self.forwarded.get('machine_type', None) - qs = self.obj_type.objects.filter(interface__isnull=True) + self.query_set = self.query_set.filter(interface__isnull=True) if machine_type: - qs = qs.filter(ip_type__machinetype__id=machine_type) + self.query_set = self.query_set.filter(ip_type__machinetype__id=machine_type) if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(ipv4__startswith=self.q) ) - - return qs diff --git a/topologie/views_autocomplete.py b/topologie/views_autocomplete.py index 801b1572..7f8553c1 100644 --- a/topologie/views_autocomplete.py +++ b/topologie/views_autocomplete.py @@ -55,11 +55,11 @@ from re2o.acl import ( class RoomAutocomplete(AutocompleteViewMixin): obj_type = Room - # Override get_queryset to add annotations so search behaves more like users expect it to - def get_queryset(self): + # 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 # Comments explain what we try to match - qs = self.obj_type.objects.annotate( + self.query_set = self.query_set.annotate( full_name=Concat("building__name", Value(" "), "name"), # Match when the user searches "B 001" full_name_stuck=Concat("building__name", "name"), # Match "B001" dorm_name=Concat("building__dormitory__name", Value(" "), "name"), # Match "Dorm 001" @@ -68,7 +68,7 @@ class RoomAutocomplete(AutocompleteViewMixin): ).all() if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(full_name__icontains=self.q) | Q(full_name_stuck__icontains=self.q) | Q(dorm_name__icontains=self.q) @@ -76,8 +76,6 @@ class RoomAutocomplete(AutocompleteViewMixin): | Q(dorm_full_colon_name__icontains=self.q) ) - return qs - #@can_view_all(Dormitory) class DormitoryAutocomplete(AutocompleteViewMixin): @@ -88,20 +86,20 @@ class DormitoryAutocomplete(AutocompleteViewMixin): class BuildingAutocomplete(AutocompleteViewMixin): obj_type = Building - def get_queryset(self): + # 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 - qs = self.obj_type.objects.annotate( + self.query_set = self.query_set.annotate( full_name=Concat("dormitory__name", Value(" "), "name"), full_name_colon=Concat("dormitory__name", Value(" : "), "name"), ).all() if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(full_name__icontains=self.q) | Q(full_name_colon__icontains=self.q) ) - return qs class SwitchAutocomplete(AutocompleteViewMixin): obj_type = Switch @@ -110,38 +108,38 @@ class SwitchAutocomplete(AutocompleteViewMixin): class PortAutocomplete(AutocompleteViewMixin): obj_type = Port - def get_queryset(self): + # 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 - qs = self.obj_type.objects.annotate( + self.query_set = self.query_set.annotate( full_name=Concat("switch__name", Value(" "), "port", output_field=CharField()), full_name_stuck=Concat("switch__name", "port", output_field=CharField()), full_name_dash=Concat("switch__name", Value(" - "), "port", output_field=CharField()), ).all() if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(full_name__icontains=self.q) | Q(full_name_stuck__icontains=self.q) | Q(full_name_dash__icontains=self.q) ) - return qs - class SwitchBayAutocomplete(AutocompleteViewMixin): obj_type = SwitchBay - def get_queryset(self): + # 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 - qs = self.obj_type.objects.annotate( + 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" ).all() if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(full_name__icontains=self.q) | Q(dorm_name__icontains=self.q) | Q(dorm_full_name__icontains=self.q) diff --git a/users/views_autocomplete.py b/users/views_autocomplete.py index b565a4ce..f0cfc3ae 100644 --- a/users/views_autocomplete.py +++ b/users/views_autocomplete.py @@ -61,24 +61,23 @@ class SchoolAutocomplete(AutocompleteViewMixin): #@can_view_all(User) class UserAutocomplete(AutocompleteViewMixin): obj_type = User - # Override get_queryset to add annotations so search behaves more like users expect it to - def get_queryset(self): + + # 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 - qs = self.obj_type.objects.annotate( + self.query_set = self.query_set.annotate( full_name=Concat("adherent__name", Value(" "), "surname"), # Match when the user searches "Toto Passoir" full_name_reverse=Concat("surname", Value(" "), "adherent__name"), # Match when the user searches "Passoir Toto" ).all() if self.q: - qs = qs.filter( + self.query_set = self.query_set.filter( Q(pseudo__icontains=self.q) | Q(full_name__icontains=self.q) | Q(full_name_reverse__icontains=self.q) ) - return qs - -#@can_view_all(Adherent) +#can_view_all(Adherent) class AdherentAutocomplete(AutocompleteViewMixin): obj_type = Adherent