def addCategory(cat_dict): if not isinstance(cat_dict, dict): return -1 db = gherkindb.load("databases/cats.db", True) dbresid = gherkindb.load("databases/catids.db", True) # Reservation already exists - This isn't an edit function if db.get(cat_dict["id"]) is not None: return -2 # Force unique labels for catkey in db.scpyall(): cat = db.get(catkey) if cat["label"] == cat_dict["label"]: return -3 # Obtain a new reservation id new_res_id = dbresid.get("next_res_id") if new_res_id is None: new_res_id = 0 dbresid.set("next_res_id", new_res_id + 1) new_res_id = str(new_res_id) cat_dict["id"] = new_res_id # Add to the db db.set(cat_dict["id"], cat_dict) return 0
def addItem(item_dict): if not isinstance(item_dict, dict): return -1 db = gherkindb.load("databases/items.db", True) check = db.get(item_dict["barcode"]) if check is not None: return -2 # Add the item db.set(item_dict["barcode"], item_dict) # Create a schedule list for the item dbs = gherkindb.load("databases/schedules.db", True) if dbs.get(item_dict["barcode"]) is None: dbs.lcreate(item_dict["barcode"]) return 0
def authRequest(token): db = gherkindb.load("databases/config.db", True) stored_token = db.get("qtApiToken") if stored_token is None: print(">> No tokens loaded in db") return False if stored_token != token: return False return True
def handleItemData(item_dict): try: item_dict = json.loads(item_dict) except: return generateError("JSON Error", "Json unable to loads item data.") expected = ["barcode", "name", "desc", "periphs"] periphreq = ["name", "desc", "count", "numberpresent"] for x in expected: if x not in item_dict.keys(): return generateError("Item Error", "Item missing required dictionary elements.") if not isinstance(item_dict["periphs"],list): return generateError("Periph Error", "Item value of non-list type.") for x in item_dict["periphs"]: if not isinstance(x, dict): return generateError("Periph Error", "Peripheral entry of non-dict type.") for y in periphreq: if y not in x.keys(): return generateError("Periph Error", "Peripheral entry missing required dictionary elements.") updating = False db = gherkindb.load("databases/items.db", True) check = db.get(item_dict["barcode"]) if check is not None: updating = True if updating: result = "Item updated; " else: result = "Item created; " # Add the item - Full add, even if update db.set(item_dict["barcode"], item_dict) schedule_requires_creating = not updating # Create a schedule list for the item if it doesn't exist dbs = gherkindb.load("databases/schedules.db", True) if dbs.get(item_dict["barcode"]) is None: dbs.lcreate(item_dict["barcode"]) if schedule_requires_creating: result = result + "Schedule created." else: result = result + "Existing item had no schedule. Created." # We don't update the schedule here. return generateUpdateResponse("Item Update Complete", result)
def getTodaysReminders(): db = gherkindb.load("databases/rem_ti.db", True) todays_list = db.get(generateDayStamp()) if todays_list is None or len(todays_list) == 0: return json.dumps([]) reminders = [] for reminder in todays_list: reminders.append(reminder) try: reminders = json.dumps(reminders) except: return generateError("DB Error", "Json unable to dump reminders list to string") return reminders
def addReservation(res_dict): if not isinstance(res_dict, dict): return -1 dbti = gherkindb.load("databases/res_ti.db", True) dbid = gherkindb.load("databases/res_id.db", True) dbresid = gherkindb.load("databases/uniqueids.db", True) # Reservation already exists - This isn't an edit function if dbid.get(res_dict["id"]) is not None: return -2 # Obtain a new reservation id new_res_id = dbresid.get("next_res_id") if new_res_id is None: new_res_id = 0 dbresid.set("next_res_id", new_res_id+1) new_res_id = str(new_res_id) res_dict["id"] = new_res_id res_dict["created"] = generateTimeSimestamp() # Add to the comprehensive db dbid.set(res_dict["id"], res_dict) # Time-indexed db will have multiple (potentially) per-day # If it doesn't exist, we need to make a list res_dict["ti"] = generateDayStamp() if dbti.get(res_dict["ti"]) is None: dbti.lcreate(res_dict["ti"]) # Add the teservation to the time-indexed db dbti.ladd(res_dict["ti"], res_dict) # Schedule the device to be out for the time - we assume that if the # user could select the item, it exists in the db for item in res_dict["itemBarcodes"]: addToSchedule(item, res_dict["id"], res_dict["start"], res_dict["end"]) return 0
def addReminder(rem_dict): if not isinstance(rem_dict, dict): return -1 dbti = gherkindb.load("databases/rem_ti.db", True) dbid = gherkindb.load("databases/rem_id.db", True) # Reservation already exists - This isn't an edit function if dbid.get(rem_dict["id"]) is not None: return -2 rem_dict["created"] = generateTimeSimestamp() # Add to the comprehensive db dbid.set(rem_dict["id"], rem_dict) # Time-indexed db will have multiple (potentially) per-day # If it doesn't exist, we need to make a list if dbti.get(rem_dict["ti"]) is None: dbti.lcreate(rem_dict["ti"]) # Add the teservation to the time-indexed db dbti.ladd(rem_dict["ti"], rem_dict) return 0
def getAllReminders(): db = gherkindb.load("databases/rem_id.db", True) reminders = [] for key in db.scpyall(): rem = db.get(key) if rem is None: return generateError("DB Error", "No, or 'None' item key(s)") reminders.append(rem) if len(reminders) == 0: return generateError("DB Error", "There are no reminders found in databases/res_id.db") try: reminders = json.dumps(reminders) except: return generateError("DB Error", "Json unable to dump reminders list to string") return reminders
def getCats(): db = gherkindb.load("databases/cats.db", True) cats = [] for key in db.scpyall(): cat = db.get(key) if cat is None: return generateError("DB Error", "No, or 'None' cat key(s)") cats.append(cat) if len(cats) == 0: return generateError("DB Error", "There are no cats found in databases/res_id.db") try: cats = json.dumps(cats) except: return generateError("DB Error", "Json unable to dump cats list to string") return cats
def getAllItems(): db = gherkindb.load("databases/items.db", True) items = [] for key in db.scpyall(): item = db.get(key) if item is None: return generateError("DB Error", "No, or 'None' item key(s)") items.append(item) if len(items) == 0: return generateError("DB Error", "There are no items found in databases/items.db") try: items = json.dumps(items) except: return generateError("DB Error", "Json unable to dump item list to string") return items
def getSchedules(): db = gherkindb.load("databases/schedules.db", True) schedules = [] for key in db.scpyall(): sched = db.get(key) if sched is None: return generateError("DB Error", "No, or 'None' item key(s)") schedules.append(sched) if len(schedules) == 0: return generateError( "DB Error", "There are no schedules found in databases/res_id.db") try: schedules = json.dumps(schedules) except: return generateError("DB Error", "Json unable to dump schedules list to string") return schedules
def addToSchedule(item_barcode, res_id, start, end): sid = sha256((res_id + item_barcode + start + end).encode()).hexdigest() db = gherkindb.load("databases/schedules.db", True) # Items make their own schedule lists, but just in-case # we do a None check if db.get(item_barcode) is None: db.lcreate(item_barcode) db.ladd(item_barcode, { "sid": sid, "resid": res_id, "start": start, "end": end }) return sid
def getSingleSchedule(id): db = gherkindb.load("databases/schedules.db", True) ret_sched = None for key in db.scpyall(): sched = db.get(key) if sched is None: return generateError("DB Error", "No, or 'None' item key(s)") elif key == id: ret_sched = [{ "id": key, "schedule": sched }] if ret_sched is None: return generateError("DB Error", "There are no schedules for {} in databases/schedules.db".format(id)) try: ret_sched = json.dumps(ret_sched) except: return generateError("DB Error", "Json unable to dump schedules list to string") return ret_sched
dbresid.set("next_res_id", new_res_id + 1) new_res_id = str(new_res_id) cat_dict["id"] = new_res_id # Add to the db db.set(cat_dict["id"], cat_dict) return 0 ''' Load the dbs with some test info ''' if __name__ == '__main__': db = gherkindb.load("databases/config.db", True) if db.get("qtApiToken") is None: print("Loading development api token") db.set( "qtApiToken", "fefd8a1a97021dbab2d105c4784a1906cd89fc575009387d378b8807192c16e3") print( "Adding item: ", addItem({ "barcode": "938-x837-3284", "name": "Projector 1", "desc": "A test item named projector",