From e964bb7bd56c912b5af52f4f71140d6fc3cbfd46 Mon Sep 17 00:00:00 2001 From: Jadowyne Ulve Date: Sat, 26 Apr 2025 18:53:14 -0500 Subject: [PATCH] recipes_api.addRecipe updated to new schema --- .../database_recipes.cpython-312.pyc | Bin 6300 -> 7607 bytes .../__pycache__/recipes_api.cpython-312.pyc | Bin 17846 -> 18083 bytes scripts/recipes/database_recipes.py | 17 +++++++++++++++++ scripts/recipes/recipes_api.py | 17 ++++++++++++----- scripts/recipes/sql/postRecipe.sql | 4 ++++ 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 scripts/recipes/sql/postRecipe.sql diff --git a/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc b/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc index 832ef5d4d581de65e5a5813ca151924e018e0bc4..5389543a17b2cfa205acc031ac287ac77f097ce1 100644 GIT binary patch delta 750 zcmY+C&ubGw6vuaVcCtGgceBlpHvO?_(rQb<+8X=A8fmE;D=4T4q7(#aH)6E4brTQ5 z1`GC3@nGW|ycDUP1fjO{;7Kqfc=6KGL!wQLlqNPwd+tfLXMs$}KzJtFGGxksbp;3o zLSd!)RE&Gq-RFMxN24gmXdvmg#gB-$5`+OEPh9eVuyxeJ5DKzYJt9(pEe0a&c0j|j zE3+k~9V@QFs(L3<*e7IC>QblE6`dHl-+uqC+X4vffwTkAO zAK#_grk>cQXQ~1s!)v*U?thhek$L%WN00Sx>9LjGwf=4W>>{pVs8=N~N$zT)9nIY0 zU*C!kq$(&hvX-i_V#C<=!uXF+^Skbf8Cx2!G?_J@tVOBzld@2Y4T|g1C4rloEwvtic+@#l)D;ijoqdkd#ykg;J1Mrd)~= zD^5!%WX^jO&IgG#DwmR&$D>@!VsmN;ms^`s30zK))X>JB{{toi(CFsI+r~+RybOi= zd-(M^oF4wReF%iZJFY9|XJ+ju)9DfC95DW*vX|MN@PN73VW8xaRTW@b3GMUg;JAST I&MRxrH%`C51DEC6zUWHI*%mC55ep zHHsZ50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;u+QSSl&nxDM8+0QGdIO%ZcbGr) zxzu6t_DkH=-Ur5td&as&W8FPt^P;i&zR`N$TyejwVOh_ZwyiK}rrf!{)l9}zsiImJ zjjh*UKoii3{NdXKrF9;MbJ#(64XRAk8wAJJ6=O@ zAsU`T2seRCkc)5#;ihoMa|D-qky~Y$9Ep39r_YeMRRd!%e^!09WnuU5qIqN~&$E#3 zd5~4`$=GFSq07BkT{yIqb$B7|@RLG>FEdEj#2iTbMsxSBUK4lIr0uPYeOzkiSPT88 zq8yriui{SQFd&qQy0Apg)G^&JNd1x!63Hm&r(%nhW)=)QvbL}Tu}a$ojd3qpkY~@2 z98M=0qf49KZ`crG%fvJw*dt}gGI0(MMtH9zka5s_7@M<)({!@6Qu=R=OBX^*VcO+1 z)5Ff!>*RY>K+pwP~nq;X$+D+Jg6V_5?`1X;+!}UB@q;5D6q+~FqwiqsYhXUG#v6w zMZJ?!K$<2Zh~OELP_1Nw9G+H=5aec&i1f6Py=dQP; zI~a6L_xH-PLh*f;EX}M*==6N(4{DD;*(YAR-Prw5_diBg$G7Vn9$5xxwp$C`X}F z0+^#Ww>iyL1ZBaT!`%0WyncaDkSxT~T25);f}YtiXqM}sJm<;HrfWN@Sv$40I*qcC z3@C~xAe;CUO#Bw}wEm^3l^cuAN$RKEjlQ+@$H$6ET1kW+Z{N>4=DU%Q#@ zr>2fwWwI*+F9ZciP?C~sbOAg(jDL^rum!9^jv!x zZR#>9{yS6s11_Gg6w)K#JA4=g`6HqONZW&P5N-lNHpUIJsQvE{Bd zR#y$Ivx7Fdo@Z-l#8twU(^IZ;_87hF+NG0Q_!|Aw&Nr~b>b%7 diff --git a/scripts/recipes/database_recipes.py b/scripts/recipes/database_recipes.py index 7581761..06ffcb1 100644 --- a/scripts/recipes/database_recipes.py +++ b/scripts/recipes/database_recipes.py @@ -64,6 +64,23 @@ def getRecipe(site, payload:tuple, convert=True): except (Exception, psycopg2.DatabaseError) as error: raise postsqldb.DatabaseError(error, payload, sql) +def postRecipe(site:str, payload:tuple, convert=True): + database_config = config.config() + record = () + with open("scripts/recipes/sql/postRecipe.sql") as file: + sql = file.read().replace("%%site_name%%", site) + try: + with psycopg2.connect(**database_config) as conn: + with conn.cursor() as cur: + cur.execute(sql, payload) + rows = cur.fetchone() + if rows and convert: + record = postsqldb.tupleDictionaryFactory(cur.description, rows) + elif rows and not convert: + record = rows + return record + except (Exception, psycopg2.DatabaseError) as error: + raise postsqldb.DatabaseError(error, payload, sql) def postRecipeUpdate(site, payload, convert=True): database_config = config.config() diff --git a/scripts/recipes/recipes_api.py b/scripts/recipes/recipes_api.py index da215a3..24b0f00 100644 --- a/scripts/recipes/recipes_api.py +++ b/scripts/recipes/recipes_api.py @@ -7,6 +7,7 @@ import os import postsqldb, webpush from flasgger import swag_from from scripts.recipes import database_recipes +from scripts import postsqldb as db from flask_restx import Api, fields recipes_api = Blueprint('recipes_api', __name__) @@ -93,6 +94,12 @@ def getRecipe(): @recipes_api.route('/recipes/addRecipe', methods=["POST"]) def addRecipe(): + """ post a new recipe into the database by passing a recipe_name and recipe description + --- + responses: + 200: + description: Recipe was added successfully into the system + """ if request.method == "POST": recipe_name = request.get_json()['recipe_name'] recipe_description = request.get_json()['recipe_description'] @@ -100,15 +107,15 @@ def addRecipe(): site_name = session['selected_site'] user_id = session['user_id'] with psycopg2.connect(**database_config) as conn: - recipe = postsqldb.RecipesTable.Payload( + recipe = db.RecipesTable.Payload( name=recipe_name, author=user_id, description=recipe_description ) - recipe = postsqldb.RecipesTable.insert_tuple(conn, site_name, recipe.payload()) - webpush.push_ntfy('New Recipe', f"New Recipe added to {site_name}; {recipe_name}!{recipe_description} http://test.treehousefullofstars.com/recipe/view/{recipe['id']} http://test.treehousefullofstars.com/recipe/edit/{recipe['id']}") - return jsonify({'recipe': recipe, 'error': False, 'message': 'Add Recipe successful!'}) - return jsonify({'recipe': recipe, 'error': True, 'message': 'Add Recipe unsuccessful!'}) + recipe = database_recipes.postRecipe(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}'}) @recipes_api.route('/recipe/getItems', methods=["GET"]) def getItems(): diff --git a/scripts/recipes/sql/postRecipe.sql b/scripts/recipes/sql/postRecipe.sql new file mode 100644 index 0000000..29d5b61 --- /dev/null +++ b/scripts/recipes/sql/postRecipe.sql @@ -0,0 +1,4 @@ +INSERT INTO %%site_name%%_recipes +(name, author, description, creation_date, instructions, picture_path) +VALUES (%s, %s, %s, %s, %s, %s) +RETURNING *; \ No newline at end of file