185 lines
6.4 KiB
Python
185 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
InvMan
|
|
"""
|
|
|
|
# Imports
|
|
from flask import Flask, request, jsonify, abort, g
|
|
from flask_api import status
|
|
import psycopg2
|
|
from psycopg2 import pool
|
|
from configparser import ConfigParser
|
|
|
|
|
|
app = Flask(__name__) # app is the Flask app
|
|
|
|
|
|
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))
|
|
|
|
|
|
app.config['postgreSQL_pool'] = psycopg2.pool.SimpleConnectionPool(1, 20,
|
|
user = config['user'],
|
|
password = config['password'],
|
|
host = config['host'],
|
|
port = config['port'],
|
|
database = config['database'])
|
|
|
|
|
|
def get_db():
|
|
if 'db' not in g:
|
|
g.db = app.config['postgreSQL_pool'].getconn()
|
|
return g.db
|
|
|
|
|
|
@app.teardown_appcontext
|
|
def close_conn(e):
|
|
db = g.pop('db', None)
|
|
if db is not None:
|
|
app.config['postgreSQL_pool'].putconn(db)
|
|
|
|
|
|
@app.route("/api/v1/locations", methods = ["GET"])
|
|
def get_locations():
|
|
db = get_db()
|
|
with db.cursor() as cur:
|
|
cur.execute("SELECT name,description FROM location")
|
|
data = cur.fetchall()
|
|
print(data)
|
|
data2 = {}
|
|
for row in data:
|
|
data2[row[0]] = {}
|
|
data2[row[0]]['description'] = row[1]
|
|
# data2 = []
|
|
# for row in data:
|
|
# data2.append({'name': row[0], 'description': row[1]})
|
|
print(data2)
|
|
return jsonify(data2)
|
|
|
|
|
|
@app.route("/<location>/api/v1/current_quantities", methods = ["GET"])
|
|
def api_get_current_quantities(location):
|
|
db = get_db()
|
|
with db.cursor() as cur:
|
|
cur.execute("SELECT product_upc,quantity FROM product_quantity WHERE location = '{0}'".format(location))
|
|
data = cur.fetchall()
|
|
print(data)
|
|
data2 = {}
|
|
for row in data:
|
|
data2[row[0]] = row[1]
|
|
return jsonify(data2)
|
|
|
|
|
|
# @app.route("/<location>/api/v1/items", methods = ["GET"])
|
|
# def api_get_items(location):
|
|
# try:
|
|
# items = storage.get_items(location)
|
|
# print("Got items at {0}".format(location))
|
|
# return jsonify(items)
|
|
# except KeyError:
|
|
# print("KeyError, returning 404")
|
|
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
# @app.route("/<location>/api/v1/item/<item>", methods = ["GET", "DELETE"])
|
|
# def api_get_item(location, item):
|
|
# if request.method == "GET":
|
|
# try:
|
|
# itemresp = storage.get_items(location)[item.lower()]
|
|
# print("Got {0}".format(item))
|
|
# return jsonify(itemresp)
|
|
# except KeyError:
|
|
# print("KeyError, returning 404")
|
|
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
# elif request.method == "DELETE":
|
|
# try:
|
|
# storage.rm_item(location, item)
|
|
# print("Delete item {0} at {1}".format(item, location))
|
|
# return jsonify({'status': 'success'})
|
|
# except KeyError:
|
|
# print("KeyError, returning 404")
|
|
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
# @app.route("/<location>/api/v1/item/<item>/<brand>", methods = ["GET", "DELETE"])
|
|
# def api_item_brand(location, item, brand):
|
|
# if request.method == "GET":
|
|
# try:
|
|
# itemresp = storage.get_items(location)[item.lower()][brand.lower()]
|
|
# print("Got {0} of brand {1} at {2}".format(item, brand, location))
|
|
# return jsonify(itemresp)
|
|
# except KeyError:
|
|
# print("KeyError, returning 404")
|
|
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
# elif request.method == "DELETE":
|
|
# try:
|
|
# storage.rm_brand(location, item, brand)
|
|
# print("Deleted {0} of brand {1} at {2}".format(item, brand, location))
|
|
# return jsonify({'status': 'success'})
|
|
# except KeyError:
|
|
# print("KeyError, returning 404")
|
|
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
# @app.route("/<location>/api/v1/item/<item>/<brand>/<field>", methods = ["DELETE"])
|
|
# def api_rm_brand_field(location, item, brand, field):
|
|
# try:
|
|
# storage.rm_brand_field(location, item, brand, field)
|
|
# print("Deleted field {0} from brand {1} of item {2} at {3}".format(field, brand, item, location))
|
|
# return jsonify({'status': 'success'})
|
|
# except KeyError:
|
|
# print("KeyError, returning 404")
|
|
# return jsonify({'status': 'error', 'error': 'NOT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
# @app.route("/<location>/api/v1/item/<item>/<brand>/new_brand", methods = ["POST"])
|
|
# def api_new_brand(location, item, brand):
|
|
# try:
|
|
# storage.init_brand(location, item, brand)
|
|
# print("Created brand {0} under item {1} at {2}".format(brand, item, location))
|
|
# return jsonify({'status': 'success'})
|
|
# except storage.AlreadyExistsError:
|
|
# return jsonify({'status': 'error', 'error': 'BRAND_ALREADY_EXISTS'}), status.HTTP_403_FORBIDDEN
|
|
|
|
|
|
# @app.route("/<location>/api/v1/item/<item>/new_item", methods = ["POST"])
|
|
# def api_new_item(location, item):
|
|
# try:
|
|
# storage.init_item(location, item)
|
|
# print("Created item {0} at {1}".format(item, location))
|
|
# return jsonify({'status': 'success'})
|
|
# except storage.AlreadyExistsError:
|
|
# return jsonify({'status': 'error', 'error': 'ITEM_ALREADY_EXISTS'}), status.HTTP_403_FORBIDDEN
|
|
|
|
|
|
# @app.route("/<location>/api/v1/new_location", methods = ["POST"])
|
|
# def api_new_location(location):
|
|
# try:
|
|
# storage.init_location(location)
|
|
# print("Created location {0}".format(location))
|
|
# return jsonify({'status': 'success'})
|
|
# except storage.AlreadyExistsError:
|
|
# return jsonify({'status': 'error', 'error': 'ITEM_ALREADY_EXISTS'}), status.HTTP_403_FORBIDDEN
|
|
|
|
|
|
# @app.route("/<location>/api/v1/item/<item>/<brand>/add_amount/<amount>", methods = ["POST"])
|
|
# def api_add_amount(location, item, brand, amount):
|
|
# storage.increment_amount(location, item, brand, amount)
|
|
# return jsonify({'status': 'success'})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Run with `flask` or a WSGI server!")
|