feat: Added create a recipe command

This commit is contained in:
Aerex 2019-05-05 16:06:33 -05:00
parent 48896dda23
commit a1c593b118
5 changed files with 42 additions and 12 deletions

View File

@ -66,18 +66,15 @@ class RestService(object):
return r.content return r.content
def post(self, path, payload): def post(self, entity_name, entity):
api_url = self.api_url if type(entity) is not dict:
json_payload = entity.toJSON()
if self.api_url.endswith('/'):
api_url = api_url[1:]
if path.startswith('/'):
url ='{0}{1}'.format(api_url, path)
else: 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(): #if r.raise_for_status():
# logger.error(r.raise_for_status()) # logger.error(r.raise_for_status())

View File

@ -55,7 +55,7 @@ def main(ctx):
log_level = 'DEBUG' if 'level' not in log_cfg else log_cfg['level'] 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'] log_filename = 'log' if 'file_location' not in log_cfg else log_cfg['file_location']
logging.basicConfig(level=log_level, filename=log_filename) logging.basicConfig(level=log_level, filename=log_filename)
cfg['logger'] = log_cfg cfg['logger'] = logger
__validate_token(cfg) __validate_token(cfg)
ctx.ensure_object(dict) ctx.ensure_object(dict)
@ -117,7 +117,7 @@ def edit(ctx, recipe_id):
if recipe_id: if recipe_id:
recipe = Recipe(id=recipe_id, **cfg) recipe = Recipe(id=recipe_id, **cfg)
recipe.get(include_products=True) 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())) edited_recipe = click.edit(loaded_template.render(recipe.toJSON()))
if edited_recipe is not None: if edited_recipe is not None:
parsed_edited_recipe = yaml.safe_load(edited_recipe) 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)) 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() #@main.command()
#@click.pass_context #@click.pass_context
#def chore(ctx): #def chore(ctx):

View File

@ -101,6 +101,15 @@ class Recipe(Schema):
except Exception as e: except Exception as e:
raise 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): def get(self, include_products=False):
try: try:
recipe = self.rest_service.get('recipes', id=self.id) recipe = self.rest_service.get('recipes', id=self.id)

6
templates/recipe_add.yml Normal file
View File

@ -0,0 +1,6 @@
name:
description: |
base_servings:
desired_servings:
not_check_shoppinglist: