2016-12-25 02:02:22 +01:00
|
|
|
from django.conf import settings
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import render
|
2016-12-25 02:02:22 +01:00
|
|
|
|
|
|
|
from gestioncof.models import User
|
2016-09-24 19:31:50 +02:00
|
|
|
from kfet.decorators import teamkfet_required
|
2016-08-03 04:38:54 +02:00
|
|
|
from kfet.models import Account
|
2016-08-02 10:40:46 +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-12-25 12:27:42 +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:27:42 +01:00
|
|
|
self.clipper = clipper
|
|
|
|
self.fullname = fullname
|
|
|
|
|
|
|
|
|
2016-09-24 19:31:50 +02:00
|
|
|
@teamkfet_required
|
2016-08-03 04:38:54 +02:00
|
|
|
def account_create(request):
|
2016-08-02 10:40:46 +02:00
|
|
|
if "q" not in request.GET:
|
|
|
|
raise Http404
|
|
|
|
q = request.GET.get("q")
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
if len(q) == 0:
|
2016-08-03 04:38:54 +02:00
|
|
|
return render(request, "kfet/account_create_autocomplete.html")
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
data = {"q": q}
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
queries = {}
|
|
|
|
search_words = q.split()
|
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Fetching data from User, CofProfile and Account tables
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["kfet"] = Account.objects
|
|
|
|
queries["users_cof"] = User.objects.filter(profile__is_cof=True)
|
|
|
|
queries["users_notcof"] = User.objects.filter(profile__is_cof=False)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
for word in search_words:
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["kfet"] = queries["kfet"].filter(
|
2017-03-15 20:02:31 +01:00
|
|
|
Q(cofprofile__user__username__icontains=word)
|
|
|
|
| Q(cofprofile__user__first_name__icontains=word)
|
|
|
|
| Q(cofprofile__user__last_name__icontains=word)
|
2016-12-25 02:02:22 +01:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["users_cof"] = queries["users_cof"].filter(
|
2017-03-15 20:02:31 +01:00
|
|
|
Q(username__icontains=word)
|
|
|
|
| Q(first_name__icontains=word)
|
|
|
|
| Q(last_name__icontains=word)
|
2016-12-25 02:02:22 +01:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["users_notcof"] = queries["users_notcof"].filter(
|
2017-03-15 20:02:31 +01:00
|
|
|
Q(username__icontains=word)
|
|
|
|
| Q(first_name__icontains=word)
|
|
|
|
| Q(last_name__icontains=word)
|
2016-12-25 02:02:22 +01:00
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Clearing redundancies
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["kfet"] = queries["kfet"].distinct()
|
2017-02-11 02:31:55 +01:00
|
|
|
usernames = set(
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["kfet"].values_list("cofprofile__user__username", flat=True)
|
|
|
|
)
|
|
|
|
queries["kfet"] = [
|
|
|
|
(account, account.cofprofile.user) for account in queries["kfet"]
|
2016-12-25 02:02:22 +01:00
|
|
|
]
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
queries["users_cof"] = (
|
|
|
|
queries["users_cof"].exclude(username__in=usernames).distinct()
|
|
|
|
)
|
|
|
|
queries["users_notcof"] = (
|
|
|
|
queries["users_notcof"].exclude(username__in=usernames).distinct()
|
|
|
|
)
|
|
|
|
usernames |= set(queries["users_cof"].values_list("username", flat=True))
|
|
|
|
usernames |= set(queries["users_notcof"].values_list("username", flat=True))
|
2016-08-02 10:40:46 +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=word)
|
|
|
|
for word in search_words
|
|
|
|
if word.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-12-25 02:02:22 +01:00
|
|
|
|
|
|
|
# Resulting data
|
2016-08-02 10:40:46 +02:00
|
|
|
data.update(queries)
|
2018-10-06 12:35:49 +02:00
|
|
|
data["options"] = sum([len(query) for query in queries])
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
return render(request, "kfet/account_create_autocomplete.html", data)
|
2017-01-19 15:36:40 +01:00
|
|
|
|
|
|
|
|
2017-08-16 22:54:40 +02:00
|
|
|
@teamkfet_required
|
2017-01-19 15:36:40 +01:00
|
|
|
def account_search(request):
|
|
|
|
if "q" not in request.GET:
|
|
|
|
raise Http404
|
|
|
|
q = request.GET.get("q")
|
|
|
|
words = q.split()
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
data = {"q": q}
|
2017-01-19 15:36:40 +01:00
|
|
|
|
|
|
|
for word in words:
|
|
|
|
query = Account.objects.filter(
|
2018-10-06 12:35:49 +02:00
|
|
|
Q(cofprofile__user__username__icontains=word)
|
|
|
|
| Q(cofprofile__user__first_name__icontains=word)
|
|
|
|
| Q(cofprofile__user__last_name__icontains=word)
|
|
|
|
).distinct()
|
2017-01-19 15:36:40 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
query = [
|
|
|
|
(account.trigramme, account.cofprofile.user.get_full_name())
|
|
|
|
for account in query
|
|
|
|
]
|
2017-01-19 15:36:40 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
data["accounts"] = query
|
|
|
|
return render(request, "kfet/account_search_autocomplete.html", data)
|