93 lines
2.2 KiB
Python
93 lines
2.2 KiB
Python
#!/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_locations():
|
|
return list(load_raw_json().keys())
|
|
|
|
|
|
def get_items(location):
|
|
return load_raw_json()[location]
|
|
|
|
|
|
def init_location(location):
|
|
rawitems = load_raw_json()
|
|
if location not in rawitems.keys():
|
|
rawitems.update({location: {}})
|
|
write_raw_json(rawitems)
|
|
else:
|
|
raise AlreadyExistsError
|
|
|
|
|
|
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 increment_amount(location, item, brand, amount):
|
|
rawitems = load_raw_json()
|
|
newamt = rawitems[location][item][brand][amount] + int(amount)
|
|
rawitems[location][item][brand].update({"amount": newamt})
|
|
write_raw_json(rawitems)
|
|
|
|
|
|
def rm_brand_field(location, item, brand, field):
|
|
rawitems = load_raw_json()
|
|
rawitems[location][item][brand].pop(field)
|
|
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)
|