Commit da96e9da authored by Skia's avatar Skia

Search improvement

parent f1105d70
Pipeline #123 failed with stage
in 2 minutes and 20 seconds
......@@ -18,21 +18,25 @@ def index(request, context=None):
def search(query, as_json=False):
result = {'users': None, 'clubs': None}
if query:
nicks = User.objects.filter(nick_name__icontains=query).all()
users = User.objects.filter(Q(first_name__icontains=query) | Q(last_name__icontains=query)).all()
exact_nick = User.objects.filter(nick_name__iexact=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()
nicks = nicks[:5]
users = users[:5]
clubs = clubs[:5]
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')))
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')))
else:
nicks = list(nicks.all())
users = list(users.all())
clubs = list(clubs.all())
result['users'] = nicks + users
exact_nick = list(exact_nick)
nicks = list(nicks)
users = list(users)
clubs = list(clubs)
result['users'] = exact_nick + nicks + users
result['clubs'] = clubs
return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment