from grocy.request import Request
from html2text import html2text
from grocy.stock import Stock
from grocy.conf import Configuration
from copy import deepcopy
from grocy.entity import Entity
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'
def __init__(self, id=None):
self.conf = Configuration()
self.conf.load()
self.id = id
@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):
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)
request = Request('get', url)
try:
return request.send()
except Exception as e:
logger.error(e)
raise e