2019-10-29 23:48:29 -05:00
|
|
|
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'
|
2019-10-30 00:32:21 -05:00
|
|
|
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'
|
2019-10-29 23:48:29 -05:00
|
|
|
|
|
|
|
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
|
2019-10-30 00:32:21 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-11-02 00:41:09 -05:00
|
|
|
def add_missing_products(self, recipe_id=None):
|
2019-10-30 00:32:21 -05:00
|
|
|
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
|