Self hosted FLOSS fitness/workout, nutrition and weight tracker
Improvements to the full text search for ingredients
Roland Geider4b9e5f15f8ce
diff --git a/wger/core/fixtures/languages.json b/wger/core/fixtures/languages.jsonindex adc02b595..ad0dd95d0 100644--- a/wger/core/fixtures/languages.json+++ b/wger/core/fixtures/languages.json@@ -4,7 +4,8 @@"pk": 1,"fields": {"short_name": "de",- "full_name": "Deutsch"+ "full_name": "Deutsch",+ "full_name_en": "German"}},{@@ -12,7 +13,8 @@"pk": 2,"fields": {"short_name": "en",- "full_name": "English"+ "full_name": "English",+ "full_name_en": "English"}},{@@ -20,7 +22,8 @@"pk": 3,"fields": {"short_name": "bg",- "full_name": "Български език"+ "full_name": "Български език",+ "full_name_en": "Bulgarian"}},{@@ -28,7 +31,8 @@"pk": 4,"fields": {"short_name": "es",- "full_name": "Español"+ "full_name": "Español",+ "full_name_en": "Spanish"}},{@@ -36,7 +40,8 @@"pk": 5,"fields": {"short_name": "ru",- "full_name": "Русский"+ "full_name": "Русский",+ "full_name_en": "Russian"}},{@@ -44,7 +49,8 @@"pk": 6,"fields": {"short_name": "nl",- "full_name": "Nederlands"+ "full_name": "Nederlands",+ "full_name_en": "Dutch"}},{@@ -52,7 +58,8 @@"pk": 7,"fields": {"short_name": "pt",- "full_name": "Português"+ "full_name": "Português",+ "full_name_en": "Portuguese"}},{@@ -60,7 +67,8 @@"pk": 8,"fields": {"short_name": "el",- "full_name": "Ελληνικά"+ "full_name": "Ελληνικά",+ "full_name_en": "Greek"}},{@@ -68,7 +76,8 @@"pk": 9,"fields": {"short_name": "cs",- "full_name": "Čeština"+ "full_name": "Čeština",+ "full_name_en": "Czech"}},{@@ -76,7 +85,8 @@"pk": 10,"fields": {"short_name": "sv",- "full_name": "Svenska"+ "full_name": "Svenska",+ "full_name_en": "Swedish"}},{@@ -84,7 +94,8 @@"pk": 11,"fields": {"short_name": "no",- "full_name": "Norsk"+ "full_name": "Norsk",+ "full_name_en": "Norwegian"}},{@@ -92,7 +103,8 @@"pk": 12,"fields": {"short_name": "fr",- "full_name": "Français"+ "full_name": "Français",+ "full_name_en": "French"}},{@@ -100,7 +112,8 @@"pk": 13,"fields": {"short_name": "it",- "full_name": "Italian"+ "full_name": "Italiano",+ "full_name_en": "Italian"}},{@@ -108,7 +121,8 @@"pk": 14,"fields": {"short_name": "pl",- "full_name": "Polish"+ "full_name": "Polski",+ "full_name_en": "Polish"}},{@@ -116,7 +130,8 @@"pk": 15,"fields": {"short_name": "uk",- "full_name": "Українська мова"+ "full_name": "Українська мова",+ "full_name_en": "Ukrainian"}},{@@ -124,7 +139,8 @@"pk": 16,"fields": {"short_name": "tr",- "full_name": "Türkçe"+ "full_name": "Türkçe",+ "full_name_en": "Turkish"}},{@@ -132,7 +148,8 @@"pk": 17,"fields": {"short_name": "ar",- "full_name": "اَللُّغَةُ اَلْعَرَبِيَّة"+ "full_name": "اَللُّغَةُ اَلْعَرَبِيَّة",+ "full_name_en": "Arabic"}},{@@ -140,7 +157,8 @@"pk": 18,"fields": {"short_name": "az",- "full_name": "Azərbaycan dili"+ "full_name": "Azərbaycan dili",+ "full_name_en": "Azerbaijani"}},{@@ -148,7 +166,8 @@"pk": 19,"fields": {"short_name": "eo",- "full_name": "Esperanto"+ "full_name": "Esperanto",+ "full_name_en": "Esperanto"}},{@@ -156,7 +175,8 @@"pk": 20,"fields": {"short_name": "fa",- "full_name": "زبان فارسی"+ "full_name": "زبان فارسی",+ "full_name_en": "Persian"}},{@@ -164,7 +184,8 @@"pk": 21,"fields": {"short_name": "he",- "full_name": "עברית"+ "full_name": "עברית",+ "full_name_en": "Hebrew"}},{@@ -172,7 +193,8 @@"pk": 22,"fields": {"short_name": "hr",- "full_name": "Hrvatski jezik"+ "full_name": "Hrvatski jezik",+ "full_name_en": "Croatian"}},{@@ -180,7 +202,8 @@"pk": 23,"fields": {"short_name": "id",- "full_name": "Bahasa Indonesia"+ "full_name": "Bahasa Indonesia",+ "full_name_en": "Indonesian"}},{@@ -188,7 +211,8 @@"pk": 24,"fields": {"short_name": "zh",- "full_name": "漢語"+ "full_name": "漢語",+ "full_name_en": "Chinese"}}]diff --git a/wger/core/migrations/0017_language_full_name_en.py b/wger/core/migrations/0017_language_full_name_en.pynew file mode 100644index 000000000..d5ed2f137--- /dev/null+++ b/wger/core/migrations/0017_language_full_name_en.py@@ -0,0 +1,36 @@+# Generated by Django 4.2.6 on 2024-01-17 15:41++from django.db import migrations, models+++def update_language_full_name(apps, schema_editor):+ mapper = {1: 'German', 2: 'English', 3: 'Bulgarian', 4: 'Spanish', 5: 'Russian', 6: 'Dutch',+ 7: 'Portuguese', 8: 'Greek', 9: 'Czech', 10: 'Swedish', 11: 'Norwegian', 12: 'French',+ 13: 'Italian', 14: 'Polish', 15: 'Ukrainian', 16: 'Turkish', 17: 'Arabic',+ 18: 'Azerbaijani', 19: 'Esperanto', 20: 'Persian', 21: 'Hebrew', 22: 'Croatian',+ 23: 'Indonesian', 24: 'Chinese', }++ Language = apps.get_model("core", "Language")+ for language in Language.objects.all():+ language.full_name_en = mapper.get(language.id, 'English')+ language.save()+++class Migration(migrations.Migration):+ dependencies = [+ ('core', '0016_alter_language_short_name'),+ ]++ operations = [+ migrations.AddField(+ model_name='language',+ name='full_name_en',+ field=models.CharField(default='english', max_length=30,+ verbose_name='Language full name in English', ),+ preserve_default=False,+ ),+ migrations.RunPython(+ update_language_full_name,+ reverse_code=migrations.RunPython.noop,+ ),+ ]diff --git a/wger/core/models/language.py b/wger/core/models/language.pyindex a7c63da95..5c7d09da3 100644--- a/wger/core/models/language.py+++ b/wger/core/models/language.py@@ -39,6 +39,12 @@ class Language(models.Model):verbose_name=_('Language full name'),)+ # e.g. 'German'+ full_name_en = models.CharField(+ max_length=30,+ verbose_name=_('Language full name in English'),+ )+class Meta:"""Set Meta optionsdiff --git a/wger/core/templates/language/overview.html b/wger/core/templates/language/overview.htmlindex 315b77519..8ba52b0d2 100644--- a/wger/core/templates/language/overview.html+++ b/wger/core/templates/language/overview.html@@ -7,13 +7,15 @@{% block content %}<div class="list-group">{% for language in language_list %}- <a href="{% url 'core:language:view' language.id %}" class="list-group-item list-group-item-action">+ <a href="{% url 'core:language:view' language.id %}"+ class="list-group-item list-group-item-action"><span class="glyphicon glyphicon-chevron-right float-end"></span><img src="{% static language.static_path %}"width="18"alt="{% translate language.full_name %}"title="{% translate language.full_name %}">{{ language.full_name }}+ <small>({{ language.full_name_en }})</small></a>{% empty %}<a href="{% url 'core:language:add' %}" class="list-group-item">diff --git a/wger/core/templates/language/view.html b/wger/core/templates/language/view.htmlindex 4e33f0000..1c3a6af60 100644--- a/wger/core/templates/language/view.html+++ b/wger/core/templates/language/view.html@@ -20,12 +20,14 @@<tr><th>{% translate "Language short name" %}</th><th>{% translate "Language full name" %}</th>+ <th>{% translate "Language full name in English" %}</th></tr></thead><tbody><tr><td>{{ view_language.short_name }}</td><td>{{ view_language.full_name }}</td>+ <td>{{ view_language.full_name_en }}</td></tr></tbody></table>@@ -52,18 +54,21 @@{% block options %}<div class="btn-group"><div class="btn-group">- <button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-bs-toggle="dropdown"+ <button type="button" class="btn btn-primary btn-sm dropdown-toggle"+ data-bs-toggle="dropdown"aria-haspopup="true" aria-expanded="false"><span class="{% fa_class 'cog' %}"></span>{% translate "Options" %}</button><div class="dropdown-menu">- <a href="{% url 'core:language:edit' view_language.id %}" class="wger-modal-dialog dropdown-item">+ <a href="{% url 'core:language:edit' view_language.id %}"+ class="wger-modal-dialog dropdown-item"><span class="{% fa_class 'edit' %}"></span>{% translate "Edit" %}</a><div role="separator" class="dropdown-divider"></div>- <a href="{% url 'core:language:delete' view_language.id %}" class="wger-modal-dialog dropdown-item">+ <a href="{% url 'core:language:delete' view_language.id %}"+ class="wger-modal-dialog dropdown-item"><span class="{% fa_class 'trash' %}"></span>{% translate "Delete" %}</a>diff --git a/wger/core/views/languages.py b/wger/core/views/languages.pyindex 28636df88..c54016f8b 100644--- a/wger/core/views/languages.py+++ b/wger/core/views/languages.py@@ -42,7 +42,6 @@ from wger.utils.generic_views import (WgerFormMixin,)-logger = logging.getLogger(__name__)@@ -69,7 +68,7 @@ class LanguageCreateView(WgerFormMixin, LoginRequiredMixin, PermissionRequiredMi"""model = Language- fields = ['short_name', 'full_name']+ fields = ['short_name', 'full_name', 'full_name_en']title = gettext_lazy('Add')permission_required = 'core.add_language'@@ -99,7 +98,7 @@ class LanguageEditView(WgerFormMixin, LoginRequiredMixin, PermissionRequiredMixi"""model = Language- fields = ['short_name', 'full_name']+ fields = ['short_name', 'full_name', 'full_name_en']permission_required = 'core.change_language'def get_context_data(self, **kwargs):diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.pyindex 15c8dbe2c..1fa020653 100644--- a/wger/nutrition/api/views.py+++ b/wger/nutrition/api/views.py@@ -17,14 +17,11 @@# Standard Libraryimport logging-from dataclasses import asdict-import datetime# Djangofrom django.conf import settingsfrom django.utils.decorators import method_decoratorfrom django.views.decorators.cache import cache_page-from django.shortcuts import get_object_or_404# Third Partyfrom drf_spectacular.types import OpenApiTypes@@ -79,7 +76,6 @@ from wger.utils.constants import ENGLISH_SHORT_NAMEfrom wger.utils.language import load_languagefrom wger.utils.viewsets import WgerOwnerObjectModelViewSet-logger = logging.getLogger(__name__)@@ -183,23 +179,22 @@ class IngredientInfoViewSet(IngredientViewSet):),],responses={- 200:- inline_serializer(+ 200: inline_serializer(name='IngredientSearchResponse',fields={'value':- CharField(),+ CharField(),'data':- inline_serializer(- name='IngredientSearchItemResponse',- fields={- 'id': IntegerField(),- 'name': CharField(),- 'category': CharField(),- 'image': CharField(),- 'image_thumbnail': CharField()- }- )+ inline_serializer(+ name='IngredientSearchItemResponse',+ fields={+ 'id': IntegerField(),+ 'name': CharField(),+ 'category': CharField(),+ 'image': CharField(),+ 'image_thumbnail': CharField()+ }+ )})},@@ -224,7 +219,7 @@ def search(request):search_column=term,language__in=languages,status=Ingredient.STATUS_ACCEPTED,- )[:100]+ ).only('name')[:100]for ingredient in ingredients:if hasattr(ingredient, 'image'):diff --git a/wger/nutrition/migrations/0012_auto_20221207_2031.py b/wger/nutrition/migrations/0012_auto_20221207_2031.pydeleted file mode 100644index 80776acc3..000000000--- a/wger/nutrition/migrations/0012_auto_20221207_2031.py+++ /dev/null@@ -1,42 +0,0 @@-# Generated by Django 3.2.15 on 2022-12-07 19:31--import django.contrib.postgres.indexes-import django.contrib.postgres.search-from django.db import migrations---class Migration(migrations.Migration):- dependencies = [- ('nutrition', '0011_alter_logitem_datetime'),- ]-- operations = [- migrations.AddField(- model_name='ingredient',- name='search_column',- field=django.contrib.postgres.search.SearchVectorField(null=True),- ),- migrations.AddIndex(- model_name='ingredient',- index=django.contrib.postgres.indexes.GinIndex(- fields=['search_column'], name='nutrition_i_search__f274b7_gin'- ),- ),- migrations.RunSQL(- sql='''- CREATE TRIGGER ingredient_search_column_trigger- BEFORE INSERT OR UPDATE OF name, search_column- ON nutrition_ingredient- FOR EACH ROW EXECUTE PROCEDURE- tsvector_update_trigger(- search_column, 'pg_catalog.english', name- );-- UPDATE nutrition_ingredient SET search_column = NULL;- ''',- reverse_sql='''- DROP TRIGGER IF EXISTS ingredient_search_column_trigger- ON film;- '''- ),- ]diff --git a/wger/nutrition/migrations/0020_full_text_search.py b/wger/nutrition/migrations/0020_full_text_search.pynew file mode 100644index 000000000..6e0b1bf84--- /dev/null+++ b/wger/nutrition/migrations/0020_full_text_search.py@@ -0,0 +1,89 @@+# Generated by Django 3.2.15 on 2022-12-07 19:31++import django.contrib.postgres.indexes+import django.contrib.postgres.search+from django.db import migrations, models+++def forwards(apps, schema_editor):+ if not schema_editor.connection.vendor.startswith('postgres'):+ return++ schema_editor.execute('''+ CREATE OR REPLACE FUNCTION update_ingredient_index() RETURNS trigger AS $$+ DECLARE+ known_values TEXT[];+ BEGIN+ SELECT ARRAY(SELECT cfgname FROM pg_ts_config) INTO known_values;++ -- Map the ingredient language to the list of known languages+ IF NEW.language_id IS NOT NULL THEN+ SELECT lower(full_name_en) INTO NEW.index_language+ FROM core_language+ WHERE id = NEW.language_id;++ -- If the language is not known values, just use English+ IF NEW.index_language IS NULL OR NEW.index_language NOT IN (SELECT unnest(known_values)) THEN+ NEW.index_language = 'english';+ END IF;+ ELSE+ -- If language_id is NULL, just set to English+ NEW.index_language = 'english';+ END IF;++ new.search_column :=+ setweight(to_tsvector(new.index_language::regconfig, coalesce(new.name,'')), 'A') ||+ setweight(to_tsvector(new.index_language::regconfig, coalesce(new.brand,'')), 'B')||+ setweight(to_tsvector(new.index_language::regconfig, coalesce(new.common_name,'')), 'C') ;++ RETURN new;+ END+ $$ LANGUAGE plpgsql;++ -- Create the trigger+ CREATE OR REPLACE TRIGGER ingredient_search_column_trigger+ BEFORE INSERT OR UPDATE+ ON nutrition_ingredient+ FOR EACH ROW EXECUTE FUNCTION+ update_ingredient_index();++ -- Force an update of the search index+ UPDATE nutrition_ingredient SET name = name WHERE true;+ ''')+++def backwards(apps, schema_editor):+ if not schema_editor.connection.vendor.startswith('postgres'):+ return++ schema_editor.execute('''+ DROP TRIGGER IF EXISTS ingredient_search_column_trigger ON nutrition_ingredient;+ DROP FUNCTION IF EXISTS update_ingredient_index();+ ''')+++class Migration(migrations.Migration):+ dependencies = [+ ('nutrition', '0019_alter_image_license_author_and_more'),+ ]++ operations = [+ migrations.AddField(+ model_name='ingredient',+ name='index_language',+ field=models.CharField(default='english', editable=False, max_length=30),+ ),+ migrations.AddField(+ model_name='ingredient',+ name='search_column',+ field=django.contrib.postgres.search.SearchVectorField(null=True),+ ),+ migrations.AddIndex(+ model_name='ingredient',+ index=django.contrib.postgres.indexes.GinIndex(+ fields=['search_column'],+ name='nutrition_i_search__f274b7_gin'+ ),+ ),+ migrations.RunPython(forwards, backwards)+ ]diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 8331e8450..990d52a9d 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -239,6 +239,13 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):blank=True,)+ index_language = models.CharField(+ default='english',+ editable=False,+ max_length=30,+ )+ """The language used for the full text search"""+search_column = SearchVectorField(null=True)"""Column used for full text search"""