items_API.addPrefix migrated to new schema
This commit is contained in:
parent
a2c26fd6ba
commit
1a1ffddddd
@ -208,6 +208,37 @@ def getZone(site:str, payload:tuple, convert:bool=True):
|
|||||||
except Exception as error:
|
except Exception as error:
|
||||||
raise postsqldb.DatabaseError(error, payload, sql)
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
|
def getItemInfoTuple(site:str, payload:tuple, convert=True):
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
conn (_type_): _description_
|
||||||
|
site (_type_): _description_
|
||||||
|
payload (_type_): (item_info_id,)
|
||||||
|
convert (bool, optional): _description_. Defaults to True.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
DatabaseError: _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: _description_
|
||||||
|
"""
|
||||||
|
selected = ()
|
||||||
|
database_config = config.config()
|
||||||
|
sql = f"SELECT * FROM {site}_item_info WHERE id=%s;"
|
||||||
|
try:
|
||||||
|
with psycopg2.connect(**database_config) as conn:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(sql, payload)
|
||||||
|
rows = cur.fetchone()
|
||||||
|
if rows and convert:
|
||||||
|
selected = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||||
|
elif rows and not convert:
|
||||||
|
selected = rows
|
||||||
|
return selected
|
||||||
|
except Exception as error:
|
||||||
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
def selectItemLocationsTuple(site_name, payload, convert=True):
|
def selectItemLocationsTuple(site_name, payload, convert=True):
|
||||||
"""select a single tuple from ItemLocations table for site_name
|
"""select a single tuple from ItemLocations table for site_name
|
||||||
|
|
||||||
@ -826,11 +857,56 @@ def updateConversionTuple(site:str, payload: dict, convert=True, conn=None):
|
|||||||
if self_conn:
|
if self_conn:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
return updated
|
return updated
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
raise postsqldb.DatabaseError(error, payload, sql)
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
|
def updateItemInfoTuple(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}_item_info 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 = ()
|
||||||
|
|||||||
@ -786,19 +786,35 @@ def updateConversion():
|
|||||||
|
|
||||||
@items_api.route('/addPrefix', methods=['POST'])
|
@items_api.route('/addPrefix', methods=['POST'])
|
||||||
def addPrefix():
|
def addPrefix():
|
||||||
|
""" POST add prefix to the system given a item_info_id and prefix_id
|
||||||
|
---
|
||||||
|
parameters:
|
||||||
|
- in: header
|
||||||
|
name: item_info_id
|
||||||
|
schema:
|
||||||
|
type: integer
|
||||||
|
default: 1
|
||||||
|
required: true
|
||||||
|
description: item_info_id to be updated
|
||||||
|
- in: header
|
||||||
|
name: prefix_id
|
||||||
|
schema:
|
||||||
|
type: integer
|
||||||
|
default: 1
|
||||||
|
required: true
|
||||||
|
description: prefix_id to be added
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: conversion updated successfully.
|
||||||
|
"""
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
item_info_id = request.get_json()['parent_id']
|
item_info_id = request.get_json()['parent_id']
|
||||||
prefix_id = request.get_json()['prefix_id']
|
prefix_id = request.get_json()['prefix_id']
|
||||||
print(item_info_id)
|
|
||||||
print(prefix_id)
|
|
||||||
database_config = config()
|
|
||||||
site_name = session['selected_site']
|
site_name = session['selected_site']
|
||||||
with psycopg2.connect(**database_config) as conn:
|
prefixes = database_items.getItemInfoTuple(site_name, (item_info_id,))['prefixes']
|
||||||
prefixes = db.ItemInfoTable.select_tuple(conn, site_name, (item_info_id,))['prefixes']
|
prefixes.append(prefix_id)
|
||||||
print(prefixes)
|
database_items.updateItemInfoTuple(site_name, {'id': item_info_id, 'update':{'prefixes': prefixes}})
|
||||||
prefixes.append(prefix_id)
|
return jsonify(error=False, message="Prefix was added successfully")
|
||||||
db.ItemInfoTable.update_tuple(conn, site_name, {'id': item_info_id, 'update':{'prefixes': prefixes}})
|
|
||||||
return jsonify(error=False, message="Prefix was added successfully")
|
|
||||||
return jsonify(error=True, message="Unable to save this prefix, ERROR!")
|
return jsonify(error=True, message="Unable to save this prefix, ERROR!")
|
||||||
|
|
||||||
@items_api.route('/deletePrefix', methods=['POST'])
|
@items_api.route('/deletePrefix', methods=['POST'])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user