2020-06-20 19:28:48 +02: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 _
|
2020-06-20 19:28:48 +02:00
|
|
|
|
2020-07-04 13:50:19 +02:00
|
|
|
from shared import autocomplete
|
2020-06-20 19:28:48 +02:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class KfetAccountSearch(autocomplete.ModelSearch):
|
|
|
|
model = User
|
|
|
|
search_fields = [
|
|
|
|
"username",
|
|
|
|
"first_name",
|
|
|
|
"last_name",
|
|
|
|
"profile__account_kfet__trigramme",
|
2016-12-25 02:02:22 +01:00
|
|
|
]
|
2020-07-01 22:29:07 +02:00
|
|
|
verbose_name = _("Comptes existants")
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2020-06-20 19:28:48 +02:00
|
|
|
def get_queryset_filter(self, *args, **kwargs):
|
|
|
|
qset_filter = super().get_queryset_filter(*args, **kwargs)
|
|
|
|
qset_filter &= Q(profile__account_kfet__isnull=False)
|
|
|
|
return qset_filter
|
|
|
|
|
2020-07-01 22:29:07 +02:00
|
|
|
def result_verbose_name(self, user):
|
|
|
|
return "{} ({})".format(user, user.profile.account_kfet.trigramme)
|
|
|
|
|
2020-07-04 13:06:24 +02:00
|
|
|
def result_uuid(self, user):
|
|
|
|
return user.username
|
|
|
|
|
2020-06-20 19:28:48 +02:00
|
|
|
|
|
|
|
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")
|
2020-06-20 19:28:48 +02:00
|
|
|
|
|
|
|
def get_queryset_filter(self, *args, **kwargs):
|
|
|
|
qset_filter = super().get_queryset_filter(*args, **kwargs)
|
|
|
|
qset_filter &= Q(profile__account_kfet__isnull=True) & 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("kfet.account.create.fromuser", args=(user.username,))
|
|
|
|
|
2020-06-20 19:28:48 +02:00
|
|
|
|
|
|
|
class OthersSearch(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")
|
2020-06-20 19:28:48 +02:00
|
|
|
|
|
|
|
def get_queryset_filter(self, *args, **kwargs):
|
|
|
|
qset_filter = super().get_queryset_filter(*args, **kwargs)
|
|
|
|
qset_filter &= Q(profile__account_kfet__isnull=True) & 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("kfet.account.create.fromuser", args=(user.username,))
|
|
|
|
|
|
|
|
|
|
|
|
class KfetLDAPSearch(autocomplete.LDAPSearch):
|
|
|
|
def result_link(self, clipper):
|
|
|
|
return reverse(
|
|
|
|
"kfet.account.create.fromclipper", args=(clipper.clipper, clipper.fullname)
|
|
|
|
)
|
|
|
|
|
2020-06-20 19:28:48 +02:00
|
|
|
|
|
|
|
class KfetAutocomplete(autocomplete.Compose):
|
|
|
|
search_units = [
|
2020-07-04 13:06:24 +02:00
|
|
|
("kfet", KfetAccountSearch()),
|
|
|
|
("users_cof", COFMemberSearch()),
|
|
|
|
("users_notcof", OthersSearch()),
|
2020-07-01 22:29:07 +02:00
|
|
|
("clippers", KfetLDAPSearch()),
|
2018-10-06 12:35:49 +02:00
|
|
|
]
|
2020-07-01 22:29:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KfetAccountOnlyAutocomplete(autocomplete.Compose):
|
|
|
|
search_units = [("kfet", KfetAccountSearch())]
|