feat: Added subcommands remove, clear and add missing products for

shpping
This commit is contained in:
Aerex 2019-10-30 00:32:21 -05:00
parent 0c1b19aa8d
commit 78b9ebb020
3 changed files with 143 additions and 74 deletions

View File

@ -574,6 +574,41 @@ def create(template):
raise e raise e
@shopping.command()
@click.argument('shopping_id')
def delete(shopping_id):
logger = logging.getLogger('cli.shopping.delete')
try:
cfg = Configuration()
cfg.load()
util = Util(cfg=cfg)
shopping_lists = Entity(name='shopping_lists')
shopping_lists.delete(shopping_id)
except Exception as e:
raise e
@shopping.command()
@click.argument('shopping_id')
def clear(shopping_id):
logger = logging.getLogger('cli.shopping.clear')
try:
cfg = Configuration()
cfg.load()
util = Util(cfg=cfg)
# Validate that shopping list exists
entity = Entity(name='shopping_lists')
found_shopping_list = entity.get(id=shopping_id)
if found_shopping_list is None:
raise Exception('Shopping list does not exist')
shopping_list = ShoppingList(id=shopping_id)
shopping_list.clear()
except Exception as e:
raise e
@shopping.command() @shopping.command()
@click.argument('shopping_id') @click.argument('shopping_id')
@click.option('-t', 'template') @click.option('-t', 'template')
@ -583,6 +618,13 @@ def add_product(shopping_id, template):
cfg = Configuration() cfg = Configuration()
cfg.load() cfg.load()
util = Util(cfg=cfg) util = Util(cfg=cfg)
# Validate that shopping list exists
entity = Entity(name='shopping_lists')
found_shopping_list = entity.get(id=shopping_id)
if found_shopping_list is None:
raise Exception('Shopping list does not exist')
shopping_list = ShoppingList(id=shopping_id) shopping_list = ShoppingList(id=shopping_id)
if template: if template:
loaded_template = cfg.templates(template) loaded_template = cfg.templates(template)
@ -595,85 +637,62 @@ def add_product(shopping_id, template):
parsed_added_product = util.load_yaml(added_product)[0] parsed_added_product = util.load_yaml(added_product)[0]
if template == 'debug': if template == 'debug':
"TODO: Reserve for meta # TODO: Reserve for meta
return return
shopping_list.add_product(parsed_added_product) shopping_list.add_product(parsed_added_product)
except Exception as e: except Exception as e:
raise e raise e
#
# @shopping.command()
#@main.command() @click.argument('shopping_id')
#@main.group() @click.option('-t', 'template')
#def ingredient(): def remove_product(shopping_id, template):
# pass logger = logging.getLogger('cli.shopping.remove_product')
# try:
# cfg = Configuration()
#@ingredient.command('add') cfg.load()
#@click.argument('query') util = Util(cfg=cfg)
#@click.argument('recipe_id') shopping_list = ShoppingList(id=shopping_id)
#@click.option('--amount', '-a', 'amount', default=1, type=int) if template:
#@click.option('--group', '-g', type=str, default='') loaded_template = cfg.templates(template)
#@click.option('--variable-amount', '--va', 'variable_amount', default=None, type=float) else:
#@click.option('--in-stock', '--is', 'in_stock', default=False) loaded_template = cfg.templates('shopping/remove_product')
#@click.option('--disable-fulfillment', '--df', 'disable_fulfillment', default=False)
#@click.option('--note', '-n', 'note', multiple=True, default='', type=str) remove_product = click.edit(loaded_template.render(), extension='.yml')
#@click.option('--no-edit', '--ne', 'no_edit', default=False) if remove_product is None:
#def add(query, recipe_id, amount, group, variable_amount, in_stock, disable_fulfillment, note, no_edit): return
# logger = logging.getLogger('cli.ingredient.add') parsed_removed_product = util.load_yaml(remove_product)[0]
#
# try: if template == 'debug':
# loaded_template = TEMPLATE_LOADER.get_template('ingredient_add.yml') # TODO: Reserve for meta
# new_ingredient = {} return
# shopping_list.remove_product(parsed_removed_product)
# entity = Entity(name='recipes') except Exception as e:
# recipe = entity.get(id=recipe_id) raise e
#
# if not recipe:
# raise click.BadParameter(message='recipe {id} does not exist', param='recipe_id', # TODO: Figure out why this command is not working for new shopping lists
# param_hint='Use `grocy recipes ls` to get a list of recipes') @shopping.command()
# @click.argument('shopping_id')
# entity = Entity(name='products') @click.option('-t', 'template')
# product = entity.findOne(query) def add_missing_products(shopping_id, template):
# new_ingredient['product_id'] = product['id'] logger = logging.getLogger('cli.shopping.add_missing_products')
# try:
# new_ingredient['amount'] = amount cfg = Configuration()
# new_ingredient['group'] = group cfg.load()
# new_ingredient['variable_amount'] = variable_amount util = Util(cfg=cfg)
# new_ingredient['only_check_single_unit_in_stock'] = "1" if in_stock else "0"
# new_ingredient['not_check_stock_fulfillment'] = "1" if disable_fulfillment else "0" # Validate that shopping list exists
# new_ingredient['note'] = note entity = Entity(name='shopping_lists')
# found_shopping_list = entity.get(id=shopping_id)
# if not no_edit: if found_shopping_list is None:
# new_ingredient = click.edit(loaded_template.render(new_ingredient)) raise Exception('Shopping list does not exist')
#
# parsed_new_ingredient = yaml.safe_load(new_ingredient) shopping_list = ShoppingList(id=shopping_id)
# entity = Entity(name='recipes_pos') shopping_list.add_missing_products()
# #entity.create(parsed_new_ingredient) except Exception as e:
# raise e
# except Exception as e:
# logger.error(e)
# raise e
#
#
#@recipe.command('create')
#@click.pass_context
#def create(ctx):
# logger = logging.getLogger('cli.recipe.create')
#
# try:
# recipe = Entity(name='recipes')
# loaded_template = TEMPLATE_LOADER.get_template('recipe_add.yml')
# new_recipe = click.edit(loaded_template.render())
# if new_recipe is not None:
# parsed_new_recipe = yaml.safe_load(new_recipe)
# parsed_new_recipe['description'] = markdown(parsed_new_recipe['description'])
# recipe.__dict__.update(parsed_new_recipe)
# recipe.create()
# except Exception as e:
# logger.error(e)
# raise e
#
#@main.command() #@main.command()
#@click.pass_context #@click.pass_context

