Optimize code, better queries

- Remove useless Q queries
- Optimize with sets
- Queries over clipper id too
This commit is contained in:
Martin Pépin 2017-02-11 02:31:55 +01:00
parent d4b696db70
commit 02a8e74e3b
2 changed files with 16 additions and 15 deletions

View file

@ -31,8 +31,8 @@ def autocomplete(request):
bits = q.split()
# Fetching data from User and CofProfile tables
queries['members'] = CofProfile.objects.filter(Q(is_cof=True))
queries['users'] = User.objects.filter(Q(profile__is_cof=False))
queries['members'] = CofProfile.objects.filter(is_cof=True)
queries['users'] = User.objects.filter(profile__is_cof=False)
for bit in bits:
queries['members'] = queries['members'].filter(
Q(user__first_name__icontains=bit)
@ -48,16 +48,17 @@ def autocomplete(request):
# Clearing redundancies
usernames = (
list(queries['members'].values_list('login_clipper', flat='True'))
+ list(queries['users'].values_list('profile__login_clipper',
flat='True'))
set(queries['members'].values_list('login_clipper', flat='True'))
| set(queries['users'].values_list('profile__login_clipper',
flat='True'))
)
# Fetching data from the SPI
if hasattr(settings, 'LDAP_SERVER_URL'):
# Fetching
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:
conn.search(
@ -69,11 +70,11 @@ def autocomplete(request):
queries['clippers'] = [
Clipper(clipper.uid, clipper.cn)
for clipper in queries['clippers']
if clipper.uid not in usernames
if str(clipper.uid) not in usernames
]
# Resulting data
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)

View file

@ -34,8 +34,8 @@ def account_create(request):
# Fetching data from User, CofProfile and Account tables
queries['kfet'] = Account.objects
queries['users_cof'] = User.objects.filter(Q(profile__is_cof = True))
queries['users_notcof'] = User.objects.filter(Q(profile__is_cof = False))
queries['users_cof'] = User.objects.filter(profile__is_cof = True)
queries['users_notcof'] = User.objects.filter(profile__is_cof = False)
for word in search_words:
queries['kfet'] = queries['kfet'].filter(
@ -56,7 +56,7 @@ def account_create(request):
# Clearing redundancies
queries['kfet'] = queries['kfet'].distinct()
usernames = list(
usernames = set(
queries['kfet'].values_list('cofprofile__user__username', flat=True))
queries['kfet'] = [
(account, account.cofprofile.user)
@ -67,16 +67,16 @@ def account_create(request):
queries['users_cof'].exclude(username__in=usernames).distinct()
queries['users_notcof'] = \
queries['users_notcof'].exclude(username__in=usernames).distinct()
usernames += list(
usernames |= set(
queries['users_cof'].values_list('username', flat=True))
usernames += list(
usernames |= set(
queries['users_notcof'].values_list('username', flat=True))
# Fetching data from the SPI
if hasattr(settings, 'LDAP_SERVER_URL'):
# Fetching
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:
conn.search(
@ -88,7 +88,7 @@ def account_create(request):
queries['clippers'] = [
Clipper(clipper.uid, clipper.cn)
for clipper in queries['clippers']
if clipper.uid not in usernames
if str(clipper.uid) not in usernames
]
# Resulting data