2016-05-21 23:57:36 +02:00
|
|
|
from django import shortcuts
|
2016-12-25 02:02:22 +01:00
|
|
|
from django.conf import settings
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.http import Http404
|
2016-12-25 02:02:22 +01:00
|
|
|
|
2016-09-24 19:40:16 +02:00
|
|
|
from gestioncof.decorators import buro_required
|
2018-10-06 12:35:49 +02:00
|
|
|
from gestioncof.models import CofProfile
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2019-11-22 14:58:56 +01:00
|
|
|
if getattr(settings, "LDAP_SERVER_URL", None):
|
|
|
|
from ldap3 import Connection
|
|
|
|
else:
|
|
|
|
# shared.tests.testcases.TestCaseMixin.mockLDAP needs
|
|
|
|
# Connection to be defined in order to mock it.
|
|
|
|
Connection = None
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-12-25 12:29:53 +01:00
|
|
|
class Clipper(object):
|
|
|
|
def __init__(self, clipper, fullname):
|
2017-04-01 22:45:05 +02:00
|
|
|
if fullname is None:
|
|
|
|
fullname = ""
|
|
|
|
assert isinstance(clipper, str)
|
|
|
|
assert isinstance(fullname, str)
|
2016-12-25 12:29:53 +01:00
|
|
|
self.clipper = clipper
|
|
|
|
self.fullname = fullname
|
|
|
|
|
2018-01-20 16:14:55 +01:00
|
|
|
def __str__(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "{} ({})".format(self.clipper, self.fullname)
|
2018-01-20 16:14:55 +01:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2018-10-06 12:35:49 +02:00
|
|
|
return self.clipper == other.clipper and self.fullname == other.fullname
|
2018-01-20 16:14:55 +01:00
|
|
|
|
2016-12-25 12:29:53 +01:00
|
|
|
|
2016-09-24 19:40:16 +02:00
|
|
|
@buro_required
|
2016-05-21 23:57:36 +02:00
|
|
|
def autocomplete(request):
|
|
|
|
if "q" not in request.GET:
|
|
|
|
raise Http404
|
2018-10-06 12:35:49 +02:00
|
|
|
q = request.GET["q"]
|
|
|
|
data = {"q": q}
|
2016-05-21 23:57:36 +02:00
|
|
|
|
|
|
|
queries = {}
|
|
|
|
bits = q.split()
|
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Fetching data from User and CofProfile tables
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["members"] = CofProfile.objects.filter(is_cof=True)
|
|
|
|
queries["users"] = User.objects.filter(profile__is_cof=False)
|
2016-05-21 23:57:36 +02:00
|
|
|
for bit in bits:
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["members"] = queries["members"].filter(
|
2016-12-25 02:02:22 +01:00
|
|
|
Q(user__first_name__icontains=bit)
|
|
|
|
| Q(user__last_name__icontains=bit)
|
|
|
|
| Q(user__username__icontains=bit)
|
2018-10-06 12:35:49 +02:00
|
|
|
| Q(login_clipper__icontains=bit)
|
|
|
|
)
|
|
|
|
queries["users"] = queries["users"].filter(
|
2016-12-25 02:02:22 +01:00
|
|
|
Q(first_name__icontains=bit)
|
|
|
|
| Q(last_name__icontains=bit)
|
2018-10-06 12:35:49 +02:00
|
|
|
| Q(username__icontains=bit)
|
|
|
|
)
|
|
|
|
queries["members"] = queries["members"].distinct()
|
|
|
|
queries["users"] = queries["users"].distinct()
|
2016-12-25 02:02:22 +01:00
|
|
|
|
|
|
|
# Clearing redundancies
|
2018-10-06 12:35:49 +02:00
|
|
|
usernames = set(queries["members"].values_list("login_clipper", flat="True")) | set(
|
|
|
|
queries["users"].values_list("profile__login_clipper", flat="True")
|
2016-12-25 02:02:22 +01:00
|
|
|
)
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Fetching data from the SPI
|
2018-10-06 12:35:49 +02:00
|
|
|
if getattr(settings, "LDAP_SERVER_URL", None):
|
2016-12-25 02:02:22 +01:00
|
|
|
# Fetching
|
2018-10-06 12:35:49 +02:00
|
|
|
ldap_query = "(&{:s})".format(
|
|
|
|
"".join(
|
|
|
|
"(|(cn=*{bit:s}*)(uid=*{bit:s}*))".format(bit=bit)
|
|
|
|
for bit in bits
|
|
|
|
if bit.isalnum()
|
|
|
|
)
|
|
|
|
)
|
2017-03-20 09:14:20 +01:00
|
|
|
if ldap_query != "(&)":
|
2017-03-16 23:43:43 +01:00
|
|
|
# If none of the bits were legal, we do not perform the query
|
2017-04-01 22:45:05 +02:00
|
|
|
entries = None
|
2017-03-16 23:43:43 +01:00
|
|
|
with Connection(settings.LDAP_SERVER_URL) as conn:
|
2018-10-06 12:35:49 +02:00
|
|
|
conn.search("dc=spi,dc=ens,dc=fr", ldap_query, attributes=["uid", "cn"])
|
2017-04-01 22:45:05 +02:00
|
|
|
entries = conn.entries
|
2017-03-16 23:43:43 +01:00
|
|
|
# Clearing redundancies
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["clippers"] = [
|
2017-04-01 22:45:05 +02:00
|
|
|
Clipper(entry.uid.value, entry.cn.value)
|
|
|
|
for entry in entries
|
2018-10-06 12:35:49 +02:00
|
|
|
if entry.uid.value and entry.uid.value not in usernames
|
2017-03-16 23:43:43 +01:00
|
|
|
]
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Resulting data
|
|
|
|
data.update(queries)
|
2018-10-06 12:35:49 +02:00
|
|
|
data["options"] = sum(len(query) for query in queries)
|
2016-05-21 23:57:36 +02:00
|
|
|
|
|
|
|
return shortcuts.render(request, "autocomplete_user.html", data)
|