mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-30 13:50:42 -05:00
feat: adding the rest ofthe nutrition properties from schema.org (#4301)
This commit is contained in:
@@ -9,28 +9,52 @@ class Nutrition(SqlAlchemyBase):
|
||||
__tablename__ = "recipe_nutrition"
|
||||
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
|
||||
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True)
|
||||
|
||||
calories: Mapped[str | None] = mapped_column(sa.String)
|
||||
carbohydrate_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
cholesterol_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
fat_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
fiber_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
protein_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
carbohydrate_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
saturated_fat_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
|
||||
# `serving_size` is not a scaling factor, but a per-serving volume or mass
|
||||
# according to schema.org. E.g., "2 L", "500 g", "5 cups", etc.
|
||||
#
|
||||
# Ignoring for now because it's too difficult to work around variable units
|
||||
# in translation for the frontend. Also, it causes cognitive dissonance wrt
|
||||
# "servings" (i.e., "serves 2" etc.), which is an unrelated concept that
|
||||
# might cause confusion.
|
||||
#
|
||||
# serving_size: Mapped[str | None] = mapped_column(sa.String)
|
||||
|
||||
sodium_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
sugar_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
trans_fat_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
unsaturated_fat_content: Mapped[str | None] = mapped_column(sa.String)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
calories=None,
|
||||
carbohydrate_content=None,
|
||||
cholesterol_content=None,
|
||||
fat_content=None,
|
||||
fiber_content=None,
|
||||
protein_content=None,
|
||||
saturated_fat_content=None,
|
||||
sodium_content=None,
|
||||
sugar_content=None,
|
||||
carbohydrate_content=None,
|
||||
trans_fat_content=None,
|
||||
unsaturated_fat_content=None,
|
||||
) -> None:
|
||||
self.calories = calories
|
||||
self.carbohydrate_content = carbohydrate_content
|
||||
self.cholesterol_content = cholesterol_content
|
||||
self.fat_content = fat_content
|
||||
self.fiber_content = fiber_content
|
||||
self.protein_content = protein_content
|
||||
self.saturated_fat_content = saturated_fat_content
|
||||
self.sodium_content = sodium_content
|
||||
self.sugar_content = sugar_content
|
||||
self.carbohydrate_content = carbohydrate_content
|
||||
self.trans_fat_content = trans_fat_content
|
||||
self.unsaturated_fat_content = unsaturated_fat_content
|
||||
|
||||
@@ -187,7 +187,7 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
settings: dict | None = None,
|
||||
**_,
|
||||
) -> None:
|
||||
self.nutrition = Nutrition(**nutrition) if nutrition else Nutrition()
|
||||
self.nutrition = Nutrition(**(nutrition or {}))
|
||||
|
||||
if recipe_instructions is not None:
|
||||
self.recipe_instructions = [RecipeInstruction(**step, session=session) for step in recipe_instructions]
|
||||
@@ -198,7 +198,7 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
if assets:
|
||||
self.assets = [RecipeAsset(**a) for a in assets]
|
||||
|
||||
self.settings = RecipeSettings(**settings) if settings else RecipeSettings()
|
||||
self.settings = RecipeSettings(**(settings or {}))
|
||||
|
||||
if notes:
|
||||
self.notes = [Note(**n) for n in notes]
|
||||
|
||||
@@ -104,15 +104,7 @@ def content_with_meta(group_slug: str, recipe: Recipe) -> str:
|
||||
|
||||
ingredients.append(s)
|
||||
|
||||
nutrition: dict[str, str | None] = {}
|
||||
if recipe.nutrition:
|
||||
nutrition["calories"] = recipe.nutrition.calories
|
||||
nutrition["fatContent"] = recipe.nutrition.fat_content
|
||||
nutrition["fiberContent"] = recipe.nutrition.fiber_content
|
||||
nutrition["proteinContent"] = recipe.nutrition.protein_content
|
||||
nutrition["carbohydrateContent"] = recipe.nutrition.carbohydrate_content
|
||||
nutrition["sodiumContent"] = recipe.nutrition.sodium_content
|
||||
nutrition["sugarContent"] = recipe.nutrition.sugar_content
|
||||
nutrition: dict[str, str | None] = recipe.nutrition.model_dump(by_alias=True) if recipe.nutrition else {}
|
||||
|
||||
as_schema_org = {
|
||||
"@context": "https://schema.org",
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
from pydantic import ConfigDict
|
||||
from pydantic.alias_generators import to_camel
|
||||
|
||||
from mealie.schema._mealie import MealieModel
|
||||
|
||||
|
||||
class Nutrition(MealieModel):
|
||||
calories: str | None = None
|
||||
fat_content: str | None = None
|
||||
protein_content: str | None = None
|
||||
carbohydrate_content: str | None = None
|
||||
cholesterol_content: str | None = None
|
||||
fat_content: str | None = None
|
||||
fiber_content: str | None = None
|
||||
protein_content: str | None = None
|
||||
saturated_fat_content: str | None = None
|
||||
sodium_content: str | None = None
|
||||
sugar_content: str | None = None
|
||||
model_config = ConfigDict(from_attributes=True, coerce_numbers_to_str=True)
|
||||
trans_fat_content: str | None = None
|
||||
unsaturated_fat_content: str | None = None
|
||||
|
||||
model_config = ConfigDict(
|
||||
from_attributes=True,
|
||||
coerce_numbers_to_str=True,
|
||||
alias_generator=to_camel,
|
||||
)
|
||||
|
||||
@@ -12,6 +12,18 @@ from mealie.services.scraper import cleaner
|
||||
from ._migration_base import BaseMigrator
|
||||
from .utils.migration_helpers import scrape_image, split_by_line_break, split_by_semicolon
|
||||
|
||||
nutrition_map = {
|
||||
"carbohydrate": "carbohydrateContent",
|
||||
"protein": "proteinContent",
|
||||
"fat": "fatContent",
|
||||
"saturatedfat": "saturatedFatContent",
|
||||
"transfat": "transFatContent",
|
||||
"sodium": "sodiumContent",
|
||||
"fiber": "fiberContent",
|
||||
"sugar": "sugarContent",
|
||||
"unsaturatedfat": "unsaturatedFatContent",
|
||||
}
|
||||
|
||||
|
||||
class MyRecipeBoxMigrator(BaseMigrator):
|
||||
def __init__(self, **kwargs):
|
||||
@@ -53,22 +65,26 @@ class MyRecipeBoxMigrator(BaseMigrator):
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def parse_nutrition(self, input: Any) -> dict | None:
|
||||
if not input or not isinstance(input, str):
|
||||
def parse_nutrition(self, input_: Any) -> dict | None:
|
||||
if not input_ or not isinstance(input_, str):
|
||||
return None
|
||||
|
||||
nutrition = {}
|
||||
|
||||
vals = [x.strip() for x in input.split(",") if x]
|
||||
vals = (x.strip() for x in input_.split("\n") if x)
|
||||
for val in vals:
|
||||
try:
|
||||
key, value = val.split(":", maxsplit=1)
|
||||
key, value = (x.strip() for x in val.split(":", maxsplit=1))
|
||||
|
||||
if not (key and value):
|
||||
continue
|
||||
|
||||
key = nutrition_map.get(key.lower(), key)
|
||||
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
nutrition[key.strip()] = value.strip()
|
||||
nutrition[key] = value
|
||||
|
||||
return cleaner.clean_nutrition(nutrition) if nutrition else None
|
||||
|
||||
|
||||
@@ -37,6 +37,19 @@ def get_value_as_string_or_none(dictionary: dict, key: str):
|
||||
return None
|
||||
|
||||
|
||||
nutrition_map = {
|
||||
"Calories": "calories",
|
||||
"Fat": "fatContent",
|
||||
"Saturated Fat": "saturatedFatContent",
|
||||
"Cholesterol": "cholesterolContent",
|
||||
"Sodium": "sodiumContent",
|
||||
"Sugar": "sugarContent",
|
||||
"Carbohydrate": "carbohydrateContent",
|
||||
"Fiber": "fiberContent",
|
||||
"Protein": "proteinContent",
|
||||
}
|
||||
|
||||
|
||||
class PlanToEatMigrator(BaseMigrator):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@@ -63,16 +76,7 @@ class PlanToEatMigrator(BaseMigrator):
|
||||
|
||||
def _parse_recipe_nutrition_from_row(self, row: dict) -> dict:
|
||||
"""Parses the nutrition data from the row"""
|
||||
|
||||
nut_dict: dict = {}
|
||||
|
||||
nut_dict["calories"] = get_value_as_string_or_none(row, "Calories")
|
||||
nut_dict["fatContent"] = get_value_as_string_or_none(row, "Fat")
|
||||
nut_dict["proteinContent"] = get_value_as_string_or_none(row, "Protein")
|
||||
nut_dict["carbohydrateContent"] = get_value_as_string_or_none(row, "Carbohydrate")
|
||||
nut_dict["fiberContent"] = get_value_as_string_or_none(row, "Fiber")
|
||||
nut_dict["sodiumContent"] = get_value_as_string_or_none(row, "Sodium")
|
||||
nut_dict["sugarContent"] = get_value_as_string_or_none(row, "Sugar")
|
||||
nut_dict = {normalized_k: row[k] for k, normalized_k in nutrition_map.items() if k in row}
|
||||
|
||||
return cleaner.clean_nutrition(nut_dict)
|
||||
|
||||
|
||||
@@ -495,7 +495,7 @@ def clean_nutrition(nutrition: dict | None) -> dict[str, str]:
|
||||
list of valid keys
|
||||
|
||||
Assumptionas:
|
||||
- All units are supplied in grams, expect sodium which maybe be in milligrams
|
||||
- All units are supplied in grams, expect sodium and cholesterol which maybe be in milligrams
|
||||
|
||||
Returns:
|
||||
dict[str, str]: If the argument is None, or not a dictionary, an empty dictionary is returned
|
||||
@@ -509,9 +509,10 @@ def clean_nutrition(nutrition: dict | None) -> dict[str, str]:
|
||||
if matched_digits := MATCH_DIGITS.search(val):
|
||||
output_nutrition[key] = matched_digits.group(0).replace(",", ".")
|
||||
|
||||
if sodium := nutrition.get("sodiumContent", None):
|
||||
if isinstance(sodium, str) and "m" not in sodium and "g" in sodium:
|
||||
with contextlib.suppress(AttributeError, TypeError):
|
||||
output_nutrition["sodiumContent"] = str(float(output_nutrition["sodiumContent"]) * 1000)
|
||||
for key in ["sodiumContent", "cholesterolContent"]:
|
||||
if val := nutrition.get(key, None):
|
||||
if isinstance(val, str) and "m" not in val and "g" in val:
|
||||
with contextlib.suppress(AttributeError, TypeError):
|
||||
output_nutrition[key] = str(float(output_nutrition[key]) * 1000)
|
||||
|
||||
return output_nutrition
|
||||
|
||||
Reference in New Issue
Block a user