From 20db5cfd74ab92a7240bca20946fa15f18c216e7 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Thu, 30 Sep 2021 13:46:49 +0200 Subject: [PATCH] search presets --- cookbook/forms.py | 1 + cookbook/templates/settings.html | 33 +++++++++++++++++++++++++-- cookbook/views/views.py | 38 +++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/cookbook/forms.py b/cookbook/forms.py index 4b119199f..60c9691b1 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -392,6 +392,7 @@ class SearchPreferenceForm(forms.ModelForm): prefix = 'search' trigram_threshold = forms.DecimalField(min_value=0.01, max_value=1, decimal_places=2, widget=NumberInput(attrs={'class': "form-control-range", 'type': 'range'}), help_text=_('Determines how fuzzy a search is if it uses trigram similarity matching (e.g. low values mean more typos are ignored).')) + preset = forms.CharField(widget=forms.HiddenInput(),required=False) class Meta: model = SearchPreference diff --git a/cookbook/templates/settings.html b/cookbook/templates/settings.html index 0cb63e645..eba8ad14b 100644 --- a/cookbook/templates/settings.html +++ b/cookbook/templates/settings.html @@ -163,10 +163,35 @@ @@ -178,6 +203,10 @@ $('#id_search-trigram_threshold').get(0).type = 'range'; }) + function applyPreset (preset){ + $('#id_search-preset').val(preset) + $('#search_form_button').click() + } function copyToken() { let token = $('#id_token'); diff --git a/cookbook/views/views.py b/cookbook/views/views.py index aa83d4061..a2f5669a0 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -29,7 +29,7 @@ from cookbook.forms import (CommentForm, Recipe, User, from cookbook.helper.permission_helper import group_required, share_link_valid, has_group_permission from cookbook.models import (Comment, CookLog, InviteLink, MealPlan, ViewLog, ShoppingList, Space, Keyword, RecipeImport, Unit, - Food, UserFile, ShareLink) + Food, UserFile, ShareLink, SearchPreference, SearchFields) from cookbook.tables import (CookLogTable, RecipeTable, RecipeTableSmall, ViewLogTable, InviteLinkTable) from cookbook.views.data import Object @@ -219,10 +219,12 @@ def books(request): def meal_plan(request): return render(request, 'meal_plan.html', {}) + @group_required('user') def meal_plan_new(request): return render(request, 'meal_plan_new.html', {}) + @group_required('user') def supermarket(request): return render(request, 'supermarket.html', {}) @@ -344,11 +346,11 @@ def user_settings(request): if not sp: sp = SearchPreferenceForm(user=request.user) fields_searched = ( - len(search_form.cleaned_data['icontains']) - + len(search_form.cleaned_data['istartswith']) - + len(search_form.cleaned_data['trigram']) - + len(search_form.cleaned_data['fulltext'])) - # TODO add 'recommended' option + len(search_form.cleaned_data['icontains']) + + len(search_form.cleaned_data['istartswith']) + + len(search_form.cleaned_data['trigram']) + + len(search_form.cleaned_data['fulltext']) + ) if fields_searched == 0: search_form.add_error(None, _('You must select at least one field to search!')) search_error = True @@ -368,6 +370,26 @@ def user_settings(request): sp.fulltext.set(search_form.cleaned_data['fulltext']) sp.trigram_threshold = search_form.cleaned_data['trigram_threshold'] + if search_form.cleaned_data['preset'] == 'fuzzy': + sp.search = SearchPreference.SIMPLE + sp.lookup = True + sp.unaccent.set([SearchFields.objects.get(name='Name')]) + sp.icontains.set([SearchFields.objects.get(name='Name')]) + sp.istartswith.clear() + sp.trigram.set([SearchFields.objects.get(name='Name')]) + sp.fulltext.clear() + sp.trigram_threshold = 0.1 + + if search_form.cleaned_data['preset'] == 'precise': + sp.search = SearchPreference.WEB + sp.lookup = True + sp.unaccent.set(SearchFields.objects.all()) + sp.icontains.clear() + sp.istartswith.set([SearchFields.objects.get(name='Name')]) + sp.trigram.clear() + sp.fulltext.set(SearchFields.objects.all()) + sp.trigram_threshold = 0.1 + sp.save() if up: preference_form = UserPreferenceForm(instance=up) @@ -417,8 +439,8 @@ def history(request): @group_required('admin') def system(request): postgres = False if ( - settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2' # noqa: E501 - or settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql' # noqa: E501 + settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2' # noqa: E501 + or settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql' # noqa: E501 ) else True secret_key = False if os.getenv('SECRET_KEY') else True