Show purchased date

This commit is contained in:
jableader 2024-05-21 09:57:56 +10:00
parent ba55f8a00a
commit 72a705140b
2 changed files with 20 additions and 12 deletions

View file

@ -13,6 +13,7 @@ class Meal(BaseModel):
KEYS: ClassVar[List[str]] = ['id', 'meal_date'] KEYS: ClassVar[List[str]] = ['id', 'meal_date']
id: int = -1 id: int = -1
meal_date: datetime.datetime meal_date: datetime.datetime
purchase_date: Optional[datetime.datetime] = None
chefs: List[Person] = [] chefs: List[Person] = []
cleanup: List[Person] = [] cleanup: List[Person] = []
@ -91,28 +92,20 @@ async def find_meal_by_id(conn, meal_id: int) -> Meal:
LIMIT 1 LIMIT 1
''', (meal_id,)) as cursor: ''', (meal_id,)) as cursor:
async for row in cursor: async for row in cursor:
meal = Meal(**{k:v for k,v in zip(Meal.KEYS, row)}) meal = await with_purchase_date(conn, Meal(**{k:v for k,v in zip(Meal.KEYS, row)}))
await load_participants(conn, meal) await load_participants(conn, meal)
await load_recipes(conn, meal) await load_recipes(conn, meal)
await load_extra_ingredients(conn, meal) await load_extra_ingredients(conn, meal)
return meal return meal
async def find_meal_by_date(conn, date: datetime) -> Meal:
async with conn.execute(f'''
SELECT {','.join(Meal.KEYS)} FROM Meal
WHERE meal_date = ?
LIMIT 1
''', (date,)) as cursor:
async for row in cursor:
return Meal(**{k:v for k,v in zip(Meal.KEYS, row)})
async def find_meals_by_date_range(conn, start: datetime, end: datetime) -> AsyncIterator[Meal]: async def find_meals_by_date_range(conn, start: datetime, end: datetime) -> AsyncIterator[Meal]:
async with conn.execute(f''' async with conn.execute(f'''
SELECT {','.join(Meal.KEYS)} FROM Meal SELECT {','.join(Meal.KEYS)} FROM Meal
WHERE meal_date >= ? AND meal_date <= ? WHERE meal_date >= ? AND meal_date <= ?
''', (start, end)) as cursor: ''', (start, end)) as cursor:
async for row in cursor: async for row in cursor:
yield Meal(**{k:v for k,v in zip(Meal.KEYS, row)}) yield await with_purchase_date(conn, Meal(**{k:v for k,v in zip(Meal.KEYS, row)}))
async def load_participants(conn, meal: Meal) -> None: async def load_participants(conn, meal: Meal) -> None:
async with conn.execute(f''' async with conn.execute(f'''
@ -198,3 +191,17 @@ async def update_meal(conn, meal: Meal) -> None:
await sync_extra_ingredients(conn, meal.id, meal.extra_ingredients) await sync_extra_ingredients(conn, meal.id, meal.extra_ingredients)
await sync_recipes(conn, meal.id, meal.recipes) await sync_recipes(conn, meal.id, meal.recipes)
async def with_purchase_date(conn, meal: Meal) -> Meal:
async with conn.execute('''
SELECT purchased_date FROM ShoppingList
WHERE id = (
SELECT list_id FROM ShoppingListRequest
WHERE meal_id = ?
LIMIT 1
)
''', (meal.id,)) as cursor:
async for row in cursor:
meal.purchase_date = row[0]
return meal

View file

@ -246,7 +246,8 @@ async def _upcoming_meals(conn) -> AsyncIterator[Meal]:
start = datetime.now() start = datetime.now()
end = start + timedelta(days=7) end = start + timedelta(days=7)
async for meal in find_meals_by_date_range(conn, start, end): async for meal in find_meals_by_date_range(conn, start, end):
yield meal if meal.purchase_date is None:
yield meal
async def current_shopping_list(conn) -> ShoppingList: async def current_shopping_list(conn) -> ShoppingList:
shopping_list = None shopping_list = None