diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index 4987e0b5b..ee4b8d916 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -124,7 +124,7 @@ class IngredientParser: def parse_amount(self, x): amount = 0 - unit = '' + unit = None note = '' did_check_frac = False @@ -155,7 +155,7 @@ class IngredientParser: except ValueError: unit = x[end:] - if unit.startswith('(') or unit.startswith('-'): # i dont know any unit that starts with ( or - so its likely an alternative like 1L (500ml) Water or 2-3 + if unit is not None and (unit.startswith('(') or unit.startswith('-')): # i dont know any unit that starts with ( or - so its likely an alternative like 1L (500ml) Water or 2-3 unit = '' note = x return amount, unit, note @@ -230,7 +230,7 @@ class IngredientParser: # a fraction for the amount if len(tokens) > 2: try: - if not unit == '': + if unit is not None: # a unit is already found, no need to try the second argument for a fraction # probably not the best method to do it, but I didn't want to make an if check and paste the exact same thing in the else as already is in the except # noqa: E501 raise ValueError @@ -252,7 +252,7 @@ class IngredientParser: # try to use second argument as unit and everything else as ingredient, use everything as ingredient if it fails # noqa: E501 try: ingredient, note = self.parse_ingredient(tokens[2:]) - if unit == '': + if unit is None: unit = tokens[1] else: note = tokens[1] diff --git a/cookbook/tests/other/test_ingredient_parser.py b/cookbook/tests/other/test_ingredient_parser.py index 5d61fa87d..d2d6b144a 100644 --- a/cookbook/tests/other/test_ingredient_parser.py +++ b/cookbook/tests/other/test_ingredient_parser.py @@ -10,33 +10,33 @@ def test_ingredient_parser(): "4 l Wasser": (4, "l", "Wasser", ""), "½l Wasser": (0.5, "l", "Wasser", ""), "⅛ Liter Sauerrahm": (0.125, "Liter", "Sauerrahm", ""), - "5 Zwiebeln": (5, "", "Zwiebeln", ""), - "3 Zwiebeln, gehackt": (3, "", "Zwiebeln", "gehackt"), - "5 Zwiebeln (gehackt)": (5, "", "Zwiebeln", "gehackt"), - "1 Zwiebel(n)": (1, "", "Zwiebel(n)", ""), - "4 1/2 Zwiebeln": (4.5, "", "Zwiebeln", ""), - "4 ½ Zwiebeln": (4.5, "", "Zwiebeln", ""), + "5 Zwiebeln": (5, None, "Zwiebeln", ""), + "3 Zwiebeln, gehackt": (3, None, "Zwiebeln", "gehackt"), + "5 Zwiebeln (gehackt)": (5, None, "Zwiebeln", "gehackt"), + "1 Zwiebel(n)": (1, None, "Zwiebel(n)", ""), + "4 1/2 Zwiebeln": (4.5, None, "Zwiebeln", ""), + "4 ½ Zwiebeln": (4.5, None, "Zwiebeln", ""), "1/2 EL Mehl": (0.5, "EL", "Mehl", ""), - "1/2 Zwiebel": (0.5, "", "Zwiebel", ""), + "1/2 Zwiebel": (0.5, None, "Zwiebel", ""), "1/5g Mehl, gesiebt": (0.2, "g", "Mehl", "gesiebt"), - "1/2 Zitrone, ausgepresst": (0.5, "", "Zitrone", "ausgepresst"), - "etwas Mehl": (0, "", "etwas Mehl", ""), - "Öl zum Anbraten": (0, "", "Öl zum Anbraten", ""), - "n. B. Knoblauch, zerdrückt": (0, "", "n. B. Knoblauch", "zerdrückt"), + "1/2 Zitrone, ausgepresst": (0.5, None, "Zitrone", "ausgepresst"), + "etwas Mehl": (0, None, "etwas Mehl", ""), + "Öl zum Anbraten": (0, None, "Öl zum Anbraten", ""), + "n. B. Knoblauch, zerdrückt": (0, None, "n. B. Knoblauch", "zerdrückt"), "Kräuter, mediterrane (Oregano, Rosmarin, Basilikum)": ( - 0, "", "Kräuter, mediterrane", "Oregano, Rosmarin, Basilikum"), + 0, None, "Kräuter, mediterrane", "Oregano, Rosmarin, Basilikum"), "600 g Kürbisfleisch (Hokkaido), geschält, entkernt und geraspelt": ( 600, "g", "Kürbisfleisch (Hokkaido)", "geschält, entkernt und geraspelt"), - "Muskat": (0, "", "Muskat", ""), + "Muskat": (0, None, "Muskat", ""), "200 g Mehl, glattes": (200, "g", "Mehl", "glattes"), - "1 Ei(er)": (1, "", "Ei(er)", ""), + "1 Ei(er)": (1, None, "Ei(er)", ""), "1 Prise(n) Salz": (1, "Prise(n)", "Salz", ""), - "etwas Wasser, lauwarmes": (0, "", "etwas Wasser", "lauwarmes"), - "Strudelblätter, fertige, für zwei Strudel": (0, "", "Strudelblätter", "fertige, für zwei Strudel"), - "barrel-aged Bourbon": (0, "", "barrel-aged Bourbon", ""), - "golden syrup": (0, "", "golden syrup", ""), - "unsalted butter, for greasing": (0, "", "unsalted butter", "for greasing"), - "unsalted butter , for greasing": (0, "", "unsalted butter", "for greasing"), # trim + "etwas Wasser, lauwarmes": (0, None, "etwas Wasser", "lauwarmes"), + "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"), + "unsalted butter , for greasing": (0, None, "unsalted butter", "for greasing"), # trim "1 small sprig of fresh rosemary": (1, "small", "sprig of fresh rosemary", ""), # does not always work perfectly! "75 g fresh breadcrumbs": (75, "g", "fresh breadcrumbs", ""), @@ -49,7 +49,7 @@ def test_ingredient_parser(): "1 Zwiebel gehackt": (1, "Zwiebel", "gehackt", ""), "1 EL Kokosöl": (1, "EL", "Kokosöl", ""), "0.5 paket jäst (à 50 g)": (0.5, "paket", "jäst", "à 50 g"), - "ägg": (0, "", "ägg", ""), + "ägg": (0, None, "ägg", ""), "50 g smör eller margarin": (50, "g", "smör eller margarin", ""), "3,5 l Wasser": (3.5, "l", "Wasser", ""), "3.5 l Wasser": (3.5, "l", "Wasser", ""), @@ -70,4 +70,4 @@ def test_ingredient_parser(): for key, val in expectations.items(): count += 1 parsed = ingredient_parser.parse(key) - assert val == parsed + assert parsed == val diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 15e3cf299..0f1372d2e 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1190,6 +1190,11 @@ def recipe_from_source(request): 'error': True, 'msg': _('Connection Refused.') }, status=400) + except requests.exceptions.MissingSchema: + return JsonResponse({ + 'error': True, + 'msg': _('Bad URL Schema.') + }, status=400) recipe_json, recipe_tree, recipe_html, recipe_images = get_recipe_from_source(data, url, request) if len(recipe_tree) == 0 and len(recipe_json) == 0: return JsonResponse({