Commit da96e9da authored by Skia's avatar Skia 🤘
Browse files

Search improvement

parent f1105d70
Pipeline #123 failed with stage
in 2 minutes and 20 seconds
...@@ -18,21 +18,25 @@ def index(request, context=None): ...@@ -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
......
Supports Markdown
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