From e2ab3a0efb1c05e5e15d2d74c9088fb4d8c6f095 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 23 Apr 2022 14:15:06 +0200 Subject: [PATCH] fixed ingredient parser length issues --- cookbook/helper/ingredient_parser.py | 14 ++++++++++++-- cookbook/tests/other/test_ingredient_parser.py | 10 +++++++++- cookbook/views/views.py | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index c26c21ab..8c07635a 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -4,7 +4,7 @@ import unicodedata from django.core.cache import caches -from cookbook.models import Unit, Food, Automation +from cookbook.models import Unit, Food, Automation, Ingredient class IngredientParser: @@ -293,4 +293,14 @@ class IngredientParser: if unit: unit = self.apply_unit_automation(unit.strip()) - return amount, unit, self.apply_food_automation(food.strip()), note.strip() + food = self.apply_food_automation(food.strip()) + if len(food) > Food._meta.get_field('name').max_length: # test if food name is to long + # try splitting it at a space and taking only the first arg + if len(food.split()) > 1 and len(food.split()[0]) < Food._meta.get_field('name').max_length: + note = ' '.join(food.split()[1:]) + ' ' + note + food = food.split()[0] + else: + note = food + ' ' + note + food = food[:Food._meta.get_field('name').max_length] + + return amount, unit, food, note[:Ingredient._meta.get_field('note').max_length].strip() diff --git a/cookbook/tests/other/test_ingredient_parser.py b/cookbook/tests/other/test_ingredient_parser.py index 21b1853e..c5ad26ca 100644 --- a/cookbook/tests/other/test_ingredient_parser.py +++ b/cookbook/tests/other/test_ingredient_parser.py @@ -32,7 +32,7 @@ def test_ingredient_parser(): "1 Ei(er)": (1, None, "Ei(er)", ""), "1 Prise(n) Salz": (1, "Prise(n)", "Salz", ""), "etwas Wasser, lauwarmes": (0, None, "etwas Wasser", "lauwarmes"), - "Strudelblätter, fertige, für zwei Strudel": (0,None, "Strudelblätter", "fertige, für zwei Strudel"), + "Strudelblätter, fertige, für zwei Strudel": (0, None, "Strudelblätter", "fertige, für zwei Strudel"), "barrel-aged Bourbon": (0, None, "barrel-aged Bourbon", ""), "golden syrup": (0, None, "golden syrup", ""), "unsalted butter, for greasing": (0, None, "unsalted butter", "for greasing"), @@ -59,6 +59,14 @@ def test_ingredient_parser(): "1 (16 ounce) package dry lentils, rinsed": (1, "package", "dry lentils, rinsed", "16 ounce"), "2-3 c Water": (2, "c", "Water", "2-3"), "Pane (raffermo o secco) 80 g": (80, "g", "Pane", "raffermo o secco"), + "1 Knoblauchzehe(n), gehackt oder gepresst": (1.0, None, 'Knoblauchzehe(n)', 'gehackt oder gepresst'), + # test for over long food entries to get properly split into the note field + "1 Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l": ( + 1.0, 'Lorem', 'ipsum', 'dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l'), + "1 LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutl": ( + 1.0, None, 'LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlLoremipsumdolorsitametconsetetursadipscingeli', + 'LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutl') + } # for German you could say that if an ingredient does not have # an amount # and it starts with a lowercase letter, then that diff --git a/cookbook/views/views.py b/cookbook/views/views.py index d0224afb..ec1553d1 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -666,7 +666,7 @@ def test(request): parser = IngredientParser(request, False) data = { - 'original': 'Creme Frainche' + 'original': '1 LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutl' } data['parsed'] = parser.parse(data['original'])