feat: Added create a recipe command
This commit is contained in:
parent
48896dda23
commit
a1c593b118
@ -66,18 +66,15 @@ class RestService(object):
|
||||
|
||||
return r.content
|
||||
|
||||
def post(self, path, payload):
|
||||
api_url = self.api_url
|
||||
|
||||
if self.api_url.endswith('/'):
|
||||
api_url = api_url[1:]
|
||||
|
||||
if path.startswith('/'):
|
||||
url ='{0}{1}'.format(api_url, path)
|
||||
def post(self, entity_name, entity):
|
||||
if type(entity) is not dict:
|
||||
json_payload = entity.toJSON()
|
||||
else:
|
||||
url = '{0}/{1}'.format(api_url, path)
|
||||
json_payload = entity
|
||||
|
||||
r = requests.post(url, data=json.dumps(payload), headers=self.headers)
|
||||
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 r.raise_for_status():
|
||||
# logger.error(r.raise_for_status())
|
||||
|
22
grocy/cli.py
22
grocy/cli.py
@ -55,7 +55,7 @@ def main(ctx):
|
||||
log_level = 'DEBUG' if 'level' not in log_cfg else log_cfg['level']
|
||||
log_filename = 'log' if 'file_location' not in log_cfg else log_cfg['file_location']
|
||||
logging.basicConfig(level=log_level, filename=log_filename)
|
||||
cfg['logger'] = log_cfg
|
||||
cfg['logger'] = logger
|
||||
|
||||
__validate_token(cfg)
|
||||
ctx.ensure_object(dict)
|
||||
@ -117,7 +117,7 @@ def edit(ctx, recipe_id):
|
||||
if recipe_id:
|
||||
recipe = Recipe(id=recipe_id, **cfg)
|
||||
recipe.get(include_products=True)
|
||||
loaded_template = TEMPLATE_LOADER.get_template('recipe.yml')
|
||||
loaded_template = TEMPLATE_LOADER.get_template('recipe_edit.yml')
|
||||
edited_recipe = click.edit(loaded_template.render(recipe.toJSON()))
|
||||
if edited_recipe is not None:
|
||||
parsed_edited_recipe = yaml.safe_load(edited_recipe)
|
||||
@ -129,6 +129,24 @@ def edit(ctx, recipe_id):
|
||||
logger.error('Could not edit recipe {}'.format(recipe_id))
|
||||
|
||||
|
||||
@recipe.command('create')
|
||||
@click.pass_context
|
||||
def create(ctx):
|
||||
cfg = ctx.obj['cfg']
|
||||
logger = cfg['logger']
|
||||
|
||||
try:
|
||||
recipe = Recipe(**cfg)
|
||||
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)
|
||||
|
||||
#@main.command()
|
||||
#@click.pass_context
|
||||
#def chore(ctx):
|
||||
|
@ -101,6 +101,15 @@ class Recipe(Schema):
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
def create(self):
|
||||
created_recipe = {
|
||||
'description': self.description,
|
||||
'name': self.name,
|
||||
'base_servings': self.base_servings,
|
||||
'desired_servings': self.desired_servings,
|
||||
'not_check_shoppinglist': self.not_check_shoppinglist
|
||||
}
|
||||
self.rest_service.post('recipes', created_recipe)
|
||||
def get(self, include_products=False):
|
||||
try:
|
||||
recipe = self.rest_service.get('recipes', id=self.id)
|
||||
|
6
templates/recipe_add.yml
Normal file
6
templates/recipe_add.yml
Normal file
@ -0,0 +1,6 @@
|
||||
name:
|
||||
description: |
|
||||
|
||||
base_servings:
|
||||
desired_servings:
|
||||
not_check_shoppinglist:
|
Loading…
Reference in New Issue
Block a user