-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmealplan.py
31 lines (28 loc) · 1.42 KB
/
mealplan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class MealPlanManager:
def __init__(self, api, logger):
self.api = api
self.logger = logger
def create_from_recipes(self, recipes, mp_type, date, note=None, share=[]):
for r in recipes:
self.create(r, mp_type, date, note, share)
def cleanup_uncooked(self, date, mp_type):
# get all plans of meal type
plans = [mp for mp in self.api.get_meal_plans(date, ttl=False) if mp['meal_type']['id'] == mp_type]
# get all recipes cooked since cleanup date
cooked_recipes = self.api.get_recipes(params={'cookedon': date.strftime('%Y-%m-%d')}, cache=False)
# for each plan containing a recipe not cooked since cleanup date - delete the plan
plans_to_delete = [p for p in plans if p['recipe']['id'] not in [y['id'] for y in cooked_recipes]]
self.logger.info(f'Deleting {len(plans_to_delete)} meal plans that were not cooked.')
for plan in plans_to_delete:
self.api.delete_meal_plan(plan['id'])
def create(self, recipe, type, date, note, share):
self.logger.debug(f'Attempting to create mealplan of type {type} for recipe {recipe.name} on {date.strftime("%Y-%m-%d")}')
self.api.create_meal_plan(
title=recipe.name,
recipe=recipe,
servings=recipe.servings,
type=type,
note=note,
date=date,
shared=[{'id': x} for x in share]
)