View File

@ -5,6 +5,9 @@ import logging
class ShoppingList(object): class ShoppingList(object):
ADD_PRODUCT_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/add-product' ADD_PRODUCT_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/add-product'
REMOVE_PRODUCT_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/remove-product'
CLEAR_SHOPPING_LIST_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/clear'
ADD_MISSING_PRODUCTS = '{domain}/api/stock/shoppinglist/add-missing-products'
def __init__(self, id=None): def __init__(self, id=None):
self.conf = Configuration() self.conf = Configuration()
@ -28,3 +31,48 @@ class ShoppingList(object):
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
raise e raise e
def remove_product(self, entity):
logger = logging.getLogger('shoppinglist.remove_product')
try:
if self.id is None:
raise Exception('shopping list id is required')
if entity and 'product_id' not in entity:
raise Exception('Must provide a product id')
if entity and 'product_amount' not in entity:
raise Exception('Must provide a product_amount id')
entity['list_id'] = self.id
url = self.REMOVE_PRODUCT_URL_TEMPLATE.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.error(e)
raise e
def clear(self):
logger = logging.getLogger('shoppinglist.clear')
try:
if self.id is None:
raise Exception('shopping list id is required')
entity = {'list_id': self.id}
url = self.CLEAR_SHOPPING_LIST_URL_TEMPLATE.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.eror(e)
raise e
def add_missing_products(self):
logger = logging.getLogger('shoppinglist.add_min_stock')
try:
if self.id is None:
raise Exception('shopping list id is required')
entity = {'list_id': self.id}
url = self.ADD_MISSING_PRODUCTS.format(domain=self.conf.domain)
request = Request('post', url, resource=entity)
return request.send()
except Exception as e:
logger.error(e)
raise e

View File

@ -0,0 +1,2 @@
product_id:
product_amount: