forked from DGNum/gestioCOF
21b8b6042f
Rend GestioCOF compatible avec python 3. En particulier, il s'agit de : - Utiliser la version "fonction" de `print` dans `sync_clipper` et `tirage_bda`, avec le `from __future__ import print_function` pour garder la compatibilité avec python 2 - Utiliser de l'unicode par défaut, même en python 2, avec `from __future__ import unicode_literals` et le décorateur de compatibilité `python_2_unicode_compatible` de Django pour les modèles, comme décrit à https://docs.djangoproject.com/en/1.9/topics/python3/#str-and-unicode-methods - Utiliser `six.text_type` à la place de `unicode` Fixes #2.
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
from __future__ import unicode_literals
|
|
|
|
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
|
|
|
|
|
|
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)
|
|
| Q(user__last_name__icontains=bit)
|
|
| Q(user__username__icontains=bit)
|
|
| Q(login_clipper__icontains=bit))
|
|
queries['users'] = queries['users'].filter(
|
|
Q(first_name__icontains=bit)
|
|
| Q(last_name__icontains=bit)
|
|
| Q(username__icontains=bit))
|
|
queries['clippers'] = queries['clippers'].filter(
|
|
Q(fullname__icontains=bit)
|
|
| Q(username__icontains=bit))
|
|
queries['members'] = queries['members'].distinct()
|
|
queries['users'] = queries['users'].distinct()
|
|
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()
|
|
# add clippers
|
|
|
|
data.update(queries)
|
|
|
|
options = 0
|
|
for query in queries.values():
|
|
options += len(query)
|
|
data['options'] = options
|
|
|
|
return shortcuts.render(request, "autocomplete_user.html", data)
|