def check_username_exists(check_username_exists_json): check_username_exists_dict = json_decoder.decode( check_username_exists_json) print("\n", check_username_exists_dict, "\n") # if new installation, create new user table if not (user_table_exists()): create_new_user_table() # check user table is username exists fileObject = open("USERTABLE", "rb") table = pickle.load(fileObject) print("\n", table, "\n") if any(obj['username'] == check_username_exists_dict['username'] for obj in table): print("exists") return json_encoder.encode({ "message": "Success", "comment": "Username exists" }) return json_encoder.encode({ "message": "Success", "comment": "Username available" })
def check_username_and_password_matches(): try: # if new installation, create a new user table if not (user_table_exists()): create_new_user_table() # extract from POST request form data wrapped in json username = request.json['username'] password = request.json['password'] # open user table and check if username and password match. fileObject = open("USERTABLE", "rb") table = pickle.load(fileObject) if (any(username == obj['username'] and password == obj['password'] for obj in table)): return json_encoder.encode({ "message": "Success", "comment": "Username and password match" }) return json_encoder.encode({ "message": "Success", "comment": "Username and password does not match" }) except: return json_encoder.encode({ "message": "Failure", "comment": "Other error" })
def add_user(): try: user = request.json print(user) # if new installation, create a new user table if not (user_table_exists()): create_new_user_table() fileObject = open("USERTABLE", "rb") table = pickle.load(fileObject) fileObject.close() # newly created user does not have any notebooks obj = { "username": user['username'], "password": user['password'], "created_notebooks": [] } table.append(obj) # save updated user table fileObject = open("USERTABLE", "wb") pickle.dump(table, fileObject) fileObject.close() return json_encoder.encode({"message": "success"}) except: return json_encoder.encode({"message": "failure"})
def check_username_exists(check_username_exists_json): try: check_username_exists_dict = json_decoder.decode( check_username_exists_json) # if new installation, create new user table if not (user_table_exists()): create_new_user_table() # check user table is username exists fileObject = open("USERTABLE", "rb") table = pickle.load(fileObject) if any(obj['username'] == check_username_exists_dict['username'] for obj in table): return json_encoder.encode({ "message": "Success", "comment": "Username exists" }) return json_encoder.encode({ "message": "Success", "comment": "Username available" }) except: return json_encoder.encode({ "message": "Failure", "comment": "Other error" })