items_API.paginateLoc and Zones updated

This commit is contained in:
Jadowyne Ulve 2025-04-27 17:28:45 -05:00
parent 84544cc48f
commit 6dd33c2922
8 changed files with 184 additions and 40 deletions

View File

@ -128,3 +128,48 @@ def getPrefixes(site:str, payload:tuple, convert:bool=True):
return recordset, count return recordset, count
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
raise postsqldb.DatabaseError(error, payload, sql) raise postsqldb.DatabaseError(error, payload, sql)
def paginateZonesBySku(site: str, payload: tuple, convert=True):
database_config = config.config()
zones, count = (), 0
with open(f"application/items/sql/paginateZonesBySku.sql", "r+") as file:
sql = file.read().replace("%%site_name%%", site)
with open(f"application/items/sql/paginateZonesBySkuCount.sql", "r+") as file:
sql_count = 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.fetchall()
if rows and convert:
zones = [postsqldb.tupleDictionaryFactory(cur.description, row) for row in rows]
elif rows and not convert:
zones = rows
cur.execute(sql_count, payload)
count = cur.fetchone()[0]
return zones, count
except Exception as error:
raise postsqldb.DatabaseError(error, payload, sql)
def paginateLocationsBySkuZone(site: str, payload: tuple, convert=True):
database_config = config.config()
locations, count = (), 0
with open(f"application/items/sql/paginateLocationsBySkuZone.sql", "r+") as file:
sql = file.read().replace("%%site_name%%", site)
with open(f"application/items/sql/paginateLocationsBySkuZoneCount.sql", "r+") as file:
sql_count = 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.fetchall()
if rows and convert:
locations = [postsqldb.tupleDictionaryFactory(cur.description, row) for row in rows]
elif rows and not convert:
locations = rows
cur.execute(sql_count, payload)
count = cur.fetchone()[0]
return locations, count
except Exception as error:
raise postsqldb.DatabaseError(error, payload, sql)

View File

@ -223,56 +223,97 @@ def getModalPrefixes():
return jsonify({"prefixes":recordset, "end":math.ceil(count/limit), "error":False, "message":"items fetched succesfully!"}) return jsonify({"prefixes":recordset, "end":math.ceil(count/limit), "error":False, "message":"items fetched succesfully!"})
return jsonify({"prefixes":recordset, "end":math.ceil(count/limit), "error":True, "message":f"method {request.method} is not allowed!"}) return jsonify({"prefixes":recordset, "end":math.ceil(count/limit), "error":True, "message":f"method {request.method} is not allowed!"})
@items_api.route('/item/getZones', methods=['GET'])
def getZones():
page = int(request.args.get('page', 1))
limit = int(request.args.get('limit', 1))
database_config = config()
site_name = session['selected_site']
zones = []
offset = (page - 1) * limit
payload = (limit, offset)
count = 0
with psycopg2.connect(**database_config) as conn:
zones, count = database.getZonesWithCount(conn, site_name, payload, convert=True)
print(count, len(zones))
return jsonify(zones=zones, endpage=math.ceil(count[0]/limit))
@items_api.route('/item/getZonesBySku', methods=["GET"]) @items_api.route('/item/getZonesBySku', methods=["GET"])
@login_required
def getZonesbySku(): def getZonesbySku():
""" GET zones by sku by passing page, limit, item_id
---
parameters:
- in: query
name: page
schema:
type: integer
minimum: 1
default: 1
description: page of the records to GET
- in: query
name: limit
schema:
type: integer
minimum: 1
default: 10
description: number of records to grab from the system
- in: query
name: item_id
schema:
type: integer
minimum: 1
default: 1
description: item_id to pull zones for
responses:
200:
description: Zones received successfully.
"""
zones, count = [], 0
if request.method == "GET": if request.method == "GET":
page = int(request.args.get('page', 1)) page = int(request.args.get('page', 1))
limit = int(request.args.get('limit', 1)) limit = int(request.args.get('limit', 10))
item_id = int(request.args.get('item_id')) item_id = int(request.args.get('item_id'))
database_config = config()
site_name = session['selected_site'] site_name = session['selected_site']
zones = []
offset = (page - 1) * limit offset = (page - 1) * limit
payload = (item_id, limit, offset) zones, count = database_items.paginateZonesBySku(site_name, (item_id, limit, offset))
count = 0 return jsonify({'zones': zones, 'endpage': math.ceil(count/limit), 'error':False, 'message': f''})
with psycopg2.connect(**database_config) as conn: return jsonify({'zones': zones, 'endpage': math.ceil(count/limit), 'error':False, 'message': f'method {request.method} not allowed.'})
zones, count = db.ZonesTable.paginateZonesBySku(conn, site_name, payload)
print(zones, count)
return jsonify(zones=zones, endpage=math.ceil(count/limit))
@items_api.route('/item/getLocationsBySkuZone', methods=['get']) @items_api.route('/item/getLocationsBySkuZone', methods=['get'])
@login_required
def getLocationsBySkuZone(): def getLocationsBySkuZone():
""" GET locations by sku by passing page, limit, item_id, zone_id
---
parameters:
- in: query
name: page
schema:
type: integer
minimum: 1
default: 1
description: page of the records to GET
- in: query
name: limit
schema:
type: integer
minimum: 1
default: 10
description: number of records to grab from the system
- in: query
name: item_id
schema:
type: integer
minimum: 1
default: 1
description: item_id to pull locations for zone_id
- in: query
name: zone_id
schema:
type: integer
minimum: 1
default: 1
description: zone_id to pull locations for item_id
responses:
200:
description: Zones received successfully.
"""
locations, count = [], 0
if request.method == "GET":
zone_id = int(request.args.get('zone_id', 1)) zone_id = int(request.args.get('zone_id', 1))
part_id = int(request.args.get('part_id', 1)) part_id = int(request.args.get('part_id', 1))
page = int(request.args.get('page', 1)) page = int(request.args.get('page', 1))
limit = int(request.args.get('limit', 1)) limit = int(request.args.get('limit', 1))
offset = (page-1)*limit offset = (page-1)*limit
database_config = config()
site_name = session['selected_site'] site_name = session['selected_site']
locations = [] locations, count = database_items.paginateLocationsBySkuZone(site_name, (part_id, zone_id, limit, offset))
count=0 return jsonify({'locations': locations, 'endpage': math.ceil(count/limit), 'error': False, 'message': f''})
with psycopg2.connect(**database_config) as conn: return jsonify({'locations': locations, 'endpage': math.ceil(count/limit), 'error': True, 'message': f'method {request.method} is not allowed.'})
payload = (part_id, zone_id, limit, offset)
locations, count = db.LocationsTable.paginateLocationsBySkuZone(conn, site_name, payload)
return jsonify(locations=locations, endpage=math.ceil(count/limit))
@items_api.route('/item/getLocations', methods=['get']) @items_api.route('/item/getLocations', methods=['get'])

