From 74b05433e0bbdc6bed586f47c864565ac915de3c Mon Sep 17 00:00:00 2001 From: Jadowyne Ulve Date: Sat, 26 Apr 2025 18:43:17 -0500 Subject: [PATCH] recipes_api.getRecipe view updated for new schema --- .../database_recipes.cpython-312.pyc | Bin 4987 -> 6300 bytes .../__pycache__/recipes_api.cpython-312.pyc | Bin 17784 -> 17846 bytes scripts/recipes/database_recipes.py | 18 ++++++++++++ scripts/recipes/recipes_api.py | 15 ++++++---- scripts/recipes/sql/getRecipeByID.sql | 27 ++++++++++++++++++ 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 scripts/recipes/sql/getRecipeByID.sql diff --git a/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc b/scripts/recipes/__pycache__/database_recipes.cpython-312.pyc index 70469287d675707cb9caae060ed45ec6a992f22e..832ef5d4d581de65e5a5813ca151924e018e0bc4 100644 GIT binary patch delta 1061 zcmZ9K&rcIU6vt=wM|Wq-7TVG+rEZ}VA%%z_5H+UpM~go$YK#|%F+sK{C{(9SDrPIj zn0PUUI*EAjV&cKbMZ*P8ise9JOe7pYB~kwa2qcYgabBUp@Fx42_v1VJ_Dy!H>1BuO zi>_;k+nG`Q#z#Hxs=*iY%?+Kz5+C6{$s(4Nlr1^w7?do>Be_rE8HfZ+;gQNCmFviu z5Y^s~Ue)mC^jA&CxOw`gvs%y;{$i0o>$llo{eVCaYjM@JN<+vNU*o5^8|9=7Xi8>b zOPCV2eO{4md6=++IEbD31#eiiQMs6NSO``*#>ke2buO)S@Nz|Q29@Z0IKrNri5nH8_Eu;lo&5?1!ehE#U2{mCxVX&)#b0_^GjTiMI5eTQtmtR0cRWqAm(oO;rE^H~|#^9RR-p zbpbp8=tQdkZa@Pd!Vz^+_)=O8@Bs{f9}wWULSaY>)6*cw4+l*Sq^L!kc#wvk^}9wx zpdiq_29y)pv%Wte}y-pk8?KtU*hvd)42@eV?;gW(! zjt5D>uRtaw$D2tZQu13>sCLQmm{K?@f*%9Up+KTroD299?c!X!oamnZqrVa5PTn#x z{nUL8V~wqOTAa;1QMzE-$--V!JHlgr%P4)Ao%KeIwYExp>_yKy;@d`J7537@y!a0c Cso5w1 delta 166 zcmbPZ_*;$dG%qg~0}$lq@Mf&zn#d=?=rmDXNtGjoxrH%`C50n}C6zUWHI*%mC55ep zHHsZ5!VZ+-sASUQ+*lLG#aOlZHWx1=quS<|+#*by{|n7xX4MCpXS8{qSP~;6=VV3+ zEnPLBKoLl^hz&@524aRHIUw1Q>;jjDZ3G Dr^h7U diff --git a/scripts/recipes/__pycache__/recipes_api.cpython-312.pyc b/scripts/recipes/__pycache__/recipes_api.cpython-312.pyc index 32778920fd4cf7ea61663c81fe16a90205f1ae26..66bf535cae818d51eb92074118985587cf1a8379 100644 GIT binary patch delta 1280 zcmZ{jUrbwN6u|qny+8|ey-?^MM)yIDv}Lvu;v6uk6e;WGri{k@Nt9f;x22~4_BUtVh0gJ9OcHWqR!`KT5h4aJWrd+ zH!pnT?cj}JcSX)If~dTrIb==&GQxWJbQuX(7tpm8Oad{I{zVuA?P|` ztF_c?E+(Yei~?`F4t2bUrD0htjKJF2ak%da3TL?Znnv933FK9{;0*|~T(O|t_8uc- zN)zZcW5*Hnegcs~bRm*(x7Ww>ZS@`?)8Oz8kQBsxKH+t4yrkuPc{49*yKmByMejVK zfLK5UH>7Q_`44Ffl1Sw(xLjfKM3Ta}F} z{z=$o;u4P!CT_uPCYyh<%-uxe!yMa-S=!NcVXNYU?<^%y%`!SDr;?I zM(bwrCFDiDY+e)+77vPfNiV-x>2)NmDPc^!#xAf;Lf)cRz-Mc`@Li)D{#dVoBUCOX zN})cm%IZEL^$N$CYU_maD^_nTSPrGNFErbn-tc>u{;y%)K{EJQ$vXh8$;ZJ)x z9CX&feN|b)O5b7Mw8GQ|XRA1so8Et5+O_GCtM7xhS&6ou*eWg``6@S6`Rj@|9@%2k zme{OJ`Wb;Zm!)$3P`f6oNIxoablpS_nmvrYX|(tzMb~aR_c|d-s3r`+>}v{!sa7_# z9_Ui)SrX1EihL9|CyZN4#)6r6{kv8Uk@YZPgun?|f)06q06pCDzvZSp3$~c94esk7 zO=tQO#isN?wOYUAZL;owF115GiQ*^5=jvVZeju>+6!|Qartn}qHFP9XbWg1*l;tvm z@sV8rke<>;{2TX6@)Z<+H_mSPTdXtNVvZqoq?@-P&C9jT%}=n8D(d4SFd3@BO5YFd#9p0_ ztn!%kUY^j>ny$so>gl=Rw1!mv zedL%1lPBQgsK1$}jMFf1svD>JaHBKj2C!$N&HU diff --git a/scripts/recipes/database_recipes.py b/scripts/recipes/database_recipes.py index 7b3f7bc..7581761 100644 --- a/scripts/recipes/database_recipes.py +++ b/scripts/recipes/database_recipes.py @@ -47,6 +47,24 @@ def getRecipes(site:str, payload:tuple, convert=True): raise postsqldb.DatabaseError(error, payload, sql) return recordset, count +def getRecipe(site, payload:tuple, convert=True): + database_config = config.config() + try: + with psycopg2.connect(**database_config) as conn: + with open(f"scripts/recipes/sql/getRecipeByID.sql", "r+") as file: + sql = file.read().replace("%%site_name%%", site) + with conn.cursor() as cur: + cur.execute(sql, payload) + rows = cur.fetchone() + if rows and convert: + record = postsqldb.tupleDictionaryFactory(cur.description, rows) + if 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() updated = () diff --git a/scripts/recipes/recipes_api.py b/scripts/recipes/recipes_api.py index 2629590..da215a3 100644 --- a/scripts/recipes/recipes_api.py +++ b/scripts/recipes/recipes_api.py @@ -55,7 +55,6 @@ def recipe(id, mode='view'): if mode == "view": return render_template("recipes/recipe_view.html", recipe_id=id, current_site=session['selected_site']) - @recipes_api.route('/recipes/getRecipes', methods=["GET"]) def getRecipes(): """ Get a subquery of recipes from the database by passing a page, limit @@ -77,14 +76,20 @@ def getRecipes(): @recipes_api.route('/recipe/getRecipe', methods=["GET"]) def getRecipe(): + """ Get a query for recipe id from database by passing an id + --- + responses: + 200: + description: id queried successfully! + + """ recipe = {} if request.method == "GET": id = int(request.args.get('id', 1)) - database_config = config() site_name = session['selected_site'] - with psycopg2.connect(**database_config) as conn: - recipe = postsqldb.RecipesTable.getRecipe(conn, site_name, (id,), convert=True) - return jsonify({'recipe': recipe, 'error': False, 'message': 'bleh'}) + recipe = database_recipes.getRecipe(site_name, (id,), convert=True) + return jsonify({'recipe': recipe, 'error': False, 'message': 'Recipe returned successfully!'}) + return jsonify({'recipe': recipe, 'error': True, 'message': f'method {request.method} not allowed'}) @recipes_api.route('/recipes/addRecipe', methods=["POST"]) def addRecipe(): diff --git a/scripts/recipes/sql/getRecipeByID.sql b/scripts/recipes/sql/getRecipeByID.sql new file mode 100644 index 0000000..88982b8 --- /dev/null +++ b/scripts/recipes/sql/getRecipeByID.sql @@ -0,0 +1,27 @@ +WITH passed_id AS (SELECT %s AS passed_id), + cte_recipe_items AS ( + SELECT items.*, + COALESCE(%%site_name%%_items.barcode, items.uuid) AS uuid, + (SELECT COALESCE(row_to_json(units.*), '{}') FROM units WHERE units.id=%%site_name%%_item_info.uom) AS item_uom, + COALESCE(%%site_name%%_items.item_name, items.item_name) AS item_name, + COALESCE(%%site_name%%_items.links, items.links) AS links, + row_to_json(units.*) as uom, + (SELECT COALESCE(array_agg(jsonb_build_object('conversion', conv, 'unit', units)), '{}') + FROM %%site_name%%_conversions conv + LEFT JOIN units ON conv.uom_id = units.id + WHERE conv.item_id = %%site_name%%_items.id) AS conversions + FROM %%site_name%%_recipe_items items + LEFT JOIN %%site_name%%_items ON items.item_id = %%site_name%%_items.id + LEFT JOIN %%site_name%%_item_info ON %%site_name%%_items.item_info_id = %%site_name%%_item_info.id + LEFT JOIN units ON units.id = items.uom + WHERE items.rp_id = (SELECT passed_id FROM passed_id) + ORDER BY items.item_name ASC + ) + +SELECT (SELECT passed_id FROM passed_id) AS passed_id, + %%site_name%%_recipes.*, + logins.username as author, + (SELECT COALESCE(array_agg(row_to_json(ris)), '{}') FROM cte_recipe_items ris) AS recipe_items +FROM %%site_name%%_recipes +JOIN logins ON %%site_name%%_recipes.author = logins.id +WHERE %%site_name%%_recipes.id=(SELECT passed_id FROM passed_id) \ No newline at end of file