Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

add meal plan section support #182

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pygrocy/data_models/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ class EntityType(str, Enum):
USER_ENTITIES = "userentities"
USER_OBJECTS = "userobjects"
MEAL_PLAN = "meal_plan"
MEAL_PLAN_SECTIONS = "meal_plan_sections"
38 changes: 38 additions & 0 deletions pygrocy/data_models/meal_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pygrocy.grocy_api_client import (
GrocyApiClient,
MealPlanResponse,
MealPlanSectionResponse,
RecipeDetailsResponse,
)

Expand Down Expand Up @@ -50,6 +51,30 @@ def get_picture_url_path(self, width: int = 400):
return f"{path}?force_serve_as=picture&best_fit_width={width}"


class MealPlanSection(DataModel):
def __init__(self, response: MealPlanSectionResponse):
self._id = response.id
self._name = response.name
self._sort_number = response.sort_number
self._row_created_timestamp = response.row_created_timestamp

@property
def id(self) -> int:
return self._id

@property
def name(self) -> str:
return self._name

@property
def sort_number(self) -> int:
return self._sort_number

@property
def row_created_timestamp(self) -> datetime:
return self._row_created_timestamp


class MealPlanItem(DataModel):
def __init__(self, response: MealPlanResponse):
self._id = response.id
Expand All @@ -58,6 +83,7 @@ def __init__(self, response: MealPlanResponse):
self._recipe_id = response.recipe_id
self._recipe_servings = response.recipe_servings
self._note = response.note
self._section_id = response.section_id

@property
def id(self) -> int:
Expand All @@ -83,8 +109,20 @@ def note(self) -> str:
def recipe(self) -> RecipeItem:
return self._recipe

@property
def section_id(self) -> int:
return self._section_id

@property
def section(self) -> MealPlanSection:
return self._section

def get_details(self, api_client: GrocyApiClient):
if self.recipe_id:
recipe = api_client.get_recipe(self.recipe_id)
if recipe:
self._recipe = RecipeItem(recipe)
if self.section_id:
section = api_client.get_meal_plan_section(self.section_id)
if section:
self._section = MealPlanSection(section)
12 changes: 11 additions & 1 deletion pygrocy/grocy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .data_models.battery import Battery
from .data_models.chore import Chore
from .data_models.generic import EntityType
from .data_models.meal_items import MealPlanItem, RecipeItem
from .data_models.meal_items import MealPlanItem, MealPlanSection, RecipeItem
from .data_models.product import Group, Product, ShoppingListProduct
from .data_models.task import Task
from .data_models.user import User # noqa: F401
Expand Down Expand Up @@ -237,3 +237,13 @@ def delete_generic(self, entity_type: EntityType, object_id: int):

def get_generic_objects_for_type(self, entity_type: EntityType):
return self._api_client.get_generic_objects_for_type(entity_type.value)

def meal_plan_sections(self) -> List[MealPlanSection]:
raw_sections = self._api_client.get_meal_plan_sections()
return [MealPlanSection(section) for section in raw_sections]

def meal_plan_section(self, meal_plan_section_id: int) -> MealPlanSection:
section = self._api_client.get_meal_plan_section(meal_plan_section_id)

if section:
return MealPlanSection(section)
28 changes: 28 additions & 0 deletions pygrocy/grocy_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import requests

from pygrocy import EntityType
from pygrocy.utils import (
localize_datetime,
parse_bool_int,
Expand Down Expand Up @@ -68,6 +69,7 @@ def __init__(self, parsed_json):
parsed_json.get("row_created_timestamp")
)
self._userfields = parsed_json.get("userfields")
self._section_id = parse_int(parsed_json.get("section_id"))

@property
def id(self) -> int:
Expand All @@ -89,6 +91,10 @@ def recipe_servings(self) -> int:
def note(self) -> str:
return self._note

@property
def section_id(self) -> int:
return self._section_id


class RecipeDetailsResponse(object):
def __init__(self, parsed_json):
Expand Down Expand Up @@ -539,6 +545,16 @@ def __init__(self, parsed_json):
)


class MealPlanSectionResponse(object):
def __init__(self, parsed_json):
self.id = parse_int(parsed_json.get("id"))
self.name = parsed_json.get("name")
self.sort_number = parse_int(parsed_json.get("sort_number"))
self.row_created_timestamp = parse_date(
parsed_json.get("row_created_timestamp")
)


def _enable_debug_mode():
_LOGGER.setLevel(logging.DEBUG)

Expand Down Expand Up @@ -818,3 +834,15 @@ def delete_generic(self, entity_type: str, object_id: int):

def get_generic_objects_for_type(self, entity_type: str):
return self._do_get_request(f"objects/{entity_type}")

def get_meal_plan_sections(self) -> List[MealPlanSectionResponse]:
parsed_json = self.get_generic_objects_for_type(EntityType.MEAL_PLAN_SECTIONS)
if parsed_json:
return [MealPlanSectionResponse(resp) for resp in parsed_json]

def get_meal_plan_section(self, meal_plan_section_id) -> MealPlanSectionResponse:
parsed_json = self._do_get_request(
f"objects/meal_plan_sections?query%5B%5D=id%3D{meal_plan_section_id}"
)
if parsed_json and len(parsed_json) == 1:
return MealPlanSectionResponse(parsed_json[0])
Loading