fix: fixed templated rendering using class object attributes

This commit is contained in:
Aerex
2019-04-13 01:28:43 -05:00
parent e5e9ddd836
commit 3a85ad936d
6 changed files with 90 additions and 109 deletions

View File

@@ -1,4 +1,5 @@
from grocy.commands.stock import Stock
from grocy.commands.product import Product
from grocy.commands.recipe import Recipe
from grocy.commands.chore import Chore
from grocy.commands.task import Task

View File

@@ -1,13 +1,14 @@
from grocy import RestService
from grocy.commands import products
import html2text
from grocy.commands import product
from tabulate import tabulate
from os import path
class Recipe(object):
GET_RECIPES = '/get-objects/recipes'
GET_RECIPE = '/get-object/recipes/{0}'
GET_PRODUCT = '/get-object/products/{0}'
GET_RECIPES_POS = '/get-object/recipes_pos'
GET_RECIPES = '/objects/recipes'
GET_RECIPE = '/objects/recipes/{0}'
GET_PRODUCT = '/objects/products/{0}'
GET_RECIPES_POS = '/objects/recipes_pos'
def __init__(self, id, **entries):
self.id = id
self.__dict__.update(entries)
@@ -19,40 +20,6 @@ class Recipe(object):
#if not hasattr('colalign', self):
# self.colalign = None
def _get_products_by_recipe_id(self):
recipe_products = self.rest_service.get(Recipe.GET_RECIPES_POS)
products_for_recipe = []
for recipe_product in recipe_products:
if recipe_product.get('recipe_id') == self.id:
## TODO: need to find a better way to run a batch call to get only products for recipe
product = self.rest_service.get(Recipe.GET_PRODUCT.format(recipe_product.get('product_id'))
## combined dict into single dict
product_recipie_info = {k: v for combined_dict in [product, recipe_product] for k, v in combined_dict.items()}
self.products.append(product_recipie_info)
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 get_list(self):
try:
recipes = self.rest_service.get(Recipe.GET_RECIPES)
@@ -67,15 +34,45 @@ class Recipe(object):
# Generate stock overview table
return tabulate(table_entries, headers=table_headers)
def _get_products_by_recipe_id(self):
recipe_products = self.rest_service.get(Recipe.GET_RECIPES_POS)
products_for_recipe = []
for recipe_product in recipe_products:
if recipe_product.get('recipe_id') == self.id:
## TODO: need to find a better way to run a batch call to get only products for recipe
product = self.rest_service.get(Recipe.GET_PRODUCT.format(recipe_product.get('product_id')))
## combined dict into single dict
product_recipe_info = {k: v for combined_dict in [product, recipe_product] for k, v in combined_dict.items()}
self.products.append(product_recipe_info)
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 get(self, include_products=False):
try:
recipe = self.rest_service.get(Recipe.GET_RECIPE.format(self.id))
if 'description' in recipe:
recipe['description_txt'] = html2text.html2text(recipe['description'].strip())
self.__dict__.update(recipe)
if include_products:
self.products = self._get_products_by_recipe_id()
self._get_products_by_recipe_id()
except Exception as e:
raise e
return recipe.to_json()

View File

@@ -14,7 +14,6 @@ class Stock(object):
#if not hasattr('colalign', self):
# self.colalign = None
def _set_default_table_formats(self):
if not hasattr('formats', self):
@@ -24,7 +23,6 @@ class Stock(object):
self.tableformat = None
elif not hasattr('col', self.formats):
self.colalign = None
def _init_rest_service(self):
@@ -59,6 +57,5 @@ class Stock(object):
# Generate stock overview table
table_headers = ['Product', 'Amount', 'Best Before Date', 'Amount Opened']
return tabulate(table_entries, headers=table_headers)
except Exception as e:
raise e