gestioCOF/kfet/autocomplete.py

106 lines
3.3 KiB
Python
Raw Normal View History

2016-09-01 00:45:44 +02:00
# -*- coding: utf-8 -*-
2016-08-02 10:40:46 +02:00
from django.shortcuts import render
from django.http import Http404
from django.db.models import Q
from gestioncof.models import User, Clipper
2016-09-24 19:31:50 +02:00
from kfet.decorators import teamkfet_required
from kfet.models import Account
2016-08-02 10:40:46 +02:00
2017-01-19 04:06:09 +01:00
2016-09-24 19:31:50 +02:00
@teamkfet_required
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")
if (len(q) == 0):
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-01-19 04:06:09 +01:00
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['clippers'] = Clipper.objects
2016-08-02 10:40:46 +02:00
for word in search_words:
queries['kfet'] = queries['kfet'].filter(
2017-01-19 04:06:09 +01:00
Q(cofprofile__user__username__icontains=word) |
Q(cofprofile__user__first_name__icontains=word) |
Q(cofprofile__user__last_name__icontains=word)
)
queries['users_cof'] = queries['users_cof'].filter(
2017-01-19 04:06:09 +01:00
Q(username__icontains=word) |
Q(first_name__icontains=word) |
Q(last_name__icontains=word)
)
2016-08-02 10:40:46 +02:00
queries['users_notcof'] = queries['users_notcof'].filter(
2017-01-19 04:06:09 +01:00
Q(username__icontains=word) |
Q(first_name__icontains=word) |
Q(last_name__icontains=word)
)
2017-01-19 04:06:09 +01:00
queries['clippers'] = queries['clippers'].filter(
Q(username__icontains=word) |
Q(fullname__icontains=word)
)
2016-08-02 10:40:46 +02:00
queries['kfet'] = queries['kfet'].distinct()
2017-01-19 04:06:09 +01:00
usernames = list(
queries['kfet'].values_list('cofprofile__user__username', flat=True))
2017-01-19 04:06:09 +01:00
queries['kfet'] = [(account, account.cofprofile.user)
for account in queries['kfet']]
2017-01-19 04:06:09 +01:00
queries['users_cof'] = \
queries['users_cof'].exclude(username__in=usernames).distinct()
2017-01-19 04:06:09 +01:00
queries['users_notcof'] = \
queries['users_notcof'].exclude(username__in=usernames).distinct()
2017-01-19 04:06:09 +01:00
usernames += list(
queries['users_cof'].values_list('username', flat=True))
2017-01-19 04:06:09 +01:00
usernames += list(
queries['users_notcof'].values_list('username', flat=True))
2016-08-02 10:40:46 +02:00
queries['clippers'] = \
queries['clippers'].exclude(username__in=usernames).distinct()
data.update(queries)
options = 0
for query in queries.values():
options += len(query)
data['options'] = options
return render(request, "kfet/account_create_autocomplete.html", data)
2017-01-19 15:36:40 +01:00
def account_search(request):
if "q" not in request.GET:
raise Http404
q = request.GET.get("q")
words = q.split()
data = {'q': q}
for word in words:
query = Account.objects.filter(
Q(cofprofile__user__username__icontains=word) |
Q(cofprofile__user__first_name__icontains=word) |
Q(cofprofile__user__last_name__icontains=word)
).distinct()
query = [(account.trigramme, account.cofprofile.user.get_full_name())
for account in query]
data['accounts'] = query
options = len(query)
data['options'] = options
return render(request, 'kfet/account_search_autocomplete.html', data)