Files
Meal-Planner/backend/app/api/pantry.py
T
adminandClaude Opus 4.7 8e89f793d5 feat: phase r1+r2 recovery + r3-0 swiftly api ingestion
R1 stabilization: pytest harness with transactional db fixture, smoke
+ alembic + auth + scrape + approval + swiftly tests, github actions
ci yaml. Bearer-token admin auth + signed-cookie session for family
ui mutations. Async POST /api/admin/scrape (BackgroundTasks, returns
202). Path canonicalization (no /list, /planned suffixes). DATABASE_URL
fail-fast on empty.

R2 deferred-risk spikes: live lucky california fetch (R2-A), full
email+per-voter approval click round trip with single-use enforcement
(R2-B, console email backend, sendgrid stub).

R3-0 phase 3 redesign: replaced playwright html scraper with requests
based swiftly json api client. 17 categories, ~10k products per scrape,
upsert by (source, external_id). 401 surfaces actionable token-refresh
message via ScrapeLog.error_message.

Pre-existing defects fixed: shopping_list.py syntax error blocking app
import, MealPlan.votes orphan relationship, JSONB(astext=True) invalid
kwarg, missing requests dep, calorie_target schema drift, every SQLEnum
needed values_callable, 0001 had empty downgrade(), seed had duplicate
ingredient rows.

Migrations added: 0003 grocery_item.description, 0004 family_profile.
calorie_target, 0005 grocery_item.external_id + source + composite index.

Verified: 31/31 pytest green, alembic upgrade->downgrade->upgrade clean,
frontend npm run build clean, live scrape 9,960 grocery_item rows in 36s.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 14:08:19 -07:00

82 lines
2.6 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import HomePantry, Ingredient, FamilyProfile
from app.schemas import HomePantryResponse, HomePantryCreate
from app.security import require_session
from uuid import UUID
from typing import List
router = APIRouter()
@router.get("", response_model=List[HomePantryResponse])
def get_pantry_items(db: Session = Depends(get_db)):
profile = db.query(FamilyProfile).first()
if not profile:
raise HTTPException(status_code=404, detail="Family profile not found")
items = db.query(HomePantry).filter(
HomePantry.family_profile_id == profile.id
).all()
return items
@router.post("", response_model=HomePantryResponse, dependencies=[Depends(require_session)])
def add_pantry_item(item: HomePantryCreate, db: Session = Depends(get_db)):
profile = db.query(FamilyProfile).first()
if not profile:
raise HTTPException(status_code=404, detail="Family profile not found")
existing = db.query(HomePantry).filter(
HomePantry.family_profile_id == profile.id,
HomePantry.ingredient_id == item.ingredient_id
).first()
if existing:
existing.quantity = item.quantity
existing.unit = item.unit
existing.expires_at = item.expires_at
db.commit()
db.refresh(existing)
return existing
db_item = HomePantry(
family_profile_id=profile.id,
ingredient_id=item.ingredient_id,
quantity=item.quantity,
unit=item.unit,
expires_at=item.expires_at
)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
@router.delete("/{item_id}", dependencies=[Depends(require_session)])
def remove_pantry_item(item_id: UUID, db: Session = Depends(get_db)):
item = db.query(HomePantry).filter(HomePantry.id == item_id).first()
if not item:
raise HTTPException(status_code=404, detail="Pantry item not found")
db.delete(item)
db.commit()
return {"message": "Pantry item removed"}
@router.put("/{item_id}", response_model=HomePantryResponse, dependencies=[Depends(require_session)])
def update_pantry_item(item_id: UUID, update: HomePantryCreate, db: Session = Depends(get_db)):
item = db.query(HomePantry).filter(HomePantry.id == item_id).first()
if not item:
raise HTTPException(status_code=404, detail="Pantry item not found")
item.ingredient_id = update.ingredient_id
item.quantity = update.quantity
item.unit = update.unit
item.expires_at = update.expires_at
db.commit()
db.refresh(item)
return item