grocy-cli/grocy/models/recipe.py

135 lines
4.8 KiB
Python
Raw Normal View History

2019-04-28 16:12:05 -05:00
from grocy import RestService
import html2text
from grocy.models import Product, RecipePos
from grocy.models.schema import Schema
from tabulate import tabulate
from os import path
class Recipe(Schema):
def __init__(self, **entries):
self.fields = [
'name',
'picture_file_name', 'description',
'base_servings', 'desired_servings',
'not_check_shoppinglist', 'recipes_pos', 'products'
]
self.__dict__.update(entries)
self.recipes_pos = []
self.products = []
self._init_rest_service()
#self._set_default_table_formats()
#if not hasattr('tablefmt', self):
# self.tablefmt = None
#if not hasattr('colalign', self):
# self.colalign = None
def get_list(self):
try:
recipes = self.rest_service.get('recipe')
table_headers = ['#', 'Name']
table_entries = []
for recipe in recipes:
table_entry = [recipe.get('id'), recipe.get('name')]
table_entries.append(table_entry)
except Exception as e:
raise e
# Generate stock overview table
return tabulate(table_entries, headers=table_headers)
def _get_products_by_recipe_id(self):
recipe_products_info = self.rest_service.get('recipes_pos')
for recipe_product_info in recipe_products_info:
if recipe_product_info.get('recipe_id') == self.id:
## TODO: need to find a better way to run a batch call to get only products for recipe
product_info = self.rest_service.get('products', recipe_product_info.get('product_id'))
product = Product(**product_info)
product.id = recipe_product_info.get('product_id')
self.products.append(product)
recipe_pos = RecipePos(**recipe_product_info)
recipe_pos.id = recipe_product_info.get('id')
self.recipes_pos.append(recipe_pos)
def _set_default_table_formats(self):
if not hasattr('formats', self):
self.tablefmt = None
self.colalign = None
elif not hasattr('table', self.formats):
self.tableformat = None
elif not hasattr('col', self.formats):
self.colalign = None
# def _init_rest_service(self):
# if self.api.startswith == '/':
# self.api = self.api[1:]
# if self.api.endswith == '/':
# self.api = self.api[1:-1]
# self.rest_service = RestService(self.api, json=True)
# self.rest_service.addHeader('Content-Type', 'application/json')
# self.rest_service.addToken(self.token)
def toJSON(self):
obj = {}
for attr, value in self.__dict__.items():
isEmptyList = True if type(value) == list and len(value) == 0 else False
if attr in self.fields and not isEmptyList:
obj[attr] = value
return obj
def update(self):
try:
for item in self.products:
product = Product(**item)
self.rest_service.put('products', product, product.id)
#for item in self.recipes_pos:
# self.rest_service.put('recipes_pos', item)
updated_recipe = {
'description': self.description,
'name': self.name,
'base_servings': self.base_servings,
'desired_servings': self.desired_servings,
'not_check_shoppinglist': self.not_check_shoppinglist
}
self.rest_service.put('recipes', updated_recipe, self.id)
except Exception as e:
raise e
2019-06-16 23:54:10 -05:00
def addIngredient(self, ingredient):
ingredient['recipe_id'] = self.id
hasProduct = False
if 'product_id' in ingredient and ingredient['product_id'] is not None:
hasProduct = Product.exists(ingredient['product_id'])
if hasProduct:
self.rest_service.post('recipes_pos', ingredient)
else
raise Exception('No product was given')
2019-05-05 16:06:33 -05:00
def create(self):
created_recipe = {
'description': self.description,
'name': self.name,
'base_servings': self.base_servings,
'desired_servings': self.desired_servings,
'not_check_shoppinglist': self.not_check_shoppinglist
}
self.rest_service.post('recipes', created_recipe)
2019-06-16 23:54:10 -05:00
2019-04-28 16:12:05 -05:00
def get(self, include_products=False):
try:
recipe = self.rest_service.get('recipes', id=self.id)
if 'description' in recipe:
recipe['description'] = html2text.html2text(recipe['description'].strip())
self.__dict__.update(recipe)
if include_products:
self._get_products_by_recipe_id()
except Exception as e:
raise e