From d0abbef8d1eaccc6f05313b9622a1d5202d9d0f0 Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Thu, 24 Sep 2020 20:31:40 -0500 Subject: [PATCH] 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 --- storage.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/storage.py b/storage.py index d5ece56..46d45fb 100644 --- a/storage.py +++ b/storage.py @@ -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)