search: ascii everywhere and unformalized whitespace

This commit is contained in:
tleb 2020-04-21 13:50:43 +02:00 committed by Skia
parent 1a483bfa2c
commit 30091ef69c
4 changed files with 46 additions and 4 deletions

View File

@ -45,6 +45,9 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable):
def get_updated_field(self): def get_updated_field(self):
return "last_update" return "last_update"
def prepare_auto(self, obj):
return self.prepared_data["auto"].strip()
class IndexSignalProcessor(signals.BaseSignalProcessor): class IndexSignalProcessor(signals.BaseSignalProcessor):
def setup(self): def setup(self):

View File

@ -1,3 +1,13 @@
{{ object.first_name }} {% load replace %}
{{ object.last_name }}
{{ object.nick_name }} {% with first=object.first_name|safe|slugify last=object.last_name|safe|slugify nick=object.nick_name|default_if_none:""|safe|slugify %}
{{ first|replace:"|-| " }}
{{ last|replace:"|-| " }}
{{ nick|replace:"|-| " }}
{{ first|cut:"-" }}
{{ last|cut:"-" }}
{{ nick|cut:"-" }}
{{ first|cut:"-" }}{{ last|cut:"-" }}
{% endwith %}

View File

@ -0,0 +1,22 @@
from django.template.exceptions import TemplateSyntaxError
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
# arg should be of the form "|foo|bar" where the first character is the
# separator between old and new in value.replace(old, new)
@register.filter
@stringfilter
def replace(value, arg):
# s.replace('', '') == s so len(arg) == 2 is fine
if len(arg) < 2:
raise TemplateSyntaxError("badly formatted argument")
arg = arg.split(arg[0])
if len(arg) != 3:
raise TemplateSyntaxError("badly formatted argument")
return value.replace(arg[1], arg[2])

View File

@ -30,6 +30,7 @@ from django.contrib.auth.decorators import login_required
from django.utils import html from django.utils import html
from django.views.generic import ListView, TemplateView from django.views.generic import ListView, TemplateView
from django.conf import settings from django.conf import settings
from django.utils.text import slugify
import json import json
@ -73,7 +74,13 @@ def notification(request, notif_id):
def search_user(query, as_json=False): def search_user(query, as_json=False):
try: try:
res = SearchQuerySet().models(User).autocomplete(auto=html.escape(query))[:20] # slugify turns everything into ascii and every whitespace into -
# it ends by removing duplicate - (so '- - ' will turn into '-')
# replace('-', ' ') because search is whitespace based
query = slugify(query).replace("-", " ")
# is this necessary? it's not done when indexing users
query = html.escape(query)
res = SearchQuerySet().models(User).autocomplete(auto=query)[:20]
return [r.object for r in res] return [r.object for r in res]
except TypeError: except TypeError:
return [] return []