grocy-cli/grocy/recipe.py

107 lines
4.0 KiB
Python
Raw Normal View History

2019-06-16 23:54:10 -05:00
from grocy.request import Request
from html2text import html2text
from grocy.stock import Stock
2019-06-16 23:54:10 -05:00
from grocy.conf import Configuration
from copy import deepcopy
from grocy.entity import Entity
2019-06-16 23:54:10 -05:00
import logging
class Recipe(object):
GET_RECIPES_FULFILLMENT_URL_TEMPLATE = '{domain}/api/recipes/fulfillment'
GET_RECIPE_FULFILLMENT_URL_TEMPLATE = '{domain}/api/recipes/{recipeId}/fulfillment'
GET_RECIPES_POS_FULFILLMENT_URL_TEMPLATE = '{domain}/api/recipes/{recipeId}/pos/fulfillment'
2019-06-16 23:54:10 -05:00
def __init__(self, id=None):
2019-06-16 23:54:10 -05:00
self.conf = Configuration()
self.conf.load()
self.id = id
2019-06-16 23:54:10 -05:00
@property
def ingredients(self):
logger = logging.getLogger('recipes.ingredients')
stock = Stock()
ingredients_detail = []
products = Entity(name='products').get()
locations = Entity(name='locations').get()
quantity_units = Entity(name='quantity_units').get()
for recipe_pos in self.pos_fulfillment:
if recipe_pos['recipe_id'] == self.id:
data = {}
product = next((p for p in products if p['id'] == recipe_pos['product_id']), None)
product['quantity_unit'] = next((q for q in quantity_units if q['id'] == recipe_pos['qu_id']), None)
product['location'] = next((l for l in locations if l['id'] == product['location_id']), None)
product['group'] = recipe_pos['ingredient_group']
# Remove extraneous properties
recipe_pos.pop('recipe_id', None)
recipe_pos.pop('ingredient_group', None)
recipe_pos.pop('id', None)
recipe_pos.pop('product_id', None)
recipe_pos.pop('recipe_pos_id', None)
recipe_pos.pop('qu_id', None)
product.pop('qu_id_purchase', None)
product.pop('qu_id_stock', None)
product.pop('location_id', None)
product['location'].pop('row_created_timestamp', None)
product['quantity_unit'].pop('row_created_timestamp', None)
product.pop('row_created_timestamp', None)
# Merge recipe pos and products
product.update(recipe_pos)
data.update(product)
ingredients_detail.append(data)
return ingredients_detail
@property
def pos_fulfillment(self):
logger = logging.getLogger('recipe.pos_fulfillment')
try:
if self.id is None:
raise Exception('recipe id is required')
url = self.GET_RECIPES_POS_FULFILLMENT_URL_TEMPLATE.format(domain=self.conf.domain, recipeId=self.id)
request = Request('get', url)
return request.send()
except Exception as e:
logger.error(e)
raise e
def generate_plain_text_description(self, description):
if not description:
raise Exception('Missing description')
logger = logging.getLogger('recipe.generate_plain_text_description')
try:
plain_text_description = html2text(description)
return plain_text_description
except Exception as e:
logger.error(e)
raise e
@property
def fulfillment(self):
logger = logging.getLogger('recipe.fulfillment')
url = self.GET_RECIPE_FULFILLMENT_URL_TEMPLATE.format(domain=self.conf.domain, recipeId=self.id)
request = Request('get', url)
try:
return request.send()
except Exception as e:
logger.error(e)
raise e
def get_fulfillments(self):
2019-06-16 23:54:10 -05:00
logger = logging.getLogger('recipe.get_requirements')
if self.id:
url = self.GET_RECIPE_FULFILLMENT_URL_TEMPLATE.format(domain=self.conf.domain, recipeId=self.id)
else:
url = self.GET_RECIPES_FULFILLMENT_URL_TEMPLATE.format(domain=self.conf.domain)
2019-06-16 23:54:10 -05:00
request = Request('get', url)
try:
return request.send()
except Exception as e:
logger.error(e)
raise e