kpsul/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
2020-07-01 22:29:07 +02:00
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"]
2020-07-01 22:29:07 +02:00
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
2020-07-01 22:29:07 +02:00
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"]
2020-07-01 22:29:07 +02:00
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
2020-07-01 22:29:07 +02:00
def result_link(self, user):
return reverse("user-registration", args=(user.username,))
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 COFAutocomplete(autocomplete.Compose):
2019-12-11 22:00:10 +01:00
search_units = [
("members", COFMemberSearch()),
("others", COFOthersSearch()),
2020-07-01 22:29:07 +02:00
("clippers", COFLDAPSearch()),
2019-12-11 22:00:10 +01:00
]
2020-07-01 22:29:07 +02:00
cof_autocomplete = COFAutocomplete()