Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Make the ingredient search work with sqlite as well

Roland Geiderfeb98f7e58ad
diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.py
index 1fa020653..597b0bdfe 100644
--- a/wger/nutrition/api/views.py
+++ b/wger/nutrition/api/views.py
@@ -73,6 +73,7 @@ from wger.nutrition.models import (
WeightUnit,
)
from wger.utils.constants import ENGLISH_SHORT_NAME
+from wger.utils.db import is_postgres_db
from wger.utils.language import load_language
from wger.utils.viewsets import WgerOwnerObjectModelViewSet
@@ -215,13 +216,18 @@ def search(request):
return Response(json_response)
languages = [load_language(l) for l in language_codes.split(',')]
- ingredients = Ingredient.objects.filter(
- search_column=term,
+ query = Ingredient.objects.filter(
language__in=languages,
status=Ingredient.STATUS_ACCEPTED,
- ).only('name')[:100]
+ ).only('name')
- for ingredient in ingredients:
+ # Postgres uses a full-text search
+ if is_postgres_db():
+ query = query.filter(search_column=term)
+ else:
+ query = query.filter(name__icontains=term)
+
+ for ingredient in query[:100]:
if hasattr(ingredient, 'image'):
image_obj = ingredient.image
image = image_obj.image.url
diff --git a/wger/utils/db.py b/wger/utils/db.py
new file mode 100644
index 000000000..2d15b5c74
--- /dev/null
+++ b/wger/utils/db.py
@@ -0,0 +1,20 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Django
+from django.conf import settings
+
+
+def is_postgres_db():
+ return 'postgres' in settings.DATABASES['default']['ENGINE']