feat: Added create and add_product subcommands for shoppinglist cmd
This commit is contained in:
parent
6986dd0968
commit
0c1b19aa8d
76
grocy/cli.py
76
grocy/cli.py
@ -1,5 +1,3 @@
|
|||||||
"""
|
|
||||||
"""
|
|
||||||
import logging
|
import logging
|
||||||
import click
|
import click
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
@ -13,6 +11,7 @@ from grocy.table import Table
|
|||||||
from grocy.entity import Entity
|
from grocy.entity import Entity
|
||||||
from grocy.stock import Stock
|
from grocy.stock import Stock
|
||||||
from grocy.schema import get_schema
|
from grocy.schema import get_schema
|
||||||
|
from grocy.shoppinglist import ShoppingList
|
||||||
import yaml
|
import yaml
|
||||||
from sys import exit
|
from sys import exit
|
||||||
from os import path
|
from os import path
|
||||||
@ -433,7 +432,6 @@ def add_ingredient(recipe_id, template):
|
|||||||
# Add quantity_units to meta
|
# Add quantity_units to meta
|
||||||
ingredient = click.edit(loaded_template.render(), extension='.yml')
|
ingredient = click.edit(loaded_template.render(), extension='.yml')
|
||||||
if ingredient is None:
|
if ingredient is None:
|
||||||
|
|
||||||
return
|
return
|
||||||
parsed_new_ingredient = util.load_yaml(ingredient)[0]
|
parsed_new_ingredient = util.load_yaml(ingredient)[0]
|
||||||
parsed_new_ingredient['recipe_id'] = recipe_id
|
parsed_new_ingredient['recipe_id'] = recipe_id
|
||||||
@ -465,6 +463,7 @@ def remove_ingredient(recipe_id, ingredient_id):
|
|||||||
def shopping(ctx):
|
def shopping(ctx):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@shopping.command()
|
@shopping.command()
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
@click.argument('shopping_id', required=False)
|
@click.argument('shopping_id', required=False)
|
||||||
@ -486,7 +485,7 @@ def view(ctx, shopping_id, template):
|
|||||||
entity = Entity(name='products')
|
entity = Entity(name='products')
|
||||||
products = entity.get()
|
products = entity.get()
|
||||||
products_map = {product['id']: product for product in products}
|
products_map = {product['id']: product for product in products}
|
||||||
for shopping_list_item in shopping_list_items:
|
for shopping_list_item in shopping_list_items:
|
||||||
shopping_product_item = products_map[shopping_list_item['product_id']]
|
shopping_product_item = products_map[shopping_list_item['product_id']]
|
||||||
shopping_list_item['product'] = shopping_product_item
|
shopping_list_item['product'] = shopping_product_item
|
||||||
data['fields']['shopping_list_items'].append(shopping_list_item)
|
data['fields']['shopping_list_items'].append(shopping_list_item)
|
||||||
@ -503,6 +502,7 @@ def view(ctx, shopping_id, template):
|
|||||||
logger.error(e)
|
logger.error(e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
@shopping.command()
|
@shopping.command()
|
||||||
@click.option('--name', '-n', 'name')
|
@click.option('--name', '-n', 'name')
|
||||||
@click.option('-t', 'template')
|
@click.option('-t', 'template')
|
||||||
@ -521,7 +521,7 @@ def list(name, template):
|
|||||||
else:
|
else:
|
||||||
shopping_list_entities = entity.get()
|
shopping_list_entities = entity.get()
|
||||||
|
|
||||||
data = {'fields': { 'shopping_lists': shopping_list_entities }}
|
data = {'fields': {'shopping_lists': shopping_list_entities}}
|
||||||
|
|
||||||
if template:
|
if template:
|
||||||
loaded_template = cfg.templates(template)
|
loaded_template = cfg.templates(template)
|
||||||
@ -534,6 +534,72 @@ def list(name, template):
|
|||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@shopping.command()
|
||||||
|
@click.argument('shopping_id')
|
||||||
|
def browse(shopping_id):
|
||||||
|
logger = logging.getLogger('cli.shopping.browse')
|
||||||
|
try:
|
||||||
|
cfg = Configuration()
|
||||||
|
cfg.load()
|
||||||
|
url = '{domain}/shoppinglist?list={shopping_id}'.format(domain=cfg.domain, shopping_id=shopping_id)
|
||||||
|
click.launch(url, wait=False)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
|
||||||
|
|
||||||
|
@shopping.command()
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
def create(template):
|
||||||
|
logger = logging.getLogger('cli.shopping.create')
|
||||||
|
try:
|
||||||
|
cfg = Configuration()
|
||||||
|
cfg.load()
|
||||||
|
util = Util(cfg=cfg)
|
||||||
|
shopping_lists = Entity(name='shopping_lists')
|
||||||
|
if template:
|
||||||
|
loaded_template = cfg.templates(template)
|
||||||
|
else:
|
||||||
|
loaded_template = cfg.templates('shopping/create')
|
||||||
|
created_shopping_list = click.edit(loaded_template.render(),
|
||||||
|
extension='.yml')
|
||||||
|
if created_shopping_list is None:
|
||||||
|
return
|
||||||
|
parsed_created_shopping_list = util.load_yaml(created_shopping_list)[0]
|
||||||
|
|
||||||
|
if template == 'debug':
|
||||||
|
click.echo(parsed_created_shopping_list)
|
||||||
|
return
|
||||||
|
shopping_lists.create(parsed_created_shopping_list)
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@shopping.command()
|
||||||
|
@click.argument('shopping_id')
|
||||||
|
@click.option('-t', 'template')
|
||||||
|
def add_product(shopping_id, template):
|
||||||
|
logger = logging.getLogger('cli.shopping.add_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/add_product')
|
||||||
|
|
||||||
|
added_product = click.edit(loaded_template.render(), extension='.yml')
|
||||||
|
if added_product is None:
|
||||||
|
return
|
||||||
|
parsed_added_product = util.load_yaml(added_product)[0]
|
||||||
|
|
||||||
|
if template == 'debug':
|
||||||
|
"TODO: Reserve for meta
|
||||||
|
return
|
||||||
|
shopping_list.add_product(parsed_added_product)
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
@ -25,7 +25,6 @@ class Request(object):
|
|||||||
logger = logging.getLogger('request.send')
|
logger = logging.getLogger('request.send')
|
||||||
if self.resource:
|
if self.resource:
|
||||||
r = sess.request(method=self.method, url=self.url, headers=self.headers, json=self.resource)
|
r = sess.request(method=self.method, url=self.url, headers=self.headers, json=self.resource)
|
||||||
print(r.text)
|
|
||||||
else:
|
else:
|
||||||
r = sess.request(method=self.method, url=self.url, headers=self.headers)
|
r = sess.request(method=self.method, url=self.url, headers=self.headers)
|
||||||
|
|
||||||
|
30
grocy/shoppinglist.py
Normal file
30
grocy/shoppinglist.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
from grocy.request import Request
|
||||||
|
from grocy.conf import Configuration
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
class ShoppingList(object):
|
||||||
|
ADD_PRODUCT_URL_TEMPLATE = '{domain}/api/stock/shoppinglist/add-product'
|
||||||
|
|
||||||
|
def __init__(self, id=None):
|
||||||
|
self.conf = Configuration()
|
||||||
|
self.conf.load()
|
||||||
|
self.id = id
|
||||||
|
|
||||||
|
def add_product(self, entity):
|
||||||
|
logger = logging.getLogger('shoppinglist.add_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.ADD_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
|
2
templates/shopping/add_product.yml
Normal file
2
templates/shopping/add_product.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
product_id:
|
||||||
|
product_amount:
|
2
templates/shopping/create.yml
Normal file
2
templates/shopping/create.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
name:
|
||||||
|
description: |
|
Loading…
Reference in New Issue
Block a user