2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
from ldap3 import Connection
|
2016-05-26 22:44:10 +02:00
|
|
|
|
2016-05-21 23:57:36 +02:00
|
|
|
from django import shortcuts
|
|
|
|
from django.http import Http404
|
|
|
|
from django.db.models import Q
|
2017-02-22 14:46:44 +01:00
|
|
|
from django.contrib.auth.models import User, Group
|
2017-02-09 21:04:32 +01:00
|
|
|
from .decorators import buro_required
|
2016-12-25 02:02:22 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-12-25 12:29:53 +01:00
|
|
|
class Clipper(object):
|
|
|
|
def __init__(self, clipper, fullname):
|
|
|
|
self.clipper = clipper
|
|
|
|
self.fullname = fullname
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2016-07-09 21:19:37 +02: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
|
|
|
|
q = request.GET['q']
|
2017-02-22 14:46:44 +01:00
|
|
|
data = {'q': q}
|
|
|
|
cof_members = Group.objects.get(name="cof_members")
|
2016-05-21 23:57:36 +02:00
|
|
|
|
|
|
|
queries = {}
|
|
|
|
bits = q.split()
|
|
|
|
|
2017-02-12 04:24:33 +01:00
|
|
|
# Fetching data from User and Profile tables
|
2017-02-22 14:46:44 +01:00
|
|
|
queries['members'] = User.objects.filter(groups=cof_members)
|
|
|
|
queries['users'] = User.objects.exclude(groups=cof_members)
|
2016-05-21 23:57:36 +02:00
|
|
|
for bit in bits:
|
|
|
|
queries['members'] = queries['members'].filter(
|
2017-02-22 14:46:44 +01:00
|
|
|
Q(first_name__icontains=bit)
|
|
|
|
| Q(last_name__icontains=bit)
|
|
|
|
| Q(username__icontains=bit)
|
2017-02-11 18:41:47 +01:00
|
|
|
| Q(profile__login_clipper__icontains=bit))
|
2016-05-21 23:57:36 +02:00
|
|
|
queries['users'] = queries['users'].filter(
|
2016-12-25 02:02:22 +01:00
|
|
|
Q(first_name__icontains=bit)
|
|
|
|
| Q(last_name__icontains=bit)
|
|
|
|
| Q(username__icontains=bit))
|
2016-05-21 23:57:36 +02:00
|
|
|
queries['members'] = queries['members'].distinct()
|
|
|
|
queries['users'] = queries['users'].distinct()
|
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Clearing redundancies
|
|
|
|
usernames = (
|
2017-02-22 14:46:44 +01:00
|
|
|
set(queries['members'].values_list('profile__login_clipper',
|
|
|
|
flat='True'))
|
2017-02-11 02:31:55 +01:00
|
|
|
| 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
|
|
|
|
if hasattr(settings, 'LDAP_SERVER_URL'):
|
|
|
|
# Fetching
|
|
|
|
ldap_query = '(|{:s})'.format(''.join(
|
2017-02-22 14:46:44 +01:00
|
|
|
['(cn=*{bit:s}*)(uid=*{bit:s}*)'.format(bit=bit)
|
2017-02-11 02:31:55 +01:00
|
|
|
for bit in bits]
|
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(
|
|
|
|
'dc=spi,dc=ens,dc=fr', ldap_query,
|
2016-12-25 02:02:22 +01:00
|
|
|
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
|
|
|
]
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-12-25 02:02:22 +01:00
|
|
|
# Resulting data
|
|
|
|
data.update(queries)
|
2017-02-22 14:46:44 +01:00
|
|
|
data['options'] = any(bool(query) for query in queries.values())
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2017-02-22 14:46:44 +01:00
|
|
|
return shortcuts.render(request, "cof/autocomplete_user.html", data)
|