Self hosted FLOSS fitness/workout, nutrition and weight tracker
feat: trigger Open Food Facts lookup on barcode text search
Ewin7e8b02d034875
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.pyindex 4d86ad6fd..65a5c8652 100644--- a/wger/nutrition/api/filtersets.py+++ b/wger/nutrition/api/filtersets.py@@ -70,9 +70,17 @@ class IngredientFilterSet(filters.FilterSet):def search_name_fulltext(self, queryset, name, value):"""- Perform a fulltext search when postgres is available+ Perform a fulltext search when a barcode is entered or postgres is available"""+ # If a numeric value looks like a barcode (EAN-8, UPC-A, EAN-13, GTIN-14),+ # try an exact barcode lookup first.+ if value.isdigit() and len(value) in (8, 12, 13, 14):+ barcode_qs = self.search_barcode(queryset, 'code', value)++ if barcode_qs.exists():+ return barcode_qs+if is_postgres_db():# Note: this uses the default value for pg_trgm.similarity_threshold (0.3) which# might be too strict (doesn't find "butter" from "buttr"). If this needs to bediff --git a/wger/nutrition/tests/test_search_api.py b/wger/nutrition/tests/test_search_api.pyindex 105354c45..ba9463abe 100644--- a/wger/nutrition/tests/test_search_api.py+++ b/wger/nutrition/tests/test_search_api.py@@ -13,6 +13,9 @@# 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/>.+# Standard Library+from unittest.mock import patch+# Third Partyfrom rest_framework import status@@ -97,6 +100,38 @@ class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(response.data['count'], 7)+ @patch('wger.nutrition.models.Ingredient.fetch_ingredient_from_off')+ def test_search_name_with_valid_barcode(self, mock_fetch):+ """+ Searching for an 8-14 digit number in the name field should trigger the OFF API lookup+ """+ self.client.get(self.url + '?name__search=1300000000000')+ mock_fetch.assert_called_once_with('1300000000000')++ @patch('wger.nutrition.models.Ingredient.fetch_ingredient_from_off')+ def test_search_name_with_text(self, mock_fetch):+ """+ Searching for a standard text string should fall back to full-text search+ """+ self.client.get(self.url + '?name__search=apples')+ mock_fetch.assert_not_called()++ @patch('wger.nutrition.models.Ingredient.fetch_ingredient_from_off')+ def test_search_name_with_short_number(self, mock_fetch):+ """+ Searching for a short number (<8) should fall back to full-text search+ """+ self.client.get(self.url + '?name__search=7000000')+ mock_fetch.assert_not_called()++ @patch('wger.nutrition.models.Ingredient.fetch_ingredient_from_off')+ def test_search_name_with_long_number(self, mock_fetch):+ """+ Searching for a long number (>14) should fall back to full-text search+ """+ self.client.get(self.url + '?name__search=140000000000000')+ mock_fetch.assert_not_called()+def test_filter_nutriscore_exact(self):"""Exact match on nutriscore returns only ingredients with that grade