Uses the cof_members groups in kfet's autocomplete

The is_cof attribute cannot be used in database queries anymore
This commit is contained in:
Martin Pépin 2017-02-22 15:03:34 +01:00
parent 68b38228a9
commit 5632cdaa22

View file

@ -6,7 +6,7 @@ from django.shortcuts import render
from django.http import Http404 from django.http import Http404
from django.db.models import Q from django.db.models import Q
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User, Group
from kfet.decorators import teamkfet_required from kfet.decorators import teamkfet_required
from kfet.models import Account from kfet.models import Account
@ -23,6 +23,7 @@ def account_create(request):
if "q" not in request.GET: if "q" not in request.GET:
raise Http404 raise Http404
q = request.GET.get("q") q = request.GET.get("q")
cof_members = Group.objects.get(name="cof_members")
if (len(q) == 0): if (len(q) == 0):
return render(request, "kfet/account_create_autocomplete.html") return render(request, "kfet/account_create_autocomplete.html")
@ -32,10 +33,10 @@ def account_create(request):
queries = {} queries = {}
search_words = q.split() search_words = q.split()
# Fetching data from User, CofProfile and Account tables # Fetching data from User and Account tables
queries['kfet'] = Account.objects queries['kfet'] = Account.objects
queries['users_cof'] = User.objects.filter(profile__cof__is_cof=True) queries['users_cof'] = User.objects.filter(groups=cof_members)
queries['users_notcof'] = User.objects.filter(profile__cof__is_cof=False) queries['users_notcof'] = User.objects.exclude(groups=cof_members)
for word in search_words: for word in search_words:
queries['kfet'] = queries['kfet'].filter( queries['kfet'] = queries['kfet'].filter(
@ -57,26 +58,31 @@ def account_create(request):
# Clearing redundancies # Clearing redundancies
queries['kfet'] = queries['kfet'].distinct() queries['kfet'] = queries['kfet'].distinct()
usernames = set( usernames = set(
queries['kfet'].values_list('profile__user__username', flat=True)) queries['kfet'].values_list('profile__user__username', flat=True)
)
queries['kfet'] = [ queries['kfet'] = [
(account, account.profile.user) (account, account.profile.user)
for account in queries['kfet'] for account in queries['kfet']
] ]
queries['users_cof'] = \ queries['users_cof'] = (
queries['users_cof'].exclude(username__in=usernames).distinct() queries['users_cof'].exclude(username__in=usernames).distinct()
queries['users_notcof'] = \ )
queries['users_notcof'] = (
queries['users_notcof'].exclude(username__in=usernames).distinct() queries['users_notcof'].exclude(username__in=usernames).distinct()
)
usernames |= set( usernames |= set(
queries['users_cof'].values_list('username', flat=True)) queries['users_cof'].values_list('username', flat=True)
)
usernames |= set( usernames |= set(
queries['users_notcof'].values_list('username', flat=True)) queries['users_notcof'].values_list('username', flat=True)
)
# Fetching data from the SPI # Fetching data from the SPI
if hasattr(settings, 'LDAP_SERVER_URL'): if hasattr(settings, 'LDAP_SERVER_URL'):
# Fetching # Fetching
ldap_query = '(|{:s})'.format(''.join( ldap_query = '(|{:s})'.format(''.join(
['(cn=*{bit:s}*)(uid=*{bit:s}*)'.format(**{"bit": bit}) ['(cn=*{bit:s}*)(uid=*{bit:s}*)'.format(bit=bit)
for bit in search_words] for bit in search_words]
)) ))
with Connection(settings.LDAP_SERVER_URL) as conn: with Connection(settings.LDAP_SERVER_URL) as conn:
@ -94,6 +100,6 @@ def account_create(request):
# Resulting data # Resulting data
data.update(queries) data.update(queries)
data['options'] = sum([len(query) for query in queries]) data['options'] = any(queries.values())
return render(request, "kfet/account_create_autocomplete.html", data) return render(request, "kfet/account_create_autocomplete.html", data)