Add more functions to storage.py

- Add get_locations()
- Add init_location() to create a new blank location
- Add increment_amount() to increment the amount of one item
- Remove set_brand_key() since it will be replaced by separate functions
  for each property you can set
This commit is contained in:
BBaoVanC 2020-09-24 20:31:40 -05:00
parent 09730acfcc
commit d0abbef8d1
Signed by: bbaovanc
GPG Key ID: 18089E4E3CCF1D3A
1 changed files with 18 additions and 12 deletions

View File

@ -28,10 +28,23 @@ def write_raw_json(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():
@ -54,23 +67,16 @@ def init_brand(location, item, brand):
raise AlreadyExistsError
def set_brand_key(location, item, brand, key, value):
def increment_amount(location, item, brand, amount):
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})
newamt = rawitems[location][item][brand][amount] + int(amount)
rawitems[location][item][brand].update({"amount": newamt})
write_raw_json(rawitems)
def rm_brand_key(location, item, brand, key):
def rm_brand_field(location, item, brand, field):
rawitems = load_raw_json()
rawitems[location][item][brand].pop(key)
rawitems[location][item][brand].pop(field)
write_raw_json(rawitems)