2020-09-23 21:31:35 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2020-10-04 11:20:08 -05:00
|
|
|
InvMan Web API
|
|
|
|
|
|
|
|
Web API for InvMan
|
2020-09-23 21:31:35 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Imports
|
2020-10-05 01:00:50 -05:00
|
|
|
from flask import Flask, jsonify, request
|
2020-09-23 21:31:35 -05:00
|
|
|
from flask_api import status
|
|
|
|
|
2020-10-04 21:12:09 -05:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2020-10-05 01:00:50 -05:00
|
|
|
import sqlalchemy
|
2020-10-05 01:07:22 -05:00
|
|
|
from libdb import Session, Location, ProductQuantity, Product, Brand
|
2020-09-23 21:31:35 -05:00
|
|
|
|
2020-10-01 21:52:59 -05:00
|
|
|
|
2020-10-04 21:12:09 -05:00
|
|
|
app = Flask(__name__) # app is the Flask app
|
2020-10-01 21:52:59 -05:00
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/locations", methods=["GET"])
|
2020-10-01 21:52:59 -05:00
|
|
|
def get_locations():
|
2020-10-04 21:12:09 -05:00
|
|
|
"""Route to get locations"""
|
|
|
|
session = Session()
|
|
|
|
data = {}
|
|
|
|
try:
|
2020-10-05 01:07:22 -05:00
|
|
|
for name, desc in session.query(Location.name, Location.description):
|
2020-10-04 21:12:09 -05:00
|
|
|
data[name] = {'description': desc}
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
return jsonify(data)
|
2020-10-02 22:16:01 -05:00
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/location/name/<search>", methods=["GET"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_get_location_information(search):
|
2020-10-04 21:12:09 -05:00
|
|
|
"""Route to get information about a location"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = session.query(Location.name, Location.description). \
|
2020-10-05 01:07:22 -05:00
|
|
|
filter(Location.name == search).one()
|
2020-10-04 21:12:09 -05:00
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
data2 = {}
|
|
|
|
data2['name'] = data.name
|
|
|
|
data2['description'] = data.description
|
|
|
|
return jsonify(data2)
|
2020-10-01 21:52:59 -05:00
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/location/<location>/quantities", methods=["GET"])
|
2020-10-01 21:52:59 -05:00
|
|
|
def api_get_current_quantities(location):
|
2020-10-04 21:12:09 -05:00
|
|
|
"""Route to get quantities in a location"""
|
|
|
|
session = Session()
|
|
|
|
data = {}
|
|
|
|
try:
|
2020-10-05 01:07:22 -05:00
|
|
|
for upc, quantity in session.query(ProductQuantity.product_upc, ProductQuantity.quantity) \
|
|
|
|
.filter(ProductQuantity.location == location).all():
|
2020-10-04 21:12:09 -05:00
|
|
|
data[upc] = quantity
|
|
|
|
return jsonify(data)
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/location/<location>/quantity/<searchmethod>/<search>", methods=["GET"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_get_quantity_of_product_in_location(location, searchmethod, search):
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to get the quantity of a product at a location"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
if searchmethod == 'upc':
|
|
|
|
data = session.query(ProductQuantity.quantity).filter(
|
2020-10-05 01:07:22 -05:00
|
|
|
ProductQuantity.location == location,
|
|
|
|
ProductQuantity.product_upc == search).one()
|
2020-10-05 01:00:50 -05:00
|
|
|
return jsonify(data[0])
|
|
|
|
elif searchmethod == 'name':
|
|
|
|
data = session.query(ProductQuantity.quantity).filter(
|
2020-10-05 01:07:22 -05:00
|
|
|
ProductQuantity.location == location,
|
|
|
|
ProductQuantity.name == search).one()
|
2020-10-05 01:00:50 -05:00
|
|
|
return jsonify(data[0])
|
2020-10-02 22:16:01 -05:00
|
|
|
else:
|
|
|
|
print("invalid search method, sending 400")
|
2020-10-05 01:00:50 -05:00
|
|
|
return jsonify({'error': 'INVALID_SEARCH_METHOD'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
2020-10-02 22:16:01 -05:00
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/products", methods=["GET"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_get_products():
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to get a list of products"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = {}
|
|
|
|
for upc, brand, name, size, sizeunit, description in session.query(
|
2020-10-05 01:07:22 -05:00
|
|
|
Product.upc, Product.brand, Product.name,
|
|
|
|
Product.size, Product.sizeunit, Product.description).all():
|
2020-10-05 01:00:50 -05:00
|
|
|
data[upc] = {'brand': brand, 'name': name, 'size': size,
|
|
|
|
'sizeunit': sizeunit, 'description': description}
|
|
|
|
return jsonify(data)
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
2020-10-04 21:12:09 -05:00
|
|
|
|
2020-10-02 22:16:01 -05:00
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/product/<searchmethod>/<search>", methods=["GET"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_get_product_information(searchmethod, search):
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to get a information about a product"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
if searchmethod == 'name':
|
|
|
|
result = session.query(Product.upc, Product.brand, Product.name,
|
2020-10-05 01:07:22 -05:00
|
|
|
Product.size, Product.sizeunit, Product.description).filter(
|
|
|
|
Product.name == search
|
|
|
|
).one()
|
2020-10-05 01:00:50 -05:00
|
|
|
data = {'upc': result[0], 'brand': result[1], 'name': result[2], 'size': result[3],
|
|
|
|
'sizeunit': result[4], 'description': result[5]}
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
if searchmethod == 'upc':
|
|
|
|
result = session.query(Product.upc, Product.brand, Product.name,
|
2020-10-05 01:07:22 -05:00
|
|
|
Product.size, Product.sizeunit, Product.description).filter(
|
|
|
|
Product.upc == search
|
|
|
|
).one()
|
2020-10-05 01:00:50 -05:00
|
|
|
data = {'upc': result[0], 'brand': result[1], 'name': result[2], 'size': result[3],
|
|
|
|
'sizeunit': result[4], 'description': result[5]}
|
|
|
|
return jsonify(data)
|
|
|
|
|
2020-10-02 22:16:01 -05:00
|
|
|
else:
|
|
|
|
print("invalid search method, sending 400")
|
2020-10-05 01:00:50 -05:00
|
|
|
return jsonify({'error': 'INVALID_SEARCH_METHOD'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
2020-10-02 22:16:01 -05:00
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/brands", methods=["GET"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_list_brands():
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to list all brands"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = {}
|
|
|
|
for name, description in session.query(Brand.name, Brand.description).all():
|
|
|
|
data[name] = {'description': description}
|
|
|
|
return jsonify(data)
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
2020-10-02 08:52:58 -05:00
|
|
|
|
2020-10-02 22:16:01 -05:00
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/brand/name/<search>", methods=["GET"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_get_brand_by_name(search):
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to get information about a location"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = session.query(Brand.name, Brand.description). \
|
2020-10-05 01:07:22 -05:00
|
|
|
filter(Brand.name == search).one()
|
2020-10-05 01:00:50 -05:00
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
data2 = {'name': data.name, 'description': data.description}
|
|
|
|
return jsonify(data2)
|
2020-09-28 21:27:08 -05:00
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/create_location", methods=["POST"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_create_location():
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to create a new location"""
|
|
|
|
session = Session()
|
|
|
|
if "name" not in request.form:
|
|
|
|
return jsonify({'error': 'NO_NAME_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
else:
|
|
|
|
locname = request.form['name']
|
|
|
|
if "description" in request.form:
|
|
|
|
locdesc = request.form['description']
|
|
|
|
else:
|
|
|
|
locdesc = None
|
2020-10-05 01:07:22 -05:00
|
|
|
|
2020-10-05 01:00:50 -05:00
|
|
|
ins = sqlalchemy.insert(Location).values(name=locname, description=locdesc)
|
|
|
|
print(f"ins is {ins}")
|
|
|
|
session.execute(ins)
|
|
|
|
print("executed ins")
|
|
|
|
session.commit()
|
|
|
|
print("committed")
|
|
|
|
|
|
|
|
return jsonify({'api_endpoint': f'/api/v1/location/name/{locname}'})
|
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/create_brand", methods=["POST"])
|
2020-10-02 22:16:01 -05:00
|
|
|
def api_create_brand():
|
2020-10-05 01:00:50 -05:00
|
|
|
"""Route to create a new brand"""
|
|
|
|
session = Session()
|
|
|
|
if "name" not in request.form:
|
|
|
|
return jsonify({'error': 'NO_NAME_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
else:
|
|
|
|
brandname = request.form['name']
|
|
|
|
if "description" in request.form:
|
|
|
|
branddesc = request.form['description']
|
|
|
|
else:
|
|
|
|
branddesc = None
|
2020-10-05 01:07:22 -05:00
|
|
|
|
|
|
|
ins = sqlalchemy.insert(Brand).values(
|
|
|
|
name=brandname, description=branddesc)
|
2020-10-05 01:00:50 -05:00
|
|
|
print(f"ins is {ins}")
|
|
|
|
session.execute(ins)
|
|
|
|
print("executed ins")
|
|
|
|
session.commit()
|
|
|
|
print("committed")
|
|
|
|
|
|
|
|
return jsonify({'api_endpoints': [f'/api/v1/brand/name/{brandname}']})
|
|
|
|
|
|
|
|
|
2020-10-05 01:07:22 -05:00
|
|
|
@app.route("/api/v1/create_product", methods=["POST"])
|
2020-10-05 01:00:50 -05:00
|
|
|
def api_create_product():
|
|
|
|
"""Route to create a new product"""
|
|
|
|
session = Session()
|
|
|
|
reqfields = ['upc', 'brand', 'name', 'size', 'sizeunit']
|
|
|
|
for field in reqfields:
|
|
|
|
if field not in request.form:
|
|
|
|
return jsonify({'error': f'NO_{field.upper()}_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
if "description" in request.form:
|
|
|
|
desc = request.form['description']
|
|
|
|
else:
|
|
|
|
desc = None
|
2020-10-05 01:07:22 -05:00
|
|
|
|
2020-10-05 01:00:50 -05:00
|
|
|
ins = sqlalchemy.insert(Product).values(upc=request.form['upc'], brand=request.form['brand'],
|
2020-10-05 01:07:22 -05:00
|
|
|
name=request.form['name'], size=request.form['size'],
|
|
|
|
sizeunit=request.form['sizeunit'],
|
|
|
|
description=desc)
|
2020-10-05 01:00:50 -05:00
|
|
|
print(f"ins is {ins}")
|
|
|
|
session.execute(ins)
|
|
|
|
print("executed ins")
|
|
|
|
session.commit()
|
|
|
|
print("committed")
|
|
|
|
|
|
|
|
return jsonify({'api_endpoints': [f'/api/v1/product/upc/{request.form["upc"]}',
|
|
|
|
f'/api/v1/product/name/{request.form["name"]}']})
|
2020-09-28 21:27:08 -05:00
|
|
|
|
|
|
|
|
2020-09-23 21:31:35 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print("Run with `flask` or a WSGI server!")
|