diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index dd3bfb990..f944e4164 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -9,10 +9,6 @@ from cookbook.models import Food, Ingredient, Unit class IngredientParser: request = None ignore_rules = False - # food_aliases = {} - # unit_aliases = {} - # never_unit = {} - # transpose_words = {} automation = None def __init__(self, request, cache_mode=True, ignore_automations=False): @@ -24,88 +20,8 @@ class IngredientParser: """ self.request = request self.ignore_rules = ignore_automations - self.automation = AutomationEngine(self.request, use_cache=cache_mode) - # if cache_mode: - # FOOD_CACHE_KEY = f'automation_food_alias_{self.request.space.pk}' - # if c := caches['default'].get(FOOD_CACHE_KEY, None): - # self.food_aliases = c - # caches['default'].touch(FOOD_CACHE_KEY, 30) - # else: - # for a in Automation.objects.filter(space=self.request.space, disabled=False, type=Automation.FOOD_ALIAS).only('param_1', 'param_2').order_by('order').all(): - # self.food_aliases[a.param_1.lower()] = a.param_2 - # caches['default'].set(FOOD_CACHE_KEY, self.food_aliases, 30) - - # UNIT_CACHE_KEY = f'automation_unit_alias_{self.request.space.pk}' - # if c := caches['default'].get(UNIT_CACHE_KEY, None): - # self.unit_aliases = c - # caches['default'].touch(UNIT_CACHE_KEY, 30) - # else: - # for a in Automation.objects.filter(space=self.request.space, disabled=False, type=Automation.UNIT_ALIAS).only('param_1', 'param_2').order_by('order').all(): - # self.unit_aliases[a.param_1.lower()] = a.param_2 - # caches['default'].set(UNIT_CACHE_KEY, self.unit_aliases, 30) - - # NEVER_UNIT_CACHE_KEY = f'automation_never_unit_{self.request.space.pk}' - # if c := caches['default'].get(NEVER_UNIT_CACHE_KEY, None): - # self.never_unit = c - # caches['default'].touch(NEVER_UNIT_CACHE_KEY, 30) - # else: - # for a in Automation.objects.filter(space=self.request.space, disabled=False, type=Automation.NEVER_UNIT).only('param_1', 'param_2').order_by('order').all(): - # self.never_unit[a.param_1.lower()] = a.param_2 - # caches['default'].set(NEVER_UNIT_CACHE_KEY, self.never_unit, 30) - - # TRANSPOSE_WORDS_CACHE_KEY = f'automation_transpose_words_{self.request.space.pk}' - # if c := caches['default'].get(TRANSPOSE_WORDS_CACHE_KEY, None): - # self.transpose_words = c - # caches['default'].touch(TRANSPOSE_WORDS_CACHE_KEY, 30) - # else: - # i = 0 - # for a in Automation.objects.filter(space=self.request.space, disabled=False, type=Automation.TRANSPOSE_WORDS).only('param_1', 'param_2').order_by('order').all(): - # self.transpose_words[i] = [a.param_1.lower(), a.param_2.lower()] - # i += 1 - # caches['default'].set(TRANSPOSE_WORDS_CACHE_KEY, self.transpose_words, 30) - # else: - # self.food_aliases = {} - # self.unit_aliases = {} - # self.never_unit = {} - # self.transpose_words = {} - - # def apply_food_automation(self, food): - # """ - # Apply food alias automations to passed food - # :param food: unit as string - # :return: food as string (possibly changed by automation) - # """ - # if self.ignore_rules: - # return food - # else: - # if self.food_aliases: - # try: - # return self.food_aliases[food.lower()] - # except KeyError: - # return food - # else: - # if automation := Automation.objects.filter(space=self.request.space, type=Automation.FOOD_ALIAS, param_1__iexact=food, disabled=False).order_by('order').first(): - # return automation.param_2 - # return food - - # def apply_unit_automation(self, unit): - # """ - # Apply unit alias automations to passed unit - # :param unit: unit as string - # :return: unit as string (possibly changed by automation) - # """ - # if self.ignore_rules: - # return unit - # else: - # if self.transpose_words: - # try: - # return self.unit_aliases[unit.lower()] - # except KeyError: - # return unit - # else: - # if automation := Automation.objects.filter(space=self.request.space, type=Automation.UNIT_ALIAS, param_1__iexact=unit, disabled=False).order_by('order').first(): - # return automation.param_2 - # return unit + if not self.ignore_rules: + self.automation = AutomationEngine(self.request, use_cache=cache_mode) def get_unit(self, unit): """ @@ -237,67 +153,6 @@ class IngredientParser: food, note = self.parse_food_with_comma(tokens) return food, note - # def apply_never_unit_automations(self, tokens): - # """ - # Moves a string that should never be treated as a unit to next token and optionally replaced with default unit - # e.g. NEVER_UNIT: param1: egg, param2: None would modify ['1', 'egg', 'white'] to ['1', '', 'egg', 'white'] - # or NEVER_UNIT: param1: egg, param2: pcs would modify ['1', 'egg', 'yolk'] to ['1', 'pcs', 'egg', 'yolk'] - # :param1 string: string that should never be considered a unit, will be moved to token[2] - # :param2 (optional) unit as string: will insert unit string into token[1] - # :return: unit as string (possibly changed by automation) - # """ - - # if self.ignore_rules: - # return tokens - - # new_unit = None - # alt_unit = self.apply_unit_automation(tokens[1]) - # never_unit = False - # if self.never_unit: - # try: - # new_unit = self.never_unit[tokens[1].lower()] - # never_unit = True - # except KeyError: - # return tokens - - # else: - # if automation := Automation.objects.annotate(param_1_lower=Lower('param_1')).filter(space=self.request.space, type=Automation.NEVER_UNIT, param_1_lower__in=[ - # tokens[1].lower(), alt_unit.lower()], disabled=False).order_by('order').first(): - # new_unit = automation.param_2 - # never_unit = True - - # if never_unit: - # tokens.insert(1, new_unit) - - # return tokens - - # def apply_transpose_words_automations(self, ingredient): - # """ - # If two words (param_1 & param_2) are detected in sequence, swap their position in the ingredient string - # :param 1: first word to detect - # :param 2: second word to detect - # return: new ingredient string - # """ - - # if self.ignore_rules: - # return ingredient - - # else: - # tokens = [x.lower() for x in ingredient.replace(',', ' ').split()] - # if self.transpose_words: - # filtered_rules = {} - # for key, value in self.transpose_words.items(): - # if value[0] in tokens and value[1] in tokens: - # filtered_rules[key] = value - # for k, v in filtered_rules.items(): - # ingredient = re.sub(rf"\b({v[0]})\W*({v[1]})\b", r"\2 \1", ingredient, flags=re.IGNORECASE) - # else: - # for rule in Automation.objects.filter(space=self.request.space, type=Automation.TRANSPOSE_WORDS, disabled=False) \ - # .annotate(param_1_lower=Lower('param_1'), param_2_lower=Lower('param_2')) \ - # .filter(Q(Q(param_1_lower__in=tokens) | Q(param_2_lower__in=tokens))).order_by('order'): - # ingredient = re.sub(rf"\b({rule.param_1})\W*({rule.param_1})\b", r"\2 \1", ingredient, flags=re.IGNORECASE) - # return ingredient - def parse(self, ingredient): """ Main parsing function, takes an ingredient string (e.g. '1 l Water') and extracts amount, unit, food, ... diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index ee4c9e701..3f7b65f48 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -68,17 +68,6 @@ def get_from_scraper(scrape, request): description = '' recipe_json['description'] = parse_description(description) - - # automations = Automation.objects.filter( - # type=Automation.DESCRIPTION_REPLACE, - # space=request.space, - # disabled=False).only( - # 'param_1', - # 'param_2', - # 'param_3').all().order_by('order')[ - # :512] - # for a in automations: - # if re.match(a.param_1, (recipe_json['source_url'])[:512]): recipe_json['description'] = automation_engine.apply_regex_replace_automation(recipe_json['description'], Automation.DESCRIPTION_REPLACE) # assign servings attributes @@ -218,17 +207,6 @@ def get_from_scraper(scrape, request): traceback.print_exc() pass - # if 'source_url' in recipe_json and recipe_json['source_url']: - # automations = Automation.objects.filter( - # type=Automation.INSTRUCTION_REPLACE, - # space=request.space, - # disabled=False).only( - # 'param_1', - # 'param_2', - # 'param_3').order_by('order').all()[ - # :512] - # for a in automations: - # if re.match(a.param_1, (recipe_json['source_url'])[:512]): for s in recipe_json['steps']: s['instruction'] = automation_engine.apply_regex_replace_automation(s['instruction'], Automation.INSTRUCTION_REPLACE) # re.sub(a.param_2, a.param_3, s['instruction']) diff --git a/cookbook/tests/other/test_automations.py b/cookbook/tests/other/test_automations.py index e10275cd1..48416caa7 100644 --- a/cookbook/tests/other/test_automations.py +++ b/cookbook/tests/other/test_automations.py @@ -171,36 +171,3 @@ def test_url_import_regex_replace(u1_s1): assert recipe_json['steps'][0]['ingredients'][0]['food']['name'] == target_text assert recipe_json['steps'][0]['ingredients'][1]['unit']['name'] == target_text assert recipe_json['steps'][0]['ingredients'][1]['unit']['name'] == target_text - - -# # for some reason this tests cant run due to some kind of encoding issue, needs to be fixed -# # def test_description_replace_automation(u1_s1, space_1): -# # if 'cookbook' in os.getcwd(): -# # test_file = os.path.join(os.getcwd(), 'other', 'test_data', 'chefkoch2.html') -# # else: -# # test_file = os.path.join(os.getcwd(), 'cookbook', 'tests', 'other', 'test_data', 'chefkoch2.html') -# # -# # # original description -# # # Brokkoli - Bratlinge. Über 91 Bewertungen und für vorzüglich befunden. Mit ► Portionsrechner ► Kochbuch ► Video-Tipps! Jetzt entdecken und ausprobieren! -# # -# # with scopes_disabled(): -# # Automation.objects.create( -# # name='test1', -# # created_by=auth.get_user(u1_s1), -# # space=space_1, -# # param_1='.*', -# # param_2='.*', -# # param_3='', -# # order=1000, -# # ) -# # -# # with open(test_file, 'r', encoding='UTF-8') as d: -# # response = u1_s1.post( -# # reverse(IMPORT_SOURCE_URL), -# # { -# # 'data': d.read(), -# # 'url': 'https://www.chefkoch.de/rezepte/804871184310070/Brokkoli-Bratlinge.html', -# # }, -# # content_type='application/json') -# # recipe = json.loads(response.content)['recipe_json'] -# # assert recipe['description'] == ''