Store nutrition per serving, and check the estimate before sending it

Recipes imported from a video caption have no nutrition data, so the values
have to be estimated. Mealie will accept anything: the fields are free-text
strings, a misspelt key is dropped silently, and a whole-recipe total looks
exactly like a per-serving one. The recipe page then renders whatever landed
as fact.

patch_recipe now validates a nutrition block first. Keys must be Mealie's own,
so a number cannot vanish into "carbs". Values are normalized to bare numbers,
matching how the library already stores them. Energy is checked against the
macros with the Atwater factors (4/9/4 kcal per gram) and refused if it is
more than 25% off, which is what catches an arithmetic slip.

Nutrition is per serving, and the text import left recipeServings at 0 --- it
set only the free-text recipeYield, so "4-6 personer" gave Mealie no number to
divide by or scale with. import_recipe_text now also sets recipeServings, from
the lower bound of a range, which is how this library already stores
"10-12 personer" (recipeServings 10). patch_recipe refuses nutrition while the
count is still missing, and accepts it when the same patch supplies it.

verify_recipe fails an import that has no nutrition, or has values without a
serving count.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-17 14:52:54 +02:00
parent 42d78f4060
commit 7d693ef429
4 changed files with 249 additions and 3 deletions
+15
View File
@@ -179,6 +179,21 @@ def verify_recipe(
else:
checks.append(_check("taxonomy", "fail", "No categories or tags, and no skip reason given"))
nutrition = {k: v for k, v in (recipe.get("nutrition") or {}).items() if v}
servings = recipe.get("recipeServings") or 0
if not servings:
# Mealie's figures are per serving, so nutrition without a serving count
# is a number without a denominator.
nutrition_status: Status = "fail"
nutrition_detail = "No recipeServings, so per-serving nutrition cannot be read"
elif not nutrition:
nutrition_status = "fail"
nutrition_detail = "No nutrition values"
else:
nutrition_status = "pass"
nutrition_detail = f"{len(nutrition)} value(s) per serving, {servings:g} serving(s)"
checks.append(_check("nutrition", nutrition_status, nutrition_detail))
source = recipe.get("orgURL") or (recipe.get("extras") or {}).get("source")
checks.append(
_check(