8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 07:02:26 +00:00

Replace get_queryset with filter_results

This commit is contained in:
chirac 2020-12-30 19:22:43 +01:00 committed by Gabriel Detraz
parent 4df53ca902
commit 89ca6d231f
3 changed files with 31 additions and 38 deletions

View file

@ -46,7 +46,7 @@ from .models import (
IpList IpList
) )
from re2o.mixins import AutocompleteViewMixin from re2o.views import AutocompleteViewMixin
from re2o.acl import ( from re2o.acl import (
can_view_all, can_view_all,
@ -84,29 +84,25 @@ class OuverturePortListAutocomplete(AutocompleteViewMixin):
class InterfaceAutocomplete(AutocompleteViewMixin): class InterfaceAutocomplete(AutocompleteViewMixin):
obj_type = Interface obj_type = Interface
def get_queryset(self): # Precision on search to add annotations so search behaves more like users expect it to
qs = self.obj_type.objects.all() def filter_results(self):
if self.q: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(domain__name__icontains=self.q) Q(domain__name__icontains=self.q)
| Q(machine__name__icontains=self.q) | Q(machine__name__icontains=self.q)
) )
return qs
class IpListAutocomplete(AutocompleteViewMixin): class IpListAutocomplete(AutocompleteViewMixin):
obj_type = IpList 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) 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: 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: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(ipv4__startswith=self.q) Q(ipv4__startswith=self.q)
) )
return qs

View file

@ -55,11 +55,11 @@ from re2o.acl import (
class RoomAutocomplete(AutocompleteViewMixin): class RoomAutocomplete(AutocompleteViewMixin):
obj_type = Room obj_type = Room
# Override get_queryset to add annotations so search behaves more like users expect it to # Precision on search to add annotations so search behaves more like users expect it to
def get_queryset(self): 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 name B, and rooms from 001 - 999
# Comments explain what we try to match # 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=Concat("building__name", Value(" "), "name"), # Match when the user searches "B 001"
full_name_stuck=Concat("building__name", "name"), # Match "B001" full_name_stuck=Concat("building__name", "name"), # Match "B001"
dorm_name=Concat("building__dormitory__name", Value(" "), "name"), # Match "Dorm 001" dorm_name=Concat("building__dormitory__name", Value(" "), "name"), # Match "Dorm 001"
@ -68,7 +68,7 @@ class RoomAutocomplete(AutocompleteViewMixin):
).all() ).all()
if self.q: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q) Q(full_name__icontains=self.q)
| Q(full_name_stuck__icontains=self.q) | Q(full_name_stuck__icontains=self.q)
| Q(dorm_name__icontains=self.q) | Q(dorm_name__icontains=self.q)
@ -76,8 +76,6 @@ class RoomAutocomplete(AutocompleteViewMixin):
| Q(dorm_full_colon_name__icontains=self.q) | Q(dorm_full_colon_name__icontains=self.q)
) )
return qs
#@can_view_all(Dormitory) #@can_view_all(Dormitory)
class DormitoryAutocomplete(AutocompleteViewMixin): class DormitoryAutocomplete(AutocompleteViewMixin):
@ -88,20 +86,20 @@ class DormitoryAutocomplete(AutocompleteViewMixin):
class BuildingAutocomplete(AutocompleteViewMixin): class BuildingAutocomplete(AutocompleteViewMixin):
obj_type = Building 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 # 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=Concat("dormitory__name", Value(" "), "name"),
full_name_colon=Concat("dormitory__name", Value(" : "), "name"), full_name_colon=Concat("dormitory__name", Value(" : "), "name"),
).all() ).all()
if self.q: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q) Q(full_name__icontains=self.q)
| Q(full_name_colon__icontains=self.q) | Q(full_name_colon__icontains=self.q)
) )
return qs
class SwitchAutocomplete(AutocompleteViewMixin): class SwitchAutocomplete(AutocompleteViewMixin):
obj_type = Switch obj_type = Switch
@ -110,38 +108,38 @@ class SwitchAutocomplete(AutocompleteViewMixin):
class PortAutocomplete(AutocompleteViewMixin): class PortAutocomplete(AutocompleteViewMixin):
obj_type = Port 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 # 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 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=Concat("switch__name", Value(" "), "port", output_field=CharField()),
full_name_stuck=Concat("switch__name", "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()), full_name_dash=Concat("switch__name", Value(" - "), "port", output_field=CharField()),
).all() ).all()
if self.q: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q) Q(full_name__icontains=self.q)
| Q(full_name_stuck__icontains=self.q) | Q(full_name_stuck__icontains=self.q)
| Q(full_name_dash__icontains=self.q) | Q(full_name_dash__icontains=self.q)
) )
return qs
class SwitchBayAutocomplete(AutocompleteViewMixin): class SwitchBayAutocomplete(AutocompleteViewMixin):
obj_type = SwitchBay 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 # 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 "" 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_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_name=Concat("building__dormitory__name", Value(" "), "building__name", Value(" "), "name"), # Match "Dorm J Local Sud"
).all() ).all()
if self.q: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q) Q(full_name__icontains=self.q)
| Q(dorm_name__icontains=self.q) | Q(dorm_name__icontains=self.q)
| Q(dorm_full_name__icontains=self.q) | Q(dorm_full_name__icontains=self.q)

View file

@ -61,24 +61,23 @@ class SchoolAutocomplete(AutocompleteViewMixin):
#@can_view_all(User) #@can_view_all(User)
class UserAutocomplete(AutocompleteViewMixin): class UserAutocomplete(AutocompleteViewMixin):
obj_type = User 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 # 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=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" full_name_reverse=Concat("surname", Value(" "), "adherent__name"), # Match when the user searches "Passoir Toto"
).all() ).all()
if self.q: if self.q:
qs = qs.filter( self.query_set = self.query_set.filter(
Q(pseudo__icontains=self.q) Q(pseudo__icontains=self.q)
| Q(full_name__icontains=self.q) | Q(full_name__icontains=self.q)
| Q(full_name_reverse__icontains=self.q) | Q(full_name_reverse__icontains=self.q)
) )
return qs #can_view_all(Adherent)
#@can_view_all(Adherent)
class AdherentAutocomplete(AutocompleteViewMixin): class AdherentAutocomplete(AutocompleteViewMixin):
obj_type = Adherent obj_type = Adherent