From 0a5559b4dfaa95483993be831869548169eb2910 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 24 Nov 2018 11:45:42 +0100 Subject: [PATCH] Make search unaccented --- common/settings.py | 1 + recette/forms.py | 6 +++++- recette/migrations/0005_postgres_unaccent.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 recette/migrations/0005_postgres_unaccent.py diff --git a/common/settings.py b/common/settings.py index eab0012..2a3aaac 100644 --- a/common/settings.py +++ b/common/settings.py @@ -16,6 +16,7 @@ INSTALLED_APPS = [ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', + 'django.contrib.postgres', 'django.contrib.staticfiles', 'easy_thumbnails', 'recette', diff --git a/recette/forms.py b/recette/forms.py index d24345d..275050e 100644 --- a/recette/forms.py +++ b/recette/forms.py @@ -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 diff --git a/recette/migrations/0005_postgres_unaccent.py b/recette/migrations/0005_postgres_unaccent.py new file mode 100644 index 0000000..eabd720 --- /dev/null +++ b/recette/migrations/0005_postgres_unaccent.py @@ -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()]