mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-26 00:43:46 +00:00
Improved filter for user search
This commit is contained in:
parent
46f3f564b9
commit
131ee346c5
1 changed files with 20 additions and 1 deletions
|
@ -51,6 +51,9 @@ from re2o.acl import (
|
||||||
can_view_all,
|
can_view_all,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from django.db.models import Q, Value, CharField
|
||||||
|
from django.db.models.functions import Concat
|
||||||
|
|
||||||
#@can_view_all(School)
|
#@can_view_all(School)
|
||||||
class SchoolAutocomplete(AutocompleteViewMixin):
|
class SchoolAutocomplete(AutocompleteViewMixin):
|
||||||
obj_type = School
|
obj_type = School
|
||||||
|
@ -58,6 +61,22 @@ 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):
|
||||||
|
# Comments explain what we try to match
|
||||||
|
qs = self.obj_type.objects.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(
|
||||||
|
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):
|
class AdherentAutocomplete(AutocompleteViewMixin):
|
||||||
|
|
Loading…
Reference in a new issue