Add invman.py and storage.py
This commit is contained in:
parent
007c4ec239
commit
32798b1e0a
80
invman.py
Normal file
80
invman.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
InvMan
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
from flask import Flask, request, jsonify, abort
|
||||||
|
from flask_api import status
|
||||||
|
|
||||||
|
import storage
|
||||||
|
|
||||||
|
app = Flask(__name__) # app is the Flask app
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/<location>/api/v1/items", methods = ["GET"])
|
||||||
|
def api_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>/<key>", methods = ["DELETE"])
|
||||||
|
def api_rm_brand_key(location, item, brand, key):
|
||||||
|
try:
|
||||||
|
storage.rm_brand_key(location, item, brand, key)
|
||||||
|
print("Deleted key {0} from brand {1} of item {2} at {3}".format(key, 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
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("Run with `flask` or a WSGI server!")
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
flask
|
||||||
|
flask_api
|
86
storage.py
Normal file
86
storage.py
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
InvMan Storage Module
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
from flask import jsonify
|
||||||
|
from flask_api import status
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
class AlreadyExistsError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def load_raw_json():
|
||||||
|
with open("inventory.json", "r") as f:
|
||||||
|
print("Opened inventory.json")
|
||||||
|
raw_json = json.loads(f.read())
|
||||||
|
print("Converted JSON to dictionary")
|
||||||
|
return raw_json
|
||||||
|
|
||||||
|
|
||||||
|
def write_raw_json(writedict):
|
||||||
|
with open("inventory.json", "w+") as f:
|
||||||
|
print("Opened inventory.json for writing")
|
||||||
|
f.write(json.dumps(writedict))
|
||||||
|
print("Wrote dictionary as JSON to inventory.json")
|
||||||
|
|
||||||
|
|
||||||
|
def get_items(location):
|
||||||
|
return load_raw_json()[location]
|
||||||
|
|
||||||
|
|
||||||
|
def init_item(location, item):
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
if item not in rawitems[location].keys():
|
||||||
|
rawitems[location].update({item: {}})
|
||||||
|
write_raw_json(rawitems)
|
||||||
|
else:
|
||||||
|
raise AlreadyExistsError
|
||||||
|
|
||||||
|
|
||||||
|
def init_brand(location, item, brand):
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
if item not in rawitems[location].keys():
|
||||||
|
init_item(location, item)
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
|
||||||
|
if brand not in rawitems[location][item].keys():
|
||||||
|
rawitems[location][item].update({brand: {}})
|
||||||
|
write_raw_json(rawitems)
|
||||||
|
else:
|
||||||
|
raise AlreadyExistsError
|
||||||
|
|
||||||
|
|
||||||
|
def set_brand_key(location, item, brand, key, value):
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
if item not in rawitems[location].keys():
|
||||||
|
init_item(location, item)
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
|
||||||
|
if brand not in rawitems[location][item].keys():
|
||||||
|
init_brand(location, item, brand)
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
|
||||||
|
rawitems[location][item][brand].update({key: value})
|
||||||
|
write_raw_json(rawitems)
|
||||||
|
|
||||||
|
|
||||||
|
def rm_brand_key(location, item, brand, key):
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
rawitems[location][item][brand].pop(key)
|
||||||
|
write_raw_json(rawitems)
|
||||||
|
|
||||||
|
|
||||||
|
def rm_brand(location, item, brand):
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
rawitems[location][item].pop(brand)
|
||||||
|
write_raw_json(rawitems)
|
||||||
|
|
||||||
|
|
||||||
|
def rm_item(location, item):
|
||||||
|
rawitems = load_raw_json()
|
||||||
|
rawitems[location].pop(item)
|
||||||
|
write_raw_json(rawitems)
|
Loading…
Reference in New Issue
Block a user