mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-22 22:23:23 +00:00
Search improvement
This commit is contained in:
parent
f1105d704e
commit
da96e9da84
@ -18,21 +18,25 @@ def index(request, context=None):
|
|||||||
def search(query, as_json=False):
|
def search(query, as_json=False):
|
||||||
result = {'users': None, 'clubs': None}
|
result = {'users': None, 'clubs': None}
|
||||||
if query:
|
if query:
|
||||||
nicks = User.objects.filter(nick_name__icontains=query).all()
|
exact_nick = User.objects.filter(nick_name__iexact=query).all()
|
||||||
users = User.objects.filter(Q(first_name__icontains=query) | Q(last_name__icontains=query)).all()
|
nicks = User.objects.filter(nick_name__icontains=query).exclude(id__in=exact_nick).all()
|
||||||
|
users = User.objects.filter(Q(first_name__icontains=query) |
|
||||||
|
Q(last_name__icontains=query)).exclude(id__in=exact_nick).exclude(id__in=nicks).all()
|
||||||
clubs = Club.objects.filter(name__icontains=query).all()
|
clubs = Club.objects.filter(name__icontains=query).all()
|
||||||
nicks = nicks[:5]
|
nicks = nicks[:5]
|
||||||
users = users[:5]
|
users = users[:5]
|
||||||
clubs = clubs[:5]
|
clubs = clubs[:5]
|
||||||
if as_json: # Re-loads json to avoid double encoding by JsonResponse, but still benefit from serializers
|
if as_json: # Re-loads json to avoid double encoding by JsonResponse, but still benefit from serializers
|
||||||
|
exact_nick = json.loads(serializers.serialize('json', exact_nick, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
|
||||||
nicks = json.loads(serializers.serialize('json', nicks, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
|
nicks = json.loads(serializers.serialize('json', nicks, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
|
||||||
users = json.loads(serializers.serialize('json', users, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
|
users = json.loads(serializers.serialize('json', users, fields=('nick_name', 'last_name', 'first_name', 'profile_pict')))
|
||||||
clubs = json.loads(serializers.serialize('json', clubs, fields=('name')))
|
clubs = json.loads(serializers.serialize('json', clubs, fields=('name')))
|
||||||
else:
|
else:
|
||||||
nicks = list(nicks.all())
|
exact_nick = list(exact_nick)
|
||||||
users = list(users.all())
|
nicks = list(nicks)
|
||||||
clubs = list(clubs.all())
|
users = list(users)
|
||||||
result['users'] = nicks + users
|
clubs = list(clubs)
|
||||||
|
result['users'] = exact_nick + nicks + users
|
||||||
result['clubs'] = clubs
|
result['clubs'] = clubs
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user