Commit e66b274f authored by Skia's avatar Skia
Browse files

Add index to search function

parent 5b95299b
Pipeline #556 passed with stage
in 2 minutes and 25 seconds
......@@ -613,7 +613,6 @@ class SithFile(models.Model):
self.copy_rights()
def apply_rights_recursively(self, only_folders=False):
print(self)
children = self.children.all()
if only_folders:
children = children.filter(is_folder=True)
......
......@@ -13,7 +13,3 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
def prepare(self, obj):
ret = super(UserIndex, self).prepare(obj)
print(ret)
return ret
......@@ -10,6 +10,8 @@ import os
import json
from itertools import chain
from haystack.query import SearchQuerySet
from core.models import User, Notification
from club.models import Club
......@@ -34,24 +36,8 @@ def notification(request, notif_id):
return redirect("/")
def search_user(query, as_json=False):
users = []
if query:
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()
nicks = nicks[:5]
users = users[:50]
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')))
else:
exact_nick = list(exact_nick)
nicks = list(nicks)
users = list(users)
users = exact_nick + nicks + users
return users
res = SearchQuerySet().models(User).filter(text=query).filter_or(text__contains=query)[:20]
return [r.object for r in res]
def search_club(query, as_json=False):
clubs = []
......
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