gestioCOF/gestioncof/autocomplete.py

59 lines
1.6 KiB
Python
Raw Normal View History

2019-12-11 22:00:10 +01:00
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from shared import autocomplete
2019-12-11 22:00:10 +01:00
User = get_user_model()
class COFMemberSearch(autocomplete.ModelSearch):
model = User
search_fields = ["username", "first_name", "last_name"]
verbose_name = _("Membres du COF")
2019-12-11 22:00:10 +01:00
def get_queryset_filter(self, *args, **kwargs):
qset_filter = super().get_queryset_filter(*args, **kwargs)
qset_filter &= Q(profile__is_cof=True)
return qset_filter
def result_uuid(self, user):
return user.username
def result_link(self, user):
return reverse("user-registration", args=(user.username,))
2019-12-11 22:00:10 +01:00
class COFOthersSearch(autocomplete.ModelSearch):
model = User
search_fields = ["username", "first_name", "last_name"]
verbose_name = _("Non-membres du COF")
2019-12-11 22:00:10 +01:00
def get_queryset_filter(self, *args, **kwargs):
qset_filter = super().get_queryset_filter(*args, **kwargs)
qset_filter &= Q(profile__is_cof=False)
return qset_filter
def result_uuid(self, user):
return user.username
2019-12-11 22:00:10 +01:00
def result_link(self, user):
return reverse("user-registration", args=(user.username,))
2019-12-11 22:00:10 +01:00
class COFLDAPSearch(autocomplete.LDAPSearch):
def result_link(self, clipper):
return reverse("clipper-registration", args=(clipper.clipper, clipper.fullname))
2019-12-11 22:00:10 +01:00
class COFSearch(autocomplete.Compose):
search_units = [
("members", COFMemberSearch()),
("others", COFOthersSearch()),
("clippers", COFLDAPSearch()),
]
2019-12-11 22:00:10 +01:00
cof_search = COFSearch()