Files
Tom FluxandClaude Opus 4.8 ce61a0a86f Phase 1: mobile-first pantry redesign with In/Low/Out state
Track presence (In/Low/Out) as the primary signal instead of exact
quantities; quantity becomes an optional integer, unit optional too
(migration 0003 backfills state from quantity: 0 -> out, else in).

- Pantry rebuilt as a phone-first card list: colored state rail,
  segmented In/Low/Out switch (server-driven HTMX), greyed out-items,
  live summary counts.
- Fast add: bottom add bar with type-ahead autocomplete, location
  picker, and quick-add chips for things you've run out of.
- Per-item menu (move / set expiry / delete); expiry is now an
  optional quiet pill, never required.
- New endpoints: pantry search + set-state; dropped the old
  edit-expiry/cancel flow and its partial.
- Recipes matcher made presence-based (have-it beats have-enough);
  state added to admin. Tests cover state, add/restock, search,
  presence matching, and page rendering.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 19:55:16 +01:00

53 lines
1.6 KiB
Python

"""
Shared kitchen helpers used by both the DRF API views (views.py) and the
HTMX views (views_htmx.py). Consolidated here to avoid cross-module
duplication and the views_htmx -> views runtime coupling.
"""
from decimal import Decimal
from .models import Ingredient, PantryItem
def find_ingredient(name):
"""Find ingredient by name or alias (case-insensitive)."""
# Exact name match
try:
return Ingredient.objects.get(name__iexact=name)
except Ingredient.DoesNotExist:
pass
# Search aliases (JSONField contains). Guard against a falsy aliases value.
for ingredient in Ingredient.objects.all():
if ingredient.aliases and any(a.lower() == name.lower() for a in ingredient.aliases):
return ingredient
return None
def get_pantry_total(ingredient_id):
"""Total quantity of an ingredient across all locations, excluding items
marked 'out'. Null quantities count as 0 (presence is tracked via state,
not this number — see the pantry redesign in plan.md)."""
total = Decimal("0")
for item in (
PantryItem.objects.filter(ingredient_id=ingredient_id)
.exclude(state=PantryItem.State.OUT)
):
total += Decimal(item.quantity or 0)
return total
def get_section_key(ingredient):
"""Determine the canonical (lowercase) shopping section key from an
ingredient's tags."""
tag_names = set(ingredient.tags.values_list("name", flat=True))
if "protein" in tag_names:
return "protein"
if "veg" in tag_names:
return "veg"
if "carb" in tag_names:
return "carbs"
if "dairy" in tag_names:
return "dairy"
return "other"