feat: Added subcommands remove, clear and add missing products for
shpping
This commit is contained in:
parent
0c1b19aa8d
commit
78b9ebb020
167
grocy/cli.py
167
grocy/cli.py
@ -574,6 +574,41 @@ def create(template):
|
||||
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()
|
||||
@click.argument('shopping_id')
|
||||
@click.option('-t', 'template')
|
||||
@ -583,6 +618,13 @@ def add_product(shopping_id, template):
|
||||
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)
|
||||
if 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]
|
||||
|
||||
if template == 'debug':
|
||||
"TODO: Reserve for meta
|
||||
# TODO: Reserve for meta
|
||||
return
|
||||
shopping_list.add_product(parsed_added_product)
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
#
|
||||
#
|
||||
#@main.command()
|
||||
#@main.group()
|
||||
#def ingredient():
|
||||
# pass
|
||||
#
|
||||
#
|
||||
#@ingredient.command('add')
|
||||
#@click.argument('query')
|
||||
#@click.argument('recipe_id')
|
||||
#@click.option('--amount', '-a', 'amount', default=1, type=int)
|
||||
#@click.option('--group', '-g', type=str, default='')
|
||||
#@click.option('--variable-amount', '--va', 'variable_amount', default=None, type=float)
|
||||
#@click.option('--in-stock', '--is', 'in_stock', default=False)
|
||||
#@click.option('--disable-fulfillment', '--df', 'disable_fulfillment', default=False)
|
||||
#@click.option('--note', '-n', 'note', multiple=True, default='', type=str)
|
||||
#@click.option('--no-edit', '--ne', 'no_edit', default=False)
|
||||
#def add(query, recipe_id, amount, group, variable_amount, in_stock, disable_fulfillment, note, no_edit):
|
||||
# logger = logging.getLogger('cli.ingredient.add')
|
||||
#
|
||||
# try:
|
||||
# loaded_template = TEMPLATE_LOADER.get_template('ingredient_add.yml')
|
||||
# new_ingredient = {}
|
||||
#
|
||||
# entity = Entity(name='recipes')
|
||||
# recipe = entity.get(id=recipe_id)
|
||||
#
|
||||
# if not recipe:
|
||||
# raise click.BadParameter(message='recipe {id} does not exist', param='recipe_id',
|
||||
# param_hint='Use `grocy recipes ls` to get a list of recipes')
|
||||
#
|
||||
# entity = Entity(name='products')
|
||||
# product = entity.findOne(query)
|
||||
# new_ingredient['product_id'] = product['id']
|
||||
#
|
||||
# new_ingredient['amount'] = amount
|
||||
# new_ingredient['group'] = group
|
||||
# new_ingredient['variable_amount'] = variable_amount
|
||||
# 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"
|
||||
# new_ingredient['note'] = note
|
||||
#
|
||||
# if not no_edit:
|
||||
# new_ingredient = click.edit(loaded_template.render(new_ingredient))
|
||||
#
|
||||
# parsed_new_ingredient = yaml.safe_load(new_ingredient)
|
||||
# entity = Entity(name='recipes_pos')
|
||||
# #entity.create(parsed_new_ingredient)
|
||||
#
|
||||
# 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
|
||||
#
|
||||
|
||||
@shopping.command()
|
||||
@click.argument('shopping_id')
|
||||
@click.option('-t', 'template')
|
||||
def remove_product(shopping_id, template):
|
||||
logger = logging.getLogger('cli.shopping.remove_product')
|
||||
try:
|
||||
cfg = Configuration()
|
||||
cfg.load()
|
||||
util = Util(cfg=cfg)
|
||||
shopping_list = ShoppingList(id=shopping_id)
|
||||
if template:
|
||||
loaded_template = cfg.templates(template)
|
||||
else:
|
||||
loaded_template = cfg.templates('shopping/remove_product')
|
||||
|
||||
remove_product = click.edit(loaded_template.render(), extension='.yml')
|
||||
if remove_product is None:
|
||||
return
|
||||
parsed_removed_product = util.load_yaml(remove_product)[0]
|
||||
|
||||
if template == 'debug':
|
||||
# TODO: Reserve for meta
|
||||
return
|
||||
shopping_list.remove_product(parsed_removed_product)
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
|
||||
# TODO: Figure out why this command is not working for new shopping lists
|
||||
@shopping.command()
|
||||
@click.argument('shopping_id')
|
||||
@click.option('-t', 'template')
|
||||
def add_missing_products(shopping_id, template):
|
||||
logger = logging.getLogger('cli.shopping.add_missing_products')
|
||||
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.add_missing_products()
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
#@main.command()
|
||||
#@click.pass_context
|
||||
|
@ -5,6 +5,9 @@ import logging
|
||||
|
||||
class ShoppingList(object):
|
||||
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):
|
||||
self.conf = Configuration()
|
||||
@ -28,3 +31,48 @@ class ShoppingList(object):
|
||||
except Exception as e:
|
||||
logger.error(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
|
||||
|
2
templates/shopping/remove_product.yml
Normal file
2
templates/shopping/remove_product.yml
Normal file
@ -0,0 +1,2 @@
|
||||
product_id:
|
||||
product_amount:
|
Loading…
Reference in New Issue
Block a user