2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-02-12 04:24:33 +01:00
|
|
|
from ldap3 import Connection
|
2016-09-01 00:45:44 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.http import Http404
|
|
|
|
from django.db.models import Q
|
2016-12-25 02:02:22 +01:00
|
|
|
from django.conf import settings
|
2017-02-22 15:03:34 +01:00
|
|
|
from django.contrib.auth.models import User, Group
|
2016-12-25 02:02:22 +01:00
|
|
|
|
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
|
|
|
|
2016-12-25 12:27:42 +01:00
|
|
|
|
|
|
|
class Clipper(object):
|
|
|
|
def __init__(self, clipper, fullname):
|
|
|
|
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")
|
2017-02-22 15:03:34 +01:00
|
|
|
cof_members = Group.objects.get(name="cof_members")
|
2016-08-02 10:40:46 +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
|
|
|
|
|
|
|
data = {'q': q}
|
|
|
|
|
|
|
|
queries = {}
|
|
|
|
search_words = q.split()
|
|
|
|
|
2017-02-22 15:03:34 +01:00
|
|
|
# Fetching data from User and Account tables
|
2017-02-12 04:24:33 +01:00
|
|
|
queries['kfet'] = Account.objects
|
2017-02-22 15:03:34 +01:00
|
|
|
queries['users_cof'] = User.objects.filter(groups=cof_members)
|
|
|
|
queries['users_notcof'] = User.objects.exclude(groups=cof_members)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
for word in search_words:
|
2016-08-03 04:38:54 +02:00
|
|
|
queries['kfet'] = queries['kfet'].filter(
|
2017-02-22 15:03:34 +01:00
|
|
|
Q(profile__user__username__icontains=word)
|
|
|
|
| Q(profile__user__first_name__icontains=word)
|
|
|
|
| Q(profile__user__last_name__icontains=word)
|
|
|
|
)
|
2016-08-03 04:38:54 +02:00
|
|
|
queries['users_cof'] = queries['users_cof'].filter(
|
2017-02-12 04:24:33 +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
|
|
|
queries['users_notcof'] = queries['users_notcof'].filter(
|
2017-02-12 04:24:33 +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
|
2016-08-03 04:38:54 +02:00
|
|
|
queries['kfet'] = queries['kfet'].distinct()
|
2017-02-11 02:31:55 +01:00
|
|
|
usernames = set(
|
2017-02-22 15:03:34 +01:00
|
|
|
queries['kfet'].values_list('profile__user__username', flat=True)
|
|
|
|
)
|
2016-12-25 02:02:22 +01:00
|
|
|
queries['kfet'] = [
|
2017-02-12 04:24:33 +01:00
|
|
|
(account, account.profile.user)
|
2016-12-25 02:02:22 +01:00
|
|
|
for account in queries['kfet']
|
|
|
|
]
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2017-02-22 15:03:34 +01:00
|
|
|
queries['users_cof'] = (
|
2016-08-03 04:38:54 +02:00
|
|
|
queries['users_cof'].exclude(username__in=usernames).distinct()
|
2017-02-22 15:03:34 +01:00
|
|
|
)
|
|
|
|
queries['users_notcof'] = (
|
2016-08-03 04:38:54 +02:00
|
|
|
queries['users_notcof'].exclude(username__in=usernames).distinct()
|
2017-02-22 15:03:34 +01:00
|
|
|
)
|
2017-02-11 02:31:55 +01:00
|
|
|
usernames |= set(
|
2017-02-22 15:03:34 +01:00
|
|
|
queries['users_cof'].values_list('username', flat=True)
|
|
|
|
)
|
2017-02-11 02:31:55 +01:00
|
|
|
usernames |= set(
|
2017-02-22 15:03:34 +01:00
|
|
|
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
|
|
|
|
if hasattr(settings, 'LDAP_SERVER_URL'):
|
|
|
|
# Fetching
|
|
|
|
ldap_query = '(|{:s})'.format(''.join(
|
2017-02-22 15:03:34 +01:00
|
|
|
['(cn=*{bit:s}*)(uid=*{bit:s}*)'.format(bit=bit)
|
2017-02-12 04:24:33 +01:00
|
|
|
for bit in search_words]
|
2016-12-25 02:02:22 +01:00
|
|
|
))
|
|
|
|
with Connection(settings.LDAP_SERVER_URL) as conn:
|
2016-12-25 12:27:42 +01:00
|
|
|
conn.search(
|
2016-12-25 02:02:22 +01:00
|
|
|
'dc=spi,dc=ens,dc=fr', ldap_query,
|
|
|
|
attributes=['uid', 'cn']
|
|
|
|
)
|
2016-12-25 12:27:42 +01:00
|
|
|
queries['clippers'] = conn.entries
|
2016-12-25 02:02:22 +01:00
|
|
|
# Clearing redundancies
|
|
|
|
queries['clippers'] = [
|
2016-12-25 12:27:42 +01:00
|
|
|
Clipper(clipper.uid, clipper.cn)
|
2016-12-25 02:02:22 +01:00
|
|
|
for clipper in queries['clippers']
|
2017-02-11 02:31:55 +01:00
|
|
|
if str(clipper.uid) not in usernames
|
2016-12-25 02:02:22 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
# Resulting data
|
2016-08-02 10:40:46 +02:00
|
|
|
data.update(queries)
|
2017-02-22 15:03:34 +01:00
|
|
|
data['options'] = any(queries.values())
|
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)
|