2019-12-11 22:00:10 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2018-10-06 12:35:49 +02:00
|
|
|
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 _
|
2016-12-25 02:02:22 +01:00
|
|
|
|
2020-07-04 13:50:19 +02:00
|
|
|
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
|
|
|
|
|
2020-07-04 13:06:24 +02:00
|
|
|
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
|
|
|
|
|
2020-07-04 13:06:24 +02:00
|
|
|
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
|
|
|
|
2020-07-18 16:05:16 +02:00
|
|
|
class COFAutocomplete(autocomplete.Compose):
|
2019-12-11 22:00:10 +01:00
|
|
|
search_units = [
|
2020-07-04 13:06:24 +02:00
|
|
|
("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
|
|
|
|
|
|
|
|
2020-07-18 16:05:16 +02:00
|
|
|
cof_autocomplete = COFAutocomplete()
|