recipes_api.deleteRecipeItem updated to new schema
This commit is contained in:
parent
c753321eb6
commit
4aea8c68ff
Binary file not shown.
Binary file not shown.
@ -90,7 +90,7 @@ def getPicturePath(site:str, payload:tuple, convert:bool=True):
|
||||
rows = cur.fetchone()[0]
|
||||
return rows
|
||||
|
||||
def postRecipe(site:str, payload:tuple, convert=True):
|
||||
def postAddRecipe(site:str, payload:tuple, convert:bool=True):
|
||||
database_config = config.config()
|
||||
record = ()
|
||||
with open("scripts/recipes/sql/postRecipe.sql") as file:
|
||||
@ -108,7 +108,7 @@ def postRecipe(site:str, payload:tuple, convert=True):
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
|
||||
def postRecipeItem(site:str, payload:tuple, convert=True):
|
||||
def postAddRecipeItem(site:str, payload:tuple, convert:bool=True):
|
||||
database_config = config.config()
|
||||
record = ()
|
||||
with open("scripts/recipes/sql/postRecipeItem.sql") as file:
|
||||
@ -126,7 +126,7 @@ def postRecipeItem(site:str, payload:tuple, convert=True):
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
|
||||
def postRecipeUpdate(site, payload, convert=True):
|
||||
def postUpdateRecipe(site:str, payload:tuple, convert:bool=True):
|
||||
database_config = config.config()
|
||||
updated = ()
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
@ -142,4 +142,18 @@ def postRecipeUpdate(site, payload, convert=True):
|
||||
updated = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||
elif rows and not convert:
|
||||
updated = rows
|
||||
return updated
|
||||
return updated
|
||||
|
||||
def postDeleteRecipeItem(site:str, payload:tuple, convert:bool=True):
|
||||
database_config = config.config()
|
||||
deleted = ()
|
||||
sql = f"DELETE FROM {site}_recipe_items WHERE id=%s RETURNING *;"
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, payload)
|
||||
rows = cur.fetchone()
|
||||
if rows and convert:
|
||||
deleted = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||
elif rows and not convert:
|
||||
deleted = rows
|
||||
return deleted
|
||||
@ -114,7 +114,7 @@ def addRecipe():
|
||||
author=user_id,
|
||||
description=recipe_description
|
||||
)
|
||||
recipe = database_recipes.postRecipe(site_name, recipe.payload())
|
||||
recipe = database_recipes.postAddRecipe(site_name, recipe.payload())
|
||||
webpush.push_ntfy('New Recipe', f"New Recipe added to {site_name}; {recipe_name}! {recipe_description} \n http://test.treehousefullofstars.com/recipe/view/{recipe['id']} \n http://test.treehousefullofstars.com/recipe/edit/{recipe['id']}")
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'Recipe added successful!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method}'})
|
||||
@ -163,7 +163,7 @@ def postUpdate():
|
||||
recipe_id = int(request.get_json()['recipe_id'])
|
||||
update = request.get_json()['update']
|
||||
site_name = session['selected_site']
|
||||
recipe = database_recipes.postRecipeUpdate(site_name, {'id': recipe_id, 'update': update})
|
||||
recipe = database_recipes.postUpdateRecipe(site_name, {'id': recipe_id, 'update': update})
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'Update of Recipe successful!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': 'Update of Recipe unsuccessful!'})
|
||||
|
||||
@ -189,7 +189,7 @@ def postCustomItem():
|
||||
qty=float(request.get_json()['qty']),
|
||||
links=request.get_json()['links']
|
||||
)
|
||||
database_recipes.postRecipeItem(site_name, recipe_item.payload())
|
||||
database_recipes.postAddRecipeItem(site_name, recipe_item.payload())
|
||||
recipe = database_recipes.getRecipe(site_name, (rp_id, ))
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'Recipe Item was added successful!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} not allowed!'})
|
||||
@ -220,7 +220,7 @@ def postSKUItem():
|
||||
item_id=item['id'],
|
||||
links=item['links']
|
||||
)
|
||||
database_recipes.postRecipeItem(site_name, recipe_item.payload())
|
||||
database_recipes.postAddRecipeItem(site_name, recipe_item.payload())
|
||||
recipe = database_recipes.getRecipe(site_name, (recipe_id, ))
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': 'Recipe Item was added successful!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} is not allowed!'})
|
||||
@ -244,7 +244,7 @@ def uploadImage(recipe_id):
|
||||
file_path = current_app.config['UPLOAD_FOLDER'] + f"/recipes/{file.filename.replace(" ", "_")}"
|
||||
file.save(file_path)
|
||||
site_name = session['selected_site']
|
||||
database_recipes.postRecipeUpdate(site_name, {'id': recipe_id, 'update': {'picture_path': file.filename.replace(" ", "_")}})
|
||||
database_recipes.postUpdateRecipe(site_name, {'id': recipe_id, 'update': {'picture_path': file.filename.replace(" ", "_")}})
|
||||
return jsonify({'error': False, 'message': 'Recipe was updated successfully!'})
|
||||
|
||||
@recipes_api.route('/recipe/getImage/<recipe_id>')
|
||||
@ -267,17 +267,22 @@ def get_image(recipe_id):
|
||||
return send_from_directory('static/pictures/recipes', picture_path)
|
||||
|
||||
@recipes_api.route('/recipe/deleteRecipeItem', methods=["POST"])
|
||||
@login_required
|
||||
def deleteRecipeItem():
|
||||
""" delete recipe item from database by passing the recipe item ID
|
||||
---
|
||||
responses:
|
||||
200:
|
||||
description: recipe item deleted successfully.
|
||||
"""
|
||||
recipe = {}
|
||||
if request.method == "POST":
|
||||
id = int(request.get_json()['id'])
|
||||
database_config = config()
|
||||
site_name = session['selected_site']
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
deleted_item = postsqldb.RecipesTable.delete_item_tuple(conn, site_name, (id, ), convert=True)
|
||||
recipe = postsqldb.RecipesTable.getRecipe(conn, site_name, (int(deleted_item['rp_id']), ), convert=True)
|
||||
deleted_item = database_recipes.postDeleteRecipeItem(site_name, (id, ))
|
||||
recipe = database_recipes.getRecipe(site_name, (int(deleted_item['rp_id']),))
|
||||
return jsonify({'recipe': recipe, 'error': False, 'message': f'Recipe Item {deleted_item['item_name']} was deleted successful!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': 'Recipe Item was not deleted unsuccessful!'})
|
||||
return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} is not allowed!'})
|
||||
|
||||
@recipes_api.route('/recipe/saveRecipeItem', methods=["POST"])
|
||||
def saveRecipeItem():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user