8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 03:13:12 +00:00

Replace get_queryset with filter_results

This commit is contained in:
chirac 2020-12-30 19:22:43 +01:00
parent 5011694479
commit a5e3016119
3 changed files with 31 additions and 38 deletions

View file

@ -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

View file

@ -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)

View file

@ -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