gestioCOF/kfet/autocomplete.py
Martin Pépin 5aff771d9c Set the new structure of gestioCOF
- `cof` is renamed `gestioCOF`
- `gestioncof` become `cof` (yes it looks pretty stupid but it is not)
- `bds` is created
2017-02-09 21:28:36 +01:00

82 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import *
from django.shortcuts import render
from django.http import Http404
from django.db.models import Q
from cof.models import User, Clipper
from kfet.decorators import teamkfet_required
from kfet.models import Account
@teamkfet_required
def account_create(request):
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")
data = {'q': q}
queries = {}
search_words = q.split()
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
for word in search_words:
queries['kfet'] = queries['kfet'].filter(
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(
Q(username__icontains = word)
| Q(first_name__icontains = word)
| Q(last_name__icontains = word)
)
queries['users_notcof'] = queries['users_notcof'].filter(
Q(username__icontains = word)
| Q(first_name__icontains = word)
| Q(last_name__icontains = word)
)
queries['clippers'] = queries['clippers'].filter(
Q(username__icontains = word)
| Q(fullname__icontains = word)
)
queries['kfet'] = queries['kfet'].distinct()
usernames = list( \
queries['kfet'].values_list('cofprofile__user__username', flat=True))
queries['kfet'] = [ (account, account.cofprofile.user) \
for account in queries['kfet'] ]
queries['users_cof'] = \
queries['users_cof'].exclude(username__in=usernames).distinct()
queries['users_notcof'] = \
queries['users_notcof'].exclude(username__in=usernames).distinct()
usernames += list( \
queries['users_cof'].values_list('username', flat=True))
usernames += list( \
queries['users_notcof'].values_list('username', flat=True))
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)