items_API.updateConversion migrated to new schema
This commit is contained in:
parent
db661111e0
commit
a2c26fd6ba
@ -787,6 +787,50 @@ def deleteConversionTuple(site_name: str, payload: tuple, convert=True, conn=Non
|
|||||||
except Exception as error:
|
except Exception as error:
|
||||||
raise postsqldb.DatabaseError(error, payload, sql)
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
|
def updateConversionTuple(site:str, payload: dict, convert=True, conn=None):
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
conn (_T_connector@connect): Postgresql Connector
|
||||||
|
site (str):
|
||||||
|
table (str):
|
||||||
|
payload (dict): {'id': row_id, 'update': {... column_to_update: value_to_update_to...}}
|
||||||
|
convert (bool, optional): determines if to return tuple as dictionary. Defaults to False.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
DatabaseError:
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple or dict: updated tuple
|
||||||
|
"""
|
||||||
|
updated = ()
|
||||||
|
self_conn = False
|
||||||
|
set_clause, values = postsqldb.updateStringFactory(payload['update'])
|
||||||
|
values.append(payload['id'])
|
||||||
|
sql = f"UPDATE {site}_conversions SET {set_clause} WHERE id=%s RETURNING *;"
|
||||||
|
try:
|
||||||
|
if not conn:
|
||||||
|
database_config = config.config()
|
||||||
|
conn = psycopg2.connect(**database_config)
|
||||||
|
conn.autocommit = False
|
||||||
|
self_conn = True
|
||||||
|
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(sql, values)
|
||||||
|
rows = cur.fetchone()
|
||||||
|
if rows and convert:
|
||||||
|
updated = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||||
|
elif rows and not convert:
|
||||||
|
updated = rows
|
||||||
|
|
||||||
|
if self_conn:
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return updated
|
||||||
|
except Exception as error:
|
||||||
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
def postUpdateItemLocation(site, payload, conn=None):
|
def postUpdateItemLocation(site, payload, conn=None):
|
||||||
|
|
||||||
item_location = ()
|
item_location = ()
|
||||||
|
|||||||
@ -755,15 +755,33 @@ def deleteConversion():
|
|||||||
|
|
||||||
@items_api.route('/updateConversion', methods=['POST'])
|
@items_api.route('/updateConversion', methods=['POST'])
|
||||||
def updateConversion():
|
def updateConversion():
|
||||||
|
""" POST update conversion to the system given a conversion_id, update dictionary
|
||||||
|
---
|
||||||
|
parameters:
|
||||||
|
- in: header
|
||||||
|
name: conversion_id
|
||||||
|
schema:
|
||||||
|
type: integer
|
||||||
|
default: 1
|
||||||
|
required: true
|
||||||
|
description: conversion_id to be deleted
|
||||||
|
- in: header
|
||||||
|
name: update
|
||||||
|
schema:
|
||||||
|
type: dict
|
||||||
|
default: 1
|
||||||
|
required: true
|
||||||
|
description: data to update in key=column, value=update_data
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: conversion updated successfully.
|
||||||
|
"""
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
conversion_id = request.get_json()['conversion_id']
|
conversion_id = request.get_json()['conversion_id']
|
||||||
update_dictionary = request.get_json()['update']
|
update_dictionary = request.get_json()['update']
|
||||||
|
|
||||||
database_config = config()
|
|
||||||
site_name = session['selected_site']
|
site_name = session['selected_site']
|
||||||
with psycopg2.connect(**database_config) as conn:
|
database_items.updateConversionTuple(site_name, {'id': conversion_id, 'update': update_dictionary})
|
||||||
db.ConversionsTable.update_item_tuple(conn, site_name, {'id': conversion_id, 'update': update_dictionary})
|
return jsonify(error=False, message="Conversion was updated successfully")
|
||||||
return jsonify(error=False, message="Conversion was updated successfully")
|
|
||||||
return jsonify(error=True, message="Unable to save this conversion, ERROR!")
|
return jsonify(error=True, message="Unable to save this conversion, ERROR!")
|
||||||
|
|
||||||
@items_api.route('/addPrefix', methods=['POST'])
|
@items_api.route('/addPrefix', methods=['POST'])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user