forked from DGNum/gestioCOF
Optimize code, better queries
- Remove useless Q queries - Optimize with sets - Queries over clipper id too
This commit is contained in:
parent
d4b696db70
commit
02a8e74e3b
2 changed files with 16 additions and 15 deletions
|
@ -31,8 +31,8 @@ def autocomplete(request):
|
||||||
bits = q.split()
|
bits = q.split()
|
||||||
|
|
||||||
# Fetching data from User and CofProfile tables
|
# Fetching data from User and CofProfile tables
|
||||||
queries['members'] = CofProfile.objects.filter(Q(is_cof=True))
|
queries['members'] = CofProfile.objects.filter(is_cof=True)
|
||||||
queries['users'] = User.objects.filter(Q(profile__is_cof=False))
|
queries['users'] = User.objects.filter(profile__is_cof=False)
|
||||||
for bit in bits:
|
for bit in bits:
|
||||||
queries['members'] = queries['members'].filter(
|
queries['members'] = queries['members'].filter(
|
||||||
Q(user__first_name__icontains=bit)
|
Q(user__first_name__icontains=bit)
|
||||||
|
@ -48,16 +48,17 @@ def autocomplete(request):
|
||||||
|
|
||||||
# Clearing redundancies
|
# Clearing redundancies
|
||||||
usernames = (
|
usernames = (
|
||||||
list(queries['members'].values_list('login_clipper', flat='True'))
|
set(queries['members'].values_list('login_clipper', flat='True'))
|
||||||
+ list(queries['users'].values_list('profile__login_clipper',
|
| set(queries['users'].values_list('profile__login_clipper',
|
||||||
flat='True'))
|
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=*{:s}*)'.format(bit) for bit in bits]
|
['(cn=*{bit:s}*)(uid=*{bit:s}*)'.format(**{"bit": bit})
|
||||||
|
for bit in bits]
|
||||||
))
|
))
|
||||||
with Connection(settings.LDAP_SERVER_URL) as conn:
|
with Connection(settings.LDAP_SERVER_URL) as conn:
|
||||||
conn.search(
|
conn.search(
|
||||||
|
@ -69,11 +70,11 @@ def autocomplete(request):
|
||||||
queries['clippers'] = [
|
queries['clippers'] = [
|
||||||
Clipper(clipper.uid, clipper.cn)
|
Clipper(clipper.uid, clipper.cn)
|
||||||
for clipper in queries['clippers']
|
for clipper in queries['clippers']
|
||||||
if clipper.uid not in usernames
|
if str(clipper.uid) not in usernames
|
||||||
]
|
]
|
||||||
|
|
||||||
# Resulting data
|
# Resulting data
|
||||||
data.update(queries)
|
data.update(queries)
|
||||||
data['options'] = sum([len(query) for query in queries])
|
data['options'] = sum(len(query) for query in queries)
|
||||||
|
|
||||||
return shortcuts.render(request, "autocomplete_user.html", data)
|
return shortcuts.render(request, "autocomplete_user.html", data)
|
||||||
|
|
|
@ -34,8 +34,8 @@ def account_create(request):
|
||||||
|
|
||||||
# Fetching data from User, CofProfile and Account tables
|
# Fetching data from User, CofProfile and Account tables
|
||||||
queries['kfet'] = Account.objects
|
queries['kfet'] = Account.objects
|
||||||
queries['users_cof'] = User.objects.filter(Q(profile__is_cof = True))
|
queries['users_cof'] = User.objects.filter(profile__is_cof = True)
|
||||||
queries['users_notcof'] = User.objects.filter(Q(profile__is_cof = False))
|
queries['users_notcof'] = User.objects.filter(profile__is_cof = False)
|
||||||
|
|
||||||
for word in search_words:
|
for word in search_words:
|
||||||
queries['kfet'] = queries['kfet'].filter(
|
queries['kfet'] = queries['kfet'].filter(
|
||||||
|
@ -56,7 +56,7 @@ def account_create(request):
|
||||||
|
|
||||||
# Clearing redundancies
|
# Clearing redundancies
|
||||||
queries['kfet'] = queries['kfet'].distinct()
|
queries['kfet'] = queries['kfet'].distinct()
|
||||||
usernames = list(
|
usernames = set(
|
||||||
queries['kfet'].values_list('cofprofile__user__username', flat=True))
|
queries['kfet'].values_list('cofprofile__user__username', flat=True))
|
||||||
queries['kfet'] = [
|
queries['kfet'] = [
|
||||||
(account, account.cofprofile.user)
|
(account, account.cofprofile.user)
|
||||||
|
@ -67,16 +67,16 @@ def account_create(request):
|
||||||
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 += list(
|
usernames |= set(
|
||||||
queries['users_cof'].values_list('username', flat=True))
|
queries['users_cof'].values_list('username', flat=True))
|
||||||
usernames += list(
|
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=*{:s}*)'.format(bit) for bit in bits]
|
['(cn=*{bit:s}*)(uid=*{bit:s}*)'.format(**{"bit": bit}) for bit in bits]
|
||||||
))
|
))
|
||||||
with Connection(settings.LDAP_SERVER_URL) as conn:
|
with Connection(settings.LDAP_SERVER_URL) as conn:
|
||||||
conn.search(
|
conn.search(
|
||||||
|
@ -88,7 +88,7 @@ def account_create(request):
|
||||||
queries['clippers'] = [
|
queries['clippers'] = [
|
||||||
Clipper(clipper.uid, clipper.cn)
|
Clipper(clipper.uid, clipper.cn)
|
||||||
for clipper in queries['clippers']
|
for clipper in queries['clippers']
|
||||||
if clipper.uid not in usernames
|
if str(clipper.uid) not in usernames
|
||||||
]
|
]
|
||||||
|
|
||||||
# Resulting data
|
# Resulting data
|
||||||
|
|
Loading…
Reference in a new issue