Make search unaccented

This commit is contained in:
Claude Paroz 2018-11-24 11:45:42 +01:00
parent 71c5de49d3
commit 0a5559b4df
3 changed files with 17 additions and 1 deletions

View file

@ -16,6 +16,7 @@ INSTALLED_APPS = [
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.postgres',
'django.contrib.staticfiles',
'easy_thumbnails',
'recette',

View file

@ -2,6 +2,8 @@ from django import forms
from .models import Recette
STOP_WORDS = ['et', 'de', 'des', 'a', 'aux']
class SearchForm(forms.Form):
text = forms.CharField(
@ -17,7 +19,9 @@ class SearchForm(forms.Form):
if self.cleaned_data['veget']:
qs = qs.filter(is_veget=True)
if self.cleaned_data['text']:
qs = qs.filter(nom__icontains=self.cleaned_data['text'])
terms = [t for t in self.cleaned_data['text'].split() if t not in STOP_WORDS]
for term in terms:
qs = qs.filter(nom__unaccent__icontains=term)
if self.cleaned_data['saison'] != 'all':
qs = qs.filter(saison__contains=[self.cleaned_data['saison']])
return qs

View file

@ -0,0 +1,11 @@
from django.contrib.postgres.operations import UnaccentExtension
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('recette', '0004_recette_photo_instr'),
]
operations = [UnaccentExtension()]