Add some more routes to webapi.py
This commit is contained in:
parent
51ebe81bba
commit
1ce7954476
54
libinv.py
54
libinv.py
@ -1,54 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
# Imports
|
|
||||||
import psycopg2
|
|
||||||
from configparser import ConfigParser
|
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
|
||||||
def __init__(self):
|
|
||||||
filename = "postgres.ini"
|
|
||||||
section = "postgresql"
|
|
||||||
parser = ConfigParser()
|
|
||||||
parser.read(filename)
|
|
||||||
|
|
||||||
config = {}
|
|
||||||
if parser.has_section(section):
|
|
||||||
params = parser.items(section)
|
|
||||||
for param in params:
|
|
||||||
config[param[0]] = param[1]
|
|
||||||
else:
|
|
||||||
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
|
|
||||||
|
|
||||||
self.host = config["host"]
|
|
||||||
self.user = config["user"]
|
|
||||||
self.password = config["password"]
|
|
||||||
self.port = 5432
|
|
||||||
self.dbname = config["database"]
|
|
||||||
self.conn = None
|
|
||||||
|
|
||||||
|
|
||||||
def connect(self):
|
|
||||||
if self.conn is None:
|
|
||||||
try:
|
|
||||||
self.conn = psycopg2.connect(host=self.host, user=self.user, password=self.password, port=self.port, dbname=self.dbname)
|
|
||||||
except psycopg2.DatabaseError as e:
|
|
||||||
print(e)
|
|
||||||
raise e
|
|
||||||
finally:
|
|
||||||
print("Connection opened successfully.")
|
|
||||||
|
|
||||||
|
|
||||||
def runcmd_unsafe(self, cmd):
|
|
||||||
self.connect()
|
|
||||||
with self.conn.cursor() as cur:
|
|
||||||
cur.execute(cmd)
|
|
||||||
return cur.fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
def list_types(self):
|
|
||||||
self.connect()
|
|
||||||
with self.conn.cursor() as cur:
|
|
||||||
cur.execute("SELECT name FROM types")
|
|
||||||
types = [row for row in cur.fetchall()]
|
|
||||||
return types
|
|
@ -1,4 +1,3 @@
|
|||||||
Flask
|
Flask
|
||||||
Flask_API
|
Flask_API
|
||||||
psycopg2
|
psycopg2-binary
|
||||||
sqlalchemy
|
|
||||||
|
73
webapi.py
73
webapi.py
@ -67,49 +67,56 @@ def get_locations():
|
|||||||
return jsonify(data2)
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<location>/api/v1/current_quantities", methods = ["GET"])
|
@app.route("/api/v1/location/<location>/current_quantities", methods = ["GET"])
|
||||||
def api_get_current_quantities(location):
|
def api_get_current_quantities(location):
|
||||||
db = get_db()
|
db = get_db()
|
||||||
with db.cursor() as cur:
|
with db.cursor() as cur:
|
||||||
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = '{0}'".format(location))
|
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = %s", (location,))
|
||||||
data = cur.fetchall()
|
data = cur.fetchall()
|
||||||
print(data)
|
print(data)
|
||||||
data2 = {}
|
if len(data) <= 0:
|
||||||
for row in data:
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
data2[row[0]] = row[1]
|
else:
|
||||||
return jsonify(data2)
|
data2 = {}
|
||||||
|
for row in data:
|
||||||
|
data2[row[0]] = row[1]
|
||||||
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
# @app.route("/<location>/api/v1/items", methods = ["GET"])
|
@app.route("/api/v1/location/<location>/current_quantity/<upc>", methods = ["GET"])
|
||||||
# def api_get_items(location):
|
def api_get_quantity_of_product_in_location(location, upc):
|
||||||
# try:
|
db = get_db()
|
||||||
# items = storage.get_items(location)
|
with db.cursor() as cur:
|
||||||
# print("Got items at {0}".format(location))
|
cur.execute("SELECT quantity FROM product_quantity WHERE location = %s AND product_upc = %s", (location, upc))
|
||||||
# return jsonify(items)
|
data = cur.fetchall()
|
||||||
# except KeyError:
|
if len(data) <= 0:
|
||||||
# print("KeyError, returning 404")
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
else:
|
||||||
|
return jsonify(data[0][0])
|
||||||
|
|
||||||
|
|
||||||
# @app.route("/<location>/api/v1/item/<item>", methods = ["GET", "DELETE"])
|
@app.route("/api/v1/product/<product_upc>", methods = ["GET"])
|
||||||
# def api_get_item(location, item):
|
def api_get_product_information(product_upc):
|
||||||
# if request.method == "GET":
|
db = get_db()
|
||||||
# try:
|
with db.cursor() as cur:
|
||||||
# itemresp = storage.get_items(location)[item.lower()]
|
cur.execute("SELECT upc,brand,name,size,sizeunit,description FROM product WHERE upc = %s", (product_upc,))
|
||||||
# print("Got {0}".format(item))
|
data = cur.fetchall()
|
||||||
# return jsonify(itemresp)
|
if len(data) <= 0:
|
||||||
# except KeyError:
|
return jsonify({'error': 'NO_RESULTS_RETURNED'}), status.HTTP_404_NOT_FOUND
|
||||||
# print("KeyError, returning 404")
|
else:
|
||||||
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
row = data[0]
|
||||||
|
print(row)
|
||||||
|
|
||||||
# elif request.method == "DELETE":
|
data2 = {}
|
||||||
# try:
|
data2['upc'] = row[0]
|
||||||
# storage.rm_item(location, item)
|
data2['brand'] = row[1]
|
||||||
# print("Delete item {0} at {1}".format(item, location))
|
data2['name'] = row[2]
|
||||||
# return jsonify({'status': 'success'})
|
data2['size'] = row[3]
|
||||||
# except KeyError:
|
data2['sizeunit'] = row[4]
|
||||||
# print("KeyError, returning 404")
|
data2['description'] = row[5]
|
||||||
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
||||||
|
print(data2)
|
||||||
|
return jsonify(data2)
|
||||||
|
|
||||||
|
|
||||||
# @app.route("/<location>/api/v1/item/<item>/<brand>", methods = ["GET", "DELETE"])
|
# @app.route("/<location>/api/v1/item/<item>/<brand>", methods = ["GET", "DELETE"])
|
||||||
|
Loading…
Reference in New Issue
Block a user