def add_item_to_list(gid, measurment, sid, session): userid = __get_userid_from_key(session) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if len(gid) == 0 or len(measurment) == 0 or len(sid) == 0: kwlog.log("Format of request is invaild") return "INVAILD_FORMAT" if not MySQL.is_vaild_group(gid): kwlog.log("Group ID is not vaild") return "INVAILD_GROUP" if not MySQL.is_vaild_shopping_list(sid, userid): kwlog.log("Invaild shopping list id") name = "%s list" % str(userid) createList.create_new_list(name, session) sid = MySQL.get_shopping_lists(userid)[0][0] if not MySQL.insert_item_to_list(sid, gid, measurment, userid): kwlog.log("Unable to add item to list") return "FAILED_TO_ADD_ITEM" else: return "ADD_ITEM_COMPLETE" return "INVAILD_SHOPPING_LIST" if not MySQL.insert_item_to_list(sid, gid, measurment, userid): kwlog.log("Unable to add item to list") return "FAILED_TO_ADD_ITEM" else: return "ADD_ITEM_COMPLETE"
def __get_list(sid): final = [] slist = MySQL.get_list_of_shopping_items(sid) for s in slist: if MySQL.get_group_name_from_group_id(s[2]): k = [str(MySQL.get_group_name_from_group_id(s[2])[0]), s[3]] else: k = ["NONE", s[3]] final.append(list(k)) return list(final)
def __create_list(sid): print("Starting get_list") l = list(MySQL.get_list_of_shopping_items(sid)) print("list result:" + str(l)) final = "" for k in l: if MySQL.get_group_name_from_group_id(k[2]): final = final + ( "<tr><td>%s</td><td>%s</td></tr>" % (str(MySQL.get_group_name_from_group_id(k[2])[0]), str(k[3]))) return final
def __create_list(sid): print("Starting get_list") l = list(MySQL.get_list_of_shopping_items(sid)) print("list result:" + str(l)) final = "" for k in l: if MySQL.get_group_name_from_group_id(k[2]): final = final + ( "<tr><td>%s</td><td>%s</td></tr>" % (str(MySQL.get_group_name_from_group_id(k[2])[0]), str(k[3])) ) return final
def get_list_ID(session_key): userid = __get_userid_from_key(session_key) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: sid = MySQL.get_shopping_list_id_for_user(userid) if sid == "NONE": name = "%s list" % str(userid) createList.create_new_list(name, session_key) sid = MySQL.get_shopping_lists(userid)[0][0] print(sid) return str(sid)
def __check_vaild_date(key): # Checks if key vaild date has passed # return bool d = str(MySQL.get_session_key_expire_data(key)) d1 = datetime.strptime(d, "%Y-%m-%d %H:%M:%S") d2 = datetime.now() return d1 > d2
def update_inventory_item(info, uid, session_key): # Update inventory information for user # Return: string # info[] = [ExperationDate, PercentUsed] userid = __get_userid_from_key(session_key) for i in info: i = __clean_barcode(str(i)) if userid == "BAD_KEY": kwlog.log("Bad Session Key") return "BAD_KEY" else: if MySQL.is_item_owned_by_user(userid, uid): return MySQL.update_inventory_item(uid, info) else: "INVAILD_INVENTORY_ID"
def update_inventory_item(info, uid, session_key): # Update inventory information for user # Return: string # info[] = [ExperationDate, PercentUsed] userid = __get_userid_from_key(session_key) for i in info: i = __clean_barcode(str(i)) if userid == 'BAD_KEY': kwlog.log("Bad Session Key") return "BAD_KEY" else: if MySQL.is_item_owned_by_user(userid, uid): return MySQL.update_inventory_item(uid, info) else: "INVAILD_INVENTORY_ID"
def __create_shopping_list(userid): final = [] slist = MySQL.get_shopping_lists(userid) for s in slist: k = [s[0], s[1], __get_list(s[0]), s[2], str(s[3])] final.append(list(k)) return list(final)
def remove_item_from_list(rid, gid, session): userid = __get_userid_from_key(session) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if not MySQL.is_vaild_group(gid): return "INVAILD_GROUP" if not MySQL.is_vaild_shopping_list(rid, userid): kwlog.log("List not owned by user") return "INVAILD_SHOPPING_LIST" else: if not MySQL.remove_item_from_shopping_list(gid, rid): return "UNABLE_TO_REMOVE_ITEM" else: return "REMOVE_ITEM_COMPLETE"
def remove_all_items_from_list(rid, session): userid = __get_userid_from_key(session) print(rid) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if not MySQL.is_vaild_shopping_list(rid, userid): kwlog.log("List not owned by user") return "INVAILD_SHOPPING_LIST" else: if not MySQL.remove_all_items_from_shopping_list(rid): kwlog.log("Unable to remove items from list") return "UNABLE_TO_REMOVE_ITEMS" else: kwlog.log("Remove items complete") return "REMOVE_ITEMS_COMPLETE"
def __get_userid_from_key(key): # Gets userid from session key # Return str kwlog.log("Get userid from key") if __vaildate_sessionkey(key): return MySQL.get_userid_from_session_key(key) else: return "BAD_KEY"
def __check_act_status(userid): # Check if account is activated # Return bool data = MySQL.get_active_status(userid) if str(data[0]) == '1': return True else: return False
def update_account_information(fname, lname, email, password, sessionkey): kwlog.log("request to update account information") userid = __get_userid_from_key(sessionkey) if len(fname) > 0: if not MySQL.update_first_name_for_user(userid, fname): return False if len(lname) > 0: if not MySQL.update_last_name_for_user(userid, lname): return False if len(email) > 0: if not MySQL.update_email_for_user(userid, email): return False if len(password) > 0: password = __encrypt_password(password) if not MySQL.update_password_for_user(userid, password): return False return True
def __get_userid_from_key(key): # Gets userid from session key # Return str kwlog.log("Get userid from key") if (__vaildate_sessionkey(key)): return MySQL.get_userid_from_session_key(key) else: return "BAD_KEY"
def update_group_of_item(groupid, barcode, session_key): userid = __get_userid_from_key(session_key) if userid == "BAD_KEY": kwlog.log("Bad Session Key") return "BAD_KEY" else: kwlog.log("Updating group for product") return MySQL.update_group_of_item(groupid, barcode)
def check_active_status(username): # Checks if account has been activated # Return bool if MySQL.get_active_status(username)[0] == 0: kwlog.log("Account not activated") return False else: kwlog.log("Account activated") return True
def update_group_of_item(groupid, barcode, session_key): userid = __get_userid_from_key(session_key) if userid == 'BAD_KEY': kwlog.log("Bad Session Key") return "BAD_KEY" else: kwlog.log("Updating group for product") return MySQL.update_group_of_item(groupid, barcode)
def update_recipe(rec_id, name, dis, image, prepT, cookT, items, itemAction, sessionkey): userid = __get_userid_from_key(sessionkey) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if len(itemAction) == 0 and len(items) > 0: return "INVAILD_FORMAT" elif not (itemAction == "ADD" or itemAction == "REMOVE" or itemAction == "UPDATE" or itemAction == ""): return "INVAILD_FORMAT" else: if MySQL.owned_by_user(userid, rec_id): if len(name) > 0: if not MySQL.update_recipe_name(name, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" if len(dis) > 0: if not MySQL.update_recipe_dis(dis, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" if len(image) > 0: if not MySQL.update_recipe_image(image, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" if len(prepT) > 0: if not MySQL.update_recipe_prep(prepT, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" if len(cookT) > 0: if not MySQL.update_recipe_cook(cookT, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" if len(itemAction) > 0: if itemAction == "ADD": if not MySQL.update_recipe_add_item(items, rec_id): kwlog.log("Add item to recipe failed") return "UPDATE_FAILED" elif itemAction == "REMOVE": if not MySQL.update_recipe_remove_item(items, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" else: if not MySQL.update_recipe_update_item(items, rec_id): kwlog.log("Update recipe name failed") return "UPDATE_FAILED" return "UPDATE_COMPLETE" else: return "BAD_REC_ID"
def __check_code(userid, code): # Check code aginst DB # Return bool d_code = MySQL.get_act_code(userid) if d_code == code: kwlog.log("Activation code matches") return True else: kwlog.log("Activation code does not match") return False
def remove_shopping_list(sid, session): if removeItem.remove_all_items_from_list(sid, session) == "REMOVE_ITEMS_COMPLETE": userid = __get_userid_from_key(session_key) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if len(sid) == 0: kwlog.log("Format of request is invaild") return "INVAILD_FORMAT" if not MySQL.is_vaild_shopping_list(sid, userid): kwlog.log("Invaild shopping list id") return "INVAILD_SHOPPING_LIST" if MySQL.remove_shopping_list(sid): return True else: return False else: return False
def __session_key_exist(key): # Checks Session Key exist # Return bool if __check_vaild_date(key): if MySQL.get_userid_from_session_key(key): return True else: return False else: kwlog.log("Key has expired") return False
def remove_shopping_list(sid, session): if removeItem.remove_all_items_from_list( sid, session) == "REMOVE_ITEMS_COMPLETE": userid = __get_userid_from_key(session_key) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if len(sid) == 0: kwlog.log("Format of request is invaild") return "INVAILD_FORMAT" if not MySQL.is_vaild_shopping_list(sid, userid): kwlog.log("Invaild shopping list id") return "INVAILD_SHOPPING_LIST" if MySQL.remove_shopping_list(sid): return True else: return False else: return False
def user_exist(usr): # Check if userid exist # Return str kwlog.log("Checking if user is in DB") if (safetyCheck(usr)): if MySQL.is_userid_in_DB(usr): return "ID_FOUND" else: return "NO_ID_FOUND" else: kwlog.log("safety check, failed") return "BAD_ID"
def user_exist(usr): # Check if userid exist # Return str kwlog.log("Checking if user is in DB") if(safetyCheck(usr)): if MySQL.is_userid_in_DB(usr): return "ID_FOUND" else: return "NO_ID_FOUND" else: kwlog.log("safety check, failed") return "BAD_ID"
def create_account(username, fname, lname, email, hash): # Create account and add to DB # Return bool if MySQL.put_new_account(username, fname, lname, email, hash): if create_confirmation_email(fname, email, username): kwlog.log("Account Created, all good") return True else: kwlog.log("Error during creating confirmation email") return False else: return False
def send_list(sid, session_key): userid = __get_userid_from_key(session_key) if userid == "BAD_KEY": kwlog.log("Invaild session key") return "BAD_KEY" if not MySQL.is_vaild_shopping_list(sid, userid): kwlog.log("Invaild Shopping List") return "BAD_LIST" if not __send_email(userid, sid): kwlog.log("Failed to send shopping list") return "SEND_FAILED" else: return "EMAIL_SENT"
def send_list(sid, session_key): userid = __get_userid_from_key(session_key) if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" if not MySQL.is_vaild_shopping_list(sid, userid): kwlog.log("Invaild Shopping List") return "BAD_LIST" if not __send_email(userid, sid): kwlog.log("Failed to send shopping list") return "SEND_FAILED" else: return "EMAIL_SENT"
def create_new_list(name, sessionkey): if len(sessionkey) > 0: userid = __get_userid_from_key(sessionkey) else: return "INVAILD_FORMAT" if userid == 'BAD_KEY': kwlog.log("Invaild session key") return "BAD_KEY" else: if not len(name) > 0: return "INVAILD_FORMAT" if MySQL.create_new_shopping_list(name, userid): return "LIST_CREATED" else: return "FAILED_TO_CREATE_LIST"
def __send_email(userid, sid): email = MySQL.get_email_for_user(userid) list_str = __create_list(sid) if list_str == "FAILED": return False kwlog.log("Create email request") server = smtplib.SMTP("smtp.gmail.com", 587) server.ehlo() server.starttls() server.login("*****@*****.**", "KitchenWizard") kwlog.log("Login to email - complete") msg = MIMEMultipart() msg["From"] = "*****@*****.**" msg["To"] = email msg["Subject"] = "Kitchen Wizard - Shopping List" body = """ <html> <head></head> <body> <h1>Shopping List</h1> <table cellpadding = \"15\" border = \"1\"> <tr><th>Item</th><th>Quantity</th></tr> %s </table> <br/><br/><br/> <p> Remember,<br/> Everyone Loves KitchenWizard!!! </p> </body> </html> """ % ( list_str ) msg.attach(MIMEText(body, "html")) kwlog.log("Sending message...") try: server.sendmail("*****@*****.**", email, msg.as_string()) server.close() kwlog.log("Message sent") return True except: server.close() kwlog.log("Message Failed") return False
def __send_email(userid, sid): email = MySQL.get_email_for_user(userid) list_str = __create_list(sid) if list_str == "FAILED": return False kwlog.log("Create email request") server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login("*****@*****.**", "KitchenWizard") kwlog.log("Login to email - complete") msg = MIMEMultipart() msg['From'] = "*****@*****.**" msg['To'] = email msg['Subject'] = "Kitchen Wizard - Shopping List" body = """ <html> <head></head> <body> <h1>Shopping List</h1> <table cellpadding = \"15\" border = \"1\"> <tr><th>Item</th><th>Quantity</th></tr> %s </table> <br/><br/><br/> <p> Remember,<br/> Everyone Loves KitchenWizard!!! </p> </body> </html> """ % (list_str) msg.attach(MIMEText(body, 'html')) kwlog.log("Sending message...") try: server.sendmail("*****@*****.**", email, msg.as_string()) server.close() kwlog.log("Message sent") return True except: server.close() kwlog.log("Message Failed") return False
def remove_recipe(recipe_id, session_key): userid = __get_userid_from_key(session_key) recipe_id = int(recipe_id) if not __recipe_in_inventory(recipe_id, userid): kwlog.log("Recipe not in inventory") return False else: if MySQL.remove_all_items_from_recipe(recipe_id): kwlog.log("removed all items") if __remove_recipe_from_db(recipe_id): kwlog.log("recipe removed from DB") return True else: kwlog.log("could not remove recipe") return False else: kwlog.log("failed to remove all items") return False
def get_list_of_generic_items(): # Get list of generic Items # Return: str return MySQL.get_groups()
def add_confirmation_to_db(code, username): # Adds confirmation code to DB # Return bool return MySQL.put_confirmation_code_in_database(code, username)
def __get_userid_from_activation_code(code): # Get userid linked with act code # Return: str return MySQL.get_userid_linked_to_act_code(code)
def __remove_item_from_db(item_id): return MySQL.remove_item_from_inventory(item_id)
def __item_in_inventory(item_id, userid): if MySQL.is_item_in_inventory(item_id, userid): return True else: return False
def uppdate_session_key(username, ses): # Update Session key in DB # Return void if not MySQL.update_session_key_for_usr(username, ses): kwlog.log("Error, updating key")
def get_hash_for_user(userid): # Get hash for user, return hash # Return str return MySQL.get_password_hash_for_usr(userid)
def get_group_id(name): return MySQL.get_group_by_name(name)
def check_if_email_exist(email): data = MySQL.is_email_in_database(email) if not data: return False else: return True