View File

@ -0,0 +1,15 @@
WITH passed_id AS (SELECT %s AS passed_id),
cte_item_locations AS (
SELECT DISTINCT ils.location_id FROM %%site_name%%_item_locations ils
WHERE ils.part_id = (SELECT passed_id FROM passed_id)
),
cte_locations AS (
SELECT DISTINCT locations.zone_id FROM %%site_name%%_locations locations
WHERE locations.id IN (SELECT location_id FROM cte_item_locations)
)
SELECT DISTINCT location.* FROM cte_item_locations cil
JOIN %%site_name%%_locations location ON cil.location_id = location.id
WHERE location.zone_id = %s
LIMIT %s OFFSET %s;

View File

@ -0,0 +1,15 @@
WITH passed_id AS (SELECT %s AS passed_id),
cte_item_locations AS (
SELECT DISTINCT ils.location_id FROM %%site_name%%_item_locations ils
WHERE ils.part_id = (SELECT passed_id FROM passed_id)
),
cte_locations AS (
SELECT DISTINCT locations.zone_id FROM %%site_name%%_locations locations
WHERE locations.id IN (SELECT location_id FROM cte_item_locations)
)
SELECT COUNT(DISTINCT location.*) FROM cte_item_locations cil
JOIN %%site_name%%_locations location ON cil.location_id = location.id
WHERE location.zone_id = %s
LIMIT %s OFFSET %s;

View File

@ -0,0 +1,15 @@
WITH passed_id AS (SELECT %s AS passed_id),
cte_item_locations AS (
SELECT DISTINCT ils.location_id FROM %%site_name%%_item_locations ils
WHERE ils.part_id = (SELECT passed_id FROM passed_id)
),
cte_locations AS (
SELECT DISTINCT locations.zone_id FROM %%site_name%%_locations locations
WHERE locations.id IN (SELECT location_id FROM cte_item_locations)
)
SELECT DISTINCT zone.* FROM cte_locations cil
JOIN %%site_name%%_zones zone ON cil.zone_id = zone.id
LIMIT %s OFFSET %s;

View File

@ -0,0 +1,13 @@
WITH passed_id AS (SELECT %s AS passed_id),
cte_item_locations AS (
SELECT DISTINCT ils.location_id FROM %%site_name%%_item_locations ils
WHERE ils.part_id = (SELECT passed_id FROM passed_id)
),
cte_locations AS (
SELECT DISTINCT locations.zone_id FROM %%site_name%%_locations locations
WHERE locations.id IN (SELECT location_id FROM cte_item_locations)
)
SELECT COUNT(DISTINCT zone.*) FROM cte_locations cil
JOIN %%site_name%%_zones zone ON cil.zone_id = zone.id
LIMIT %s OFFSET %s;