2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
2016-05-26 22:44:10 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
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
|
2017-02-09 21:04:32 +01:00
|
|
|
from .models import CofProfile, Clipper
|
|
|
|
from .decorators import buro_required
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-09-24 19:40:16 +02:00
|
|
|
@buro_required
|
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))
|
2017-02-11 18:41:47 +01:00
|
|
|
queries['users'] = User.objects.filter(Q(profile__cof__is_cof=False))
|
2016-05-21 23:57:36 +02:00
|
|
|
queries['clippers'] = Clipper.objects
|
|
|
|
for bit in bits:
|
|
|
|
queries['members'] = queries['members'].filter(
|
2017-02-11 18:41:47 +01:00
|
|
|
Q(profile__user__first_name__icontains=bit)
|
|
|
|
| Q(profile__user__last_name__icontains=bit)
|
|
|
|
| Q(profile__user__username__icontains=bit)
|
|
|
|
| Q(profile__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()
|
2017-02-11 18:41:47 +01:00
|
|
|
usernames = list(queries['members'].values_list('profile__login_clipper',
|
2016-07-09 22:31:56 +02:00
|
|
|
flat='True')) \
|
|
|
|
+ list(queries['users'].values_list('profile__login_clipper',
|
|
|
|
flat='True'))
|
2016-07-09 23:59:25 +02:00
|
|
|
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)
|