2016-05-21 23:57:36 +02:00
|
|
|
from django import shortcuts
|
|
|
|
from django.http import Http404
|
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from gestioncof.models import CofProfile, Clipper
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-21 23:57:36 +02:00
|
|
|
def autocomplete(request):
|
|
|
|
if "q" not in request.GET:
|
|
|
|
raise Http404
|
|
|
|
q = request.GET['q']
|
|
|
|
data = {
|
|
|
|
'q': q,
|
|
|
|
}
|
|
|
|
|
|
|
|
queries = {}
|
|
|
|
bits = q.split()
|
|
|
|
|
|
|
|
queries['members'] = CofProfile.objects.filter(Q(is_cof=True))
|
|
|
|
queries['users'] = User.objects.filter(Q(profile__is_cof=False))
|
|
|
|
queries['clippers'] = Clipper.objects
|
|
|
|
for bit in bits:
|
|
|
|
queries['members'] = queries['members'].filter(
|
|
|
|
Q(user__first_name__icontains=bit)
|
2016-07-09 21:19:37 +02:00
|
|
|
| Q(user__last_name__icontains=bit)
|
|
|
|
| Q(user__username__icontains=bit)
|
|
|
|
| Q(login_clipper__icontains=bit))
|
2016-05-21 23:57:36 +02:00
|
|
|
queries['users'] = queries['users'].filter(
|
|
|
|
Q(first_name__icontains=bit)
|
2016-07-09 21:19:37 +02:00
|
|
|
| Q(last_name__icontains=bit)
|
|
|
|
| Q(username__icontains=bit))
|
2016-05-21 23:57:36 +02:00
|
|
|
queries['clippers'] = queries['clippers'].filter(
|
|
|
|
Q(fullname__icontains=bit)
|
2016-07-09 21:19:37 +02:00
|
|
|
| Q(username__icontains=bit))
|
2016-05-21 23:57:36 +02:00
|
|
|
queries['members'] = queries['members'].distinct()
|
|
|
|
queries['users'] = queries['users'].distinct()
|
2016-07-09 21:19:37 +02:00
|
|
|
usernames = list(queries['members'].values_list('login_clipper', flat='True')) \
|
|
|
|
+ list(queries['users'].values_list('profile__login_clipper', flat='True'))
|
|
|
|
queries['clippers'] = queries['clippers'].exclude(username__in=usernames).distinct()
|
2016-05-21 23:57:36 +02:00
|
|
|
# add clippers
|
|
|
|
|
|
|
|
data.update(queries)
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-21 23:57:36 +02:00
|
|
|
options = 0
|
|
|
|
for query in queries.values():
|
|
|
|
options += len(query)
|
|
|
|
data['options'] = options
|
|
|
|
|
|
|
|
return shortcuts.render(request, "autocomplete_user.html", data)
|