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 19:22:24 -05:00
|
|
|
from libdb import Session, Location, ProductQuantity, Product, Brand, Unit, Purchase, Use
|
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 19:29:07 -05:00
|
|
|
"""Route to get information about a product"""
|
2020-10-05 01:00:50 -05:00
|
|
|
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-05 19:29:07 -05:00
|
|
|
def api_get_brands():
|
|
|
|
"""Route to get a list of all brands"""
|
2020-10-05 01:00:50 -05:00
|
|
|
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:
|
2020-10-05 19:22:24 -05:00
|
|
|
data = session.query(Brand.name, Brand.description).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 19:22:24 -05:00
|
|
|
@app.route("/api/v1/units", methods=["GET"])
|
2020-10-05 19:29:07 -05:00
|
|
|
def api_get_units():
|
|
|
|
"""Route to get a list of all units"""
|
2020-10-05 19:22:24 -05:00
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = {}
|
|
|
|
for name, description in session.query(Unit.name, Unit.description).all():
|
|
|
|
data[name] = {'description': description}
|
|
|
|
return jsonify(data)
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/unit/name/<search>", methods=["GET"])
|
|
|
|
def api_get_unit_by_name(search):
|
|
|
|
"""Route to get information about a unit"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = session.query(Unit.name, Unit.description).filter(
|
|
|
|
Unit.name == search).one()
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
data2 = {'name': data.name, 'description': data.description}
|
|
|
|
return jsonify(data2)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/purchases", methods=["GET"])
|
2020-10-05 19:29:07 -05:00
|
|
|
def api_get_purchases():
|
|
|
|
"""Route to get a list of all purchases"""
|
2020-10-05 19:22:24 -05:00
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = {}
|
|
|
|
query = session.query(Purchase.id,
|
|
|
|
Purchase.product_upc, Purchase.quantity, Purchase.date,
|
|
|
|
Purchase.location)
|
|
|
|
for purchase_id, upc, quantity, date, location in query:
|
|
|
|
data[purchase_id] = {'upc': upc, 'quantity': quantity,
|
|
|
|
'date': str(date), 'location': location}
|
|
|
|
return jsonify(data)
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/purchase/id/<search>", methods=["GET"])
|
|
|
|
def api_get_purchase_by_id(search):
|
|
|
|
"""Route to get information about a purchase"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = session.query(Purchase.id, Purchase.product_upc, Purchase.quantity,
|
|
|
|
Purchase.date, Purchase.location).filter(Purchase.id == search).one()
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
data2 = {'id': data.id, 'upc': data.product_upc,
|
|
|
|
'quantity': data.quantity, 'date': str(data.date), 'location': data.location}
|
|
|
|
return jsonify(data2)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/uses", methods=["GET"])
|
2020-10-05 19:29:07 -05:00
|
|
|
def api_get_uses():
|
|
|
|
"""Route to get a list of all uses"""
|
2020-10-05 19:22:24 -05:00
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = {}
|
|
|
|
query = session.query(Use.id,
|
|
|
|
Use.product_upc, Use.quantity, Use.date,
|
|
|
|
Use.location)
|
|
|
|
for use_id, upc, quantity, date, location in query:
|
|
|
|
data[use_id] = {'upc': upc, 'quantity': quantity,
|
|
|
|
'date': str(date), 'location': location}
|
|
|
|
return jsonify(data)
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/use/id/<search>", methods=["GET"])
|
|
|
|
def api_get_use_by_id(search):
|
|
|
|
"""Route to get information about a use"""
|
|
|
|
session = Session()
|
|
|
|
try:
|
|
|
|
data = session.query(Use.id, Use.product_upc, Use.quantity,
|
|
|
|
Use.date, Use.location).filter(Use.id == search).one()
|
|
|
|
except NoResultFound:
|
|
|
|
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
|
|
|
|
data2 = {'id': data.id, 'upc': data.product_upc,
|
|
|
|
'quantity': data.quantity, 'date': str(data.date), 'location': data.location}
|
|
|
|
return jsonify(data2)
|
|
|
|
|
|
|
|
|
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 19:22:24 -05:00
|
|
|
newlocation = Location(name=locname, description=locdesc)
|
|
|
|
session.add(newlocation)
|
|
|
|
print("added newlocation")
|
2020-10-05 01:00:50 -05:00
|
|
|
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
|
|
|
|
2020-10-05 19:22:24 -05:00
|
|
|
newbrand = Brand(name=brandname, description=branddesc)
|
|
|
|
session.add(newbrand)
|
|
|
|
print("added newbrand")
|
2020-10-05 01:00:50 -05:00
|
|
|
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 19:22:24 -05:00
|
|
|
newproduct = Product(upc=request.form['upc'], brand=request.form['brand'],
|
|
|
|
name=request.form['name'], size=request.form['size'],
|
|
|
|
sizeunit=request.form['sizeunit'],
|
|
|
|
description=desc)
|
|
|
|
session.add(newproduct)
|
|
|
|
print("added newproduct")
|
2020-10-05 01:00:50 -05:00
|
|
|
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-10-05 19:22:24 -05:00
|
|
|
@app.route("/api/v1/create_unit", methods=["POST"])
|
|
|
|
def api_create_unit():
|
|
|
|
"""Route to create a new unit"""
|
|
|
|
session = Session()
|
|
|
|
if "name" not in request.form:
|
|
|
|
return jsonify({'error': 'NO_NAME_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
else:
|
|
|
|
unitname = request.form['name']
|
|
|
|
if "description" in request.form:
|
|
|
|
unitdesc = request.form['description']
|
|
|
|
else:
|
|
|
|
unitdesc = None
|
|
|
|
|
|
|
|
newunit = Unit(name=unitname, description=unitdesc)
|
|
|
|
session.add(newunit)
|
|
|
|
print("added newunit")
|
|
|
|
session.commit()
|
|
|
|
print("committed")
|
|
|
|
|
|
|
|
return jsonify({'api_endpoints': [f'/api/v1/unit/name/{unitname}']})
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/create_purchase", methods=["POST"])
|
|
|
|
def api_create_purchase():
|
|
|
|
"""Route to create a new purchase"""
|
|
|
|
session = Session()
|
|
|
|
reqfields = ['upc', 'quantity', 'date', 'location']
|
|
|
|
for field in reqfields:
|
|
|
|
if field not in request.form:
|
|
|
|
return jsonify({'error': f'NO_{field.upper()}_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
|
|
newpurchase = Purchase(product_upc=request.form['upc'], quantity=request.form['quantity'],
|
|
|
|
date=request.form['date'], location=request.form['location'])
|
|
|
|
session.add(newpurchase)
|
|
|
|
print("added newpurchase")
|
|
|
|
session.commit()
|
|
|
|
print("committed")
|
|
|
|
|
|
|
|
return jsonify({'api_endpoints': [f'/api/v1/purchase/id/{newpurchase.id}']})
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/v1/create_use", methods=["POST"])
|
|
|
|
def api_create_use():
|
|
|
|
"""Route to create a new use"""
|
|
|
|
session = Session()
|
|
|
|
reqfields = ['upc', 'quantity', 'date', 'location']
|
|
|
|
for field in reqfields:
|
|
|
|
if field not in request.form:
|
|
|
|
return jsonify({'error': f'NO_{field.upper()}_PROVIDED'}), status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
|
|
newuse = Use(product_upc=request.form['upc'], quantity=request.form['quantity'],
|
|
|
|
date=request.form['date'], location=request.form['location'])
|
|
|
|
session.add(newuse)
|
|
|
|
print("added newuse")
|
|
|
|
session.commit()
|
|
|
|
print("committed")
|
|
|
|
|
|
|
|
return jsonify({'api_endpoints': [f'/api/v1/use/id/{newuse.id}']})
|
|
|
|
|
|
|
|
|
2020-09-23 21:31:35 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print("Run with `flask` or a WSGI server!")
|