feat: Added get shopping list and shopping lists
style: Removed unused __init__.py file
This commit is contained in:
parent
6689fd2d5d
commit
6986dd0968
@ -1,88 +0,0 @@
|
||||
import requests
|
||||
from dataclasses import asdict
|
||||
import requests_cache
|
||||
import json
|
||||
|
||||
#requests_cache.install_cache('grocy', allowable_methods=('GET',), expire_after=180)
|
||||
class RestService(object):
|
||||
API_KEY_HEADER = 'GROCY-API-KEY'
|
||||
RESOURCE_URL_TEMPLATE = '{api_url}/objects/{entity}/{objectId}'
|
||||
COLLECTION_URL_TEMPLATE = '{api_url}/objects/{entity}'
|
||||
def __init__(self, api_url, json=False):
|
||||
self.api_url = api_url
|
||||
self.headers = {}
|
||||
self.json = json
|
||||
|
||||
def put(self, entity_name, entity, entity_id):
|
||||
if type(entity) is not dict:
|
||||
json_payload = entity.toJSON()
|
||||
else:
|
||||
json_payload = entity
|
||||
|
||||
url = RestService.RESOURCE_URL_TEMPLATE.format(api_url=self.api_url, entity=entity_name, objectId=entity_id)
|
||||
r = requests.put(url, json=json_payload, headers=self.headers)
|
||||
|
||||
if r.raise_for_status():
|
||||
logger.error(r.raise_for_status())
|
||||
raise r.raise_for_status()
|
||||
|
||||
# if self.json:
|
||||
# return r.json()
|
||||
#
|
||||
return r.content
|
||||
|
||||
def get(self, entity_name, id=None):
|
||||
|
||||
if not id:
|
||||
url = RestService.COLLECTION_URL_TEMPLATE.format(api_url=self.api_url, entity=entity_name)
|
||||
else:
|
||||
url = RestService.RESOURCE_URL_TEMPLATE.format(api_url=self.api_url, entity=entity_name, objectId=id)
|
||||
|
||||
r = requests.get(url, headers=self.headers)
|
||||
|
||||
if r.raise_for_status():
|
||||
raise r.raise_for_status()
|
||||
|
||||
if self.json:
|
||||
return r.json()
|
||||
|
||||
return r.content
|
||||
|
||||
def delete(self, path, id):
|
||||
api_url = self.api_url
|
||||
|
||||
if api_url.endswith('/'):
|
||||
url = '{0}{1}'.format(api_url, path[1:])
|
||||
else:
|
||||
url = '{0}/{1}'.format(api_url, path)
|
||||
|
||||
r = requests.get(url, headers=self.headers)
|
||||
if r.raise_for_status():
|
||||
logger.error(r.raise_for_status())
|
||||
raise r.raise_for_status()
|
||||
|
||||
if self.json:
|
||||
return r.json()
|
||||
|
||||
return r.content
|
||||
|
||||
def post(self, entity_name, entity):
|
||||
if type(entity) is not dict:
|
||||
json_payload = entity.toJSON()
|
||||
else:
|
||||
json_payload = entity
|
||||
|
||||
url = RestService.COLLECTION_URL_TEMPLATE.format(api_url=self.api_url, entity=entity_name)
|
||||
|
||||
r = requests.post(url, json=json.dumps(json_payload), headers=self.headers)
|
||||
|
||||
if self.json:
|
||||
return r.json()
|
||||
|
||||
return r.content
|
||||
|
||||
def addHeader(self, type, value):
|
||||
self.headers[type] = value
|
||||
|
||||
def addToken(self, value):
|
||||
self.headers[RestService.API_KEY_HEADER] = value
|
91
grocy/cli.py
91
grocy/cli.py
@ -1,3 +1,6 @@
|
||||
"""
|
||||
"""
|
||||
import logging
|
||||
import click
|
||||
from markdown import markdown
|
||||
from html2text import html2text
|
||||
@ -13,7 +16,6 @@ from grocy.schema import get_schema
|
||||
import yaml
|
||||
from sys import exit
|
||||
from os import path
|
||||
import logging
|
||||
|
||||
|
||||
APP_NAME = 'grocy-cli'
|
||||
@ -431,6 +433,7 @@ def add_ingredient(recipe_id, template):
|
||||
# Add quantity_units to meta
|
||||
ingredient = click.edit(loaded_template.render(), extension='.yml')
|
||||
if ingredient is None:
|
||||
|
||||
return
|
||||
parsed_new_ingredient = util.load_yaml(ingredient)[0]
|
||||
parsed_new_ingredient['recipe_id'] = recipe_id
|
||||
@ -457,17 +460,81 @@ def remove_ingredient(recipe_id, ingredient_id):
|
||||
logger.error(e)
|
||||
raise e
|
||||
|
||||
#@main.command()
|
||||
#def shopping():
|
||||
# logger = logging.getLogger('cli.shopping')
|
||||
# try:
|
||||
# entity = Entity(name='shopping_list')
|
||||
# shopping_list = entity.get()
|
||||
# table = Table(shopping_list=shopping_list)
|
||||
# click.echo(table.shopping_list)
|
||||
# except Exception as e:
|
||||
# logger.error(e)
|
||||
# raise e
|
||||
@main.group()
|
||||
@click.pass_context
|
||||
def shopping(ctx):
|
||||
pass
|
||||
|
||||
@shopping.command()
|
||||
@click.pass_context
|
||||
@click.argument('shopping_id', required=False)
|
||||
@click.option('-t', 'template')
|
||||
def view(ctx, shopping_id, template):
|
||||
logger = logging.getLogger('cli.shopping.view')
|
||||
try:
|
||||
cfg = Configuration()
|
||||
cfg.load()
|
||||
data = {'fields': {'shopping_list_items': []}}
|
||||
if shopping_id:
|
||||
entity = Entity(name='shopping_list')
|
||||
all_shopping_list_items = entity.get()
|
||||
shopping_list_items = [s for s in all_shopping_list_items if s['shopping_list_id'] == shopping_id]
|
||||
|
||||
if shopping_list_items is None:
|
||||
return
|
||||
|
||||
entity = Entity(name='products')
|
||||
products = entity.get()
|
||||
products_map = {product['id']: product for product in products}
|
||||
for shopping_list_item in shopping_list_items:
|
||||
shopping_product_item = products_map[shopping_list_item['product_id']]
|
||||
shopping_list_item['product'] = shopping_product_item
|
||||
data['fields']['shopping_list_items'].append(shopping_list_item)
|
||||
|
||||
if template:
|
||||
loaded_template = cfg.templates(template)
|
||||
else:
|
||||
loaded_template = cfg.templates('shopping/view')
|
||||
|
||||
click.echo(loaded_template.render(grocy=data))
|
||||
else:
|
||||
click.echo(ctx.get_help())
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
raise e
|
||||
|
||||
@shopping.command()
|
||||
@click.option('--name', '-n', 'name')
|
||||
@click.option('-t', 'template')
|
||||
def list(name, template):
|
||||
logger = logging.getLogger('cli.shopping.list')
|
||||
cfg = Configuration()
|
||||
cfg.load()
|
||||
try:
|
||||
entity = Entity(name='shopping_lists')
|
||||
if name:
|
||||
# Convert name args to a single string
|
||||
string_name_arg = ' '.join(name) if type(name) == list else name
|
||||
shopping_list_entities = entity.find({'name': string_name_arg})
|
||||
if len(shopping_list_entities) == 0:
|
||||
return
|
||||
else:
|
||||
shopping_list_entities = entity.get()
|
||||
|
||||
data = {'fields': { 'shopping_lists': shopping_list_entities }}
|
||||
|
||||
if template:
|
||||
loaded_template = cfg.templates(template)
|
||||
else:
|
||||
loaded_template = cfg.templates('shopping/list')
|
||||
|
||||
click.echo(loaded_template.render(grocy=data))
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
raise e
|
||||
|
||||
|
||||
|
||||
#
|
||||
#
|
||||
#@main.command()
|
||||
|
9
templates/shopping/list.yml
Normal file
9
templates/shopping/list.yml
Normal file
@ -0,0 +1,9 @@
|
||||
{% for shopping_list in grocy.fields.shopping_lists %}
|
||||
id: {{ shopping_list.id }}
|
||||
name: {{ shopping_list.name }}
|
||||
description: |-
|
||||
{{ shopping_list.description }}
|
||||
{% if loop.nextitem %}
|
||||
---
|
||||
{%endif%}
|
||||
{%endfor %}
|
8
templates/shopping/view.yml
Normal file
8
templates/shopping/view.yml
Normal file
@ -0,0 +1,8 @@
|
||||
{% for shopping_list_item in grocy.fields.shopping_list_items %}
|
||||
product: {{ shopping_list_item.product.name }}
|
||||
amount: {{ shopping_list_item.amount }}
|
||||
note: {{ shopping_list_item.note }}
|
||||
{% if loop.nextitem %}
|
||||
---
|
||||
{%endif%}
|
||||
{%endfor %}
|
Loading…
Reference in New Issue
Block a user