Self hosted FLOSS fitness/workout, nutrition and weight tracker
Allow deactivating the language filter when searching for ingredients and exercises
Roland Geiderc8be37a9949d
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 30bc66616..b139429ab 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -87,6 +87,7 @@ from wger.utils.constants import (HTML_ATTRIBUTES_WHITELIST,HTML_STYLES_WHITELIST,HTML_TAG_WHITELIST,+ SEARCH_ALL_LANGUAGES,)from wger.utils.db import is_postgres_dbfrom wger.utils.language import load_language@@ -346,8 +347,14 @@ def search(request):if not q:return Response(response)+ # Filter the appropriate languageslanguages = [load_language(l) for l in language_codes.split(',')]- query = Exercise.objects.filter(language__in=languages).only('name')+ if language_codes == SEARCH_ALL_LANGUAGES:+ query = Exercise.objects.all()+ else:+ query = Exercise.objects.filter(language__in=languages)++ query = query.only('name')# Postgres uses a full-text searchif is_postgres_db():diff --git a/wger/exercises/tests/test_search_api.py b/wger/exercises/tests/test_search_api.pyindex 2849cbdff..c8b4b773d 100644--- a/wger/exercises/tests/test_search_api.py+++ b/wger/exercises/tests/test_search_api.py@@ -94,3 +94,12 @@ class SearchExerciseApiTestCase(BaseTestCase, ApiBaseTestCase):self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(len(response.data['suggestions']), 4)++ def test_search_all_languages(self):+ """+ Passing different language codes works correctly+ """+ response = self.client.get(self.url + '?term=demo&language=*')++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 4)diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.pyindex 7b0ac078d..546326b8a 100644--- a/wger/nutrition/api/views.py+++ b/wger/nutrition/api/views.py@@ -73,7 +73,10 @@ from wger.nutrition.models import (NutritionPlan,WeightUnit,)-from wger.utils.constants import ENGLISH_SHORT_NAME+from wger.utils.constants import (+ ENGLISH_SHORT_NAME,+ SEARCH_ALL_LANGUAGES,+)from wger.utils.db import is_postgres_dbfrom wger.utils.language import load_languagefrom wger.utils.viewsets import WgerOwnerObjectModelViewSet@@ -211,16 +214,23 @@ def search(request):term = request.GET.get('term', None)language_codes = request.GET.get('language', ENGLISH_SHORT_NAME)results = []- json_response = {}+ response = {}if not term:- return Response(json_response)+ return Response(response)- languages = [load_language(l) for l in language_codes.split(',')]query = Ingredient.objects.filter(- language__in=languages,status=Ingredient.STATUS_ACCEPTED,- ).only('name')+ )++ # Filter the appropriate languages+ languages = [load_language(l) for l in language_codes.split(',')]+ if language_codes != SEARCH_ALL_LANGUAGES:+ query = query.filter(+ language__in=languages,+ )++ query = query.only('name')# Postgres uses a full-text searchif is_postgres_db():@@ -253,9 +263,9 @@ def search(request):},}results.append(ingredient_json)- json_response['suggestions'] = results+ response['suggestions'] = results- return Response(json_response)+ return Response(response)class ImageViewSet(viewsets.ReadOnlyModelViewSet):diff --git a/wger/nutrition/fixtures/test-ingredients.json b/wger/nutrition/fixtures/test-ingredients.jsonindex d4152ebef..ce3a46a98 100644--- a/wger/nutrition/fixtures/test-ingredients.json+++ b/wger/nutrition/fixtures/test-ingredients.json@@ -143,8 +143,8 @@"license_author": "wger test","last_update": "2013-02-12T01:00:00+01:00","created": "2013-04-05T01:00:00+01:00",- "name": "Raw ingredient",- "language": 2,+ "name": "Testzutat 123",+ "language": 1,"sodium": "2.1","energy": 180,"fat": "2",@@ -333,7 +333,7 @@"last_update": "2013-02-12T01:00:00+01:00","created": "2013-04-05T01:00:00+01:00","name": "Needed for guest user",- "language": 2,+ "language": 3,"sodium": "2.1","energy": 180,"fat": "2",@@ -360,7 +360,7 @@"last_update": "2013-02-12T01:00:00+01:00","created": "2013-04-05T01:00:00+01:00","name": "Needed for guest user",- "language": 2,+ "language": 3,"sodium": "2.1","energy": 180,"fat": "2",diff --git a/wger/nutrition/tests/test_ingredient_overview.py b/wger/nutrition/tests/test_ingredient_overview.pyindex 449144936..8a68d690a 100644--- a/wger/nutrition/tests/test_ingredient_overview.py+++ b/wger/nutrition/tests/test_ingredient_overview.py@@ -56,7 +56,7 @@ class IngredientOverviewTestCase(WgerTestCase):self.assertEqual(response.status_code, 200)self.assertEqual(len(response.context['ingredients_list']), PAGINATION_OBJECTS_PER_PAGE)- rest_ingredients = 13+ rest_ingredients = 10response = self.client.get(reverse('nutrition:ingredient:list'), {'page': 3})self.assertEqual(response.status_code, 200)self.assertEqual(len(response.context['ingredients_list']), rest_ingredients)diff --git a/wger/nutrition/tests/test_search_api.py b/wger/nutrition/tests/test_search_api.pynew file mode 100644index 000000000..a895d4041--- /dev/null+++ b/wger/nutrition/tests/test_search_api.py@@ -0,0 +1,114 @@+# 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+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.++# Third Party+from rest_framework import status++# wger+from wger.core.tests.api_base_test import ApiBaseTestCase+from wger.core.tests.base_testcase import BaseTestCase+++class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):+ url = '/api/v2/ingredient/search/'++ def setUp(self):+ super().setUp()+ self.init_media_root()++ def test_basic_search_logged_out(self):+ """+ Logged-out users are also allowed to use the search+ """+ response = self.client.get(self.url + '?term=test')+ result1 = response.data['suggestions'][0]++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 2)+ self.assertEqual(result1['value'], 'Ingredient, test, 2, organic, raw')+ self.assertEqual(result1['data']['id'], 2)++ def test_basic_search_logged_in(self):+ """+ Logged-in users get the same results+ """+ self.authenticate('test')+ response = self.client.get(self.url + '?term=test')+ result1 = response.data['suggestions'][0]++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 2)+ self.assertEqual(result1['value'], 'Ingredient, test, 2, organic, raw')+ self.assertEqual(result1['data']['id'], 2)++ def test_search_language_code_en(self):+ """+ Explicitly passing the en language code (same as no code)+ """+ response = self.client.get(self.url + '?term=test&language=en')+ result1 = response.data['suggestions'][0]++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 2)+ self.assertEqual(result1['value'], 'Ingredient, test, 2, organic, raw')+ self.assertEqual(result1['data']['id'], 2)++ def test_search_language_code_en_no_results(self):+ """+ The "Testzutat" ingredient should not be found when searching in English+ """+ response = self.client.get(self.url + '?term=Testzutat&language=en')++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 0)++ def test_search_language_code_de(self):+ """+ The "Testübung" exercise should be only found when searching in German+ """+ response = self.client.get(self.url + '?term=Testzutat&language=de')+ result1 = response.data['suggestions'][0]++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 1)+ self.assertEqual(result1['value'], 'Testzutat 123')+ self.assertEqual(result1['data']['id'], 6)++ def test_search_several_language_codes(self):+ """+ Passing different language codes works correctly+ """+ response = self.client.get(self.url + '?term=guest&language=en,de')++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 5)++ def test_search_unknown_language_codes(self):+ """+ Unknown language codes are ignored+ """+ response = self.client.get(self.url + '?term=guest&language=en,de,kg')++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 5)++ def test_search_all_languages(self):+ """+ Disable all language filters+ """+ response = self.client.get(self.url + '?term=guest&language=*')++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(len(response.data['suggestions']), 7)diff --git a/wger/utils/constants.py b/wger/utils/constants.pyindex 144be1f2d..833ef1e69 100644--- a/wger/utils/constants.py+++ b/wger/utils/constants.py@@ -72,3 +72,4 @@ OFF_SEARCH_PRODUCT_NOT_FOUND = 0# APIAPI_MAX_ITEMS = 999+SEARCH_ALL_LANGUAGES = '*'