Add get locations and get current quantities API endpoints
This commit is contained in:
parent
fe898cde00
commit
51ebe81bba
69
webapi.py
69
webapi.py
@ -4,21 +4,80 @@ InvMan
|
||||
"""
|
||||
|
||||
# Imports
|
||||
from flask import Flask, request, jsonify, abort
|
||||
from flask import Flask, request, jsonify, abort, g
|
||||
from flask_api import status
|
||||
import psycopg2
|
||||
from psycopg2 import pool
|
||||
from configparser import ConfigParser
|
||||
|
||||
import storage
|
||||
|
||||
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'])
|
||||
|
||||
# @app.route("/api/v1/locations", methods = ["GET"])
|
||||
# def api_get_locations():
|
||||
# return jsonify(storage.get_locations())
|
||||
|
||||
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"])
|
||||
|
Loading…
Reference in New Issue
Block a user