kpsul/bds/autocomplete.py

64 lines
1.8 KiB
Python
Raw Normal View History

2020-07-21 14:50:00 +02:00
from urllib.parse import urlencode
2020-06-05 17:02:16 +02:00
from django.contrib.auth import get_user_model
from django.db.models import Q
2020-07-21 14:50:00 +02:00
from django.urls import reverse
2020-07-01 22:29:07 +02:00
from django.utils.translation import gettext_lazy as _
2020-06-05 17:02:16 +02:00
from shared import autocomplete
2020-06-05 17:02:16 +02:00
User = get_user_model()
class BDSMemberSearch(autocomplete.ModelSearch):
model = User
search_fields = ["username", "first_name", "last_name"]
2020-07-01 22:29:07 +02:00
verbose_name = _("Membres du BDS")
2020-06-05 17:02:16 +02:00
def get_queryset_filter(self, *args, **kwargs):
qset_filter = super().get_queryset_filter(*args, **kwargs)
qset_filter &= Q(bds__is_member=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):
2020-07-21 14:50:00 +02:00
return reverse("bds:user.update", args=(user.pk,))
2020-07-01 22:29:07 +02:00
2020-06-05 17:02:16 +02:00
class BDSOthersSearch(autocomplete.ModelSearch):
model = User
search_fields = ["username", "first_name", "last_name"]
2020-07-01 22:29:07 +02:00
verbose_name = _("Non-membres du BDS")
2020-06-05 17:02:16 +02:00
def get_queryset_filter(self, *args, **kwargs):
qset_filter = super().get_queryset_filter(*args, **kwargs)
qset_filter &= Q(bds__isnull=True) | Q(bds__is_member=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):
2020-07-21 14:50:00 +02:00
return reverse("bds:user.update", args=(user.pk,))
2020-07-01 22:29:07 +02:00
class BDSLDAPSearch(autocomplete.LDAPSearch):
def result_link(self, clipper):
2020-07-21 14:50:00 +02:00
url = reverse("bds:user.create.fromclipper", args=(clipper.clipper,))
2020-08-03 14:30:21 +02:00
get = {"fullname": clipper.fullname, "mail": clipper.mail}
2020-07-21 14:50:00 +02:00
return "{}?{}".format(url, urlencode(get))
2020-07-01 22:29:07 +02:00
2020-06-05 17:02:16 +02:00
class BDSSearch(autocomplete.Compose):
search_units = [
("members", BDSMemberSearch()),
("others", BDSOthersSearch()),
2020-07-01 22:29:07 +02:00
("clippers", BDSLDAPSearch()),
2020-06-05 17:02:16 +02:00
]
bds_search = BDSSearch()