Skip to content

Commit

Permalink
Add support for handling ingredients without amounts.
Browse files Browse the repository at this point in the history
Introduced `IngredientWithAmountsDisabled` to process ingredients when amounts are not enabled. Updated the ingredient parsing logic to conditionally handle ingredients based on the `enable_amount` flag.
  • Loading branch information
felixschndr committed Jan 26, 2025
1 parent b80bbe1 commit 0b72c5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
7 changes: 7 additions & 0 deletions source/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ def is_ignored(name_of_ingredient: str, ignored_ingredients: list[Ingredient]) -

def to_dict(self) -> dict:
return {"itemId": self.name, "spec": self.specification, "uuid": str(uuid.uuid4())}


@dataclasses.dataclass
class IngredientWithAmountsDisabled(Ingredient):
@staticmethod
def from_raw_data(raw_data: dict) -> Ingredient:
return IngredientWithAmountsDisabled(name=raw_data["display"])
23 changes: 11 additions & 12 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dotenv import load_dotenv
from environment_variable_getter import EnvironmentVariableGetter
from flask import Flask, request
from ingredient import Ingredient
from ingredient import Ingredient, IngredientWithAmountsDisabled
from logger_mixin import LoggerMixin

load_dotenv()
Expand All @@ -30,17 +30,15 @@ def webhook_handler() -> str:
ingredients_to_add = []
ingredients_raw_data = data["content"]["recipe_ingredient"]
for ingredient_raw_data in ingredients_raw_data:
name_of_ingredient = ingredient_raw_data["food"]["name"]
if Ingredient.is_ignored(name_of_ingredient, ignored_ingredients):
logger.log.debug(f"Ignoring ingredient {name_of_ingredient}")
continue

try:
logger.log.debug(f"Parsing ingredient {ingredient_raw_data}")
logger.log.debug(f"Parsing ingredient {ingredient_raw_data}")
if enable_amount:
name_of_ingredient = ingredient_raw_data["food"]["name"]
if Ingredient.is_ignored(name_of_ingredient, ignored_ingredients):
logger.log.debug(f"Ignoring ingredient {name_of_ingredient}")
continue
ingredients_to_add.append(Ingredient.from_raw_data(ingredient_raw_data))
except ValueError:
logger.log.warning(exc_info=True)
continue
else:
ingredients_to_add.append(IngredientWithAmountsDisabled.from_raw_data(ingredient_raw_data))

logger.log.info(f"Adding ingredients to Bring: {ingredients_to_add}")
loop.run_until_complete(bring_handler.add_items(ingredients_to_add))
Expand All @@ -60,7 +58,8 @@ def parse_ignored_ingredients() -> list[Ingredient]:
ignored_ingredients_input = EnvironmentVariableGetter.get("IGNORED_INGREDIENTS")
except RuntimeError:
logger.log.info(
'The variable IGNORED_INGREDIENTS is not set. All ingredients will be added. Consider adding something like "Salt,Pepper"'
"The variable IGNORED_INGREDIENTS is not set. All ingredients will be added. "
'Consider setting the variable to something like "Salt,Pepper"'
)
return []

Expand Down

0 comments on commit 0b72c5c

Please # to comment.