def insert_in_datastore_and_get_id(entityKind, entity): print("Entering insert_in_datastore_and_get_id...") # Initialize the response dictionary response = {"id": 0, "message": "", "validOutputReturned": True} entity = datastoreoperations.create_datastore_entity(entityKind, entity) if not entity['validOutputReturned']: # Error creating datastore entity response['validOutputReturned'] = False response['message'] = entity['message'] # Return. Don't move forward. return response id = entity['entity'].key.id # entity['datastore_id'] = id # Update the Datastore ID in Datastore updated_entity = { "last_modified_timestamp": datetime.datetime.now(), "datastore_id": id } entity = datastoreoperations.update_datastore_entity( entityKind, id, updated_entity) if not entity['validOutputReturned']: # Error creating datastore entity response['validOutputReturned'] = False response['message'] = entity['message'] # Return. Don't move forward. return response # Update the Datastore ID in the Output Dictionary response['id'] = entity['entity'].key.id response['message'] = "Successfully inserted into the Datastore." return response
def delete_user(entityKind, username): print("Entering delete_user...") # Initialize the response dictionary response = {"result": False, "message": "", "validOutputReturned": True} # Check whether user exists, and fetch the ID. user = isValidUser(entityKind, username.lower()) if not user['validOutputReturned']: # Error returned from isValidUser. Cannot delete. response['message'] = user['message'] response['validOutputReturned'] = False # Return. Don't move forward. return response # Valid output returned from isValidUser if not user['result']: # User does not exist. Cannot update. response['message'] = user['message'] # Return. Don't move forward. return response # User exists. Go ahead with delete. id = user['userDetails']['id'] flag_user_hard_delete = utilities.get_value_by_entityKind_and_key( env['config_entityKind'], "flag_user_hard_delete")['config_value'] if flag_user_hard_delete: # HARD delete from Datastore entityKind = utilities.get_value_by_entityKind_and_key( env['config_entityKind'], "datastore_kind_users")['config_value'] delete_user = datastoreoperations.delete_datastore_entity( entityKind, id) if not delete_user['validOutputReturned']: # Error returned from delete_user response['message'] = delete_user['message'] response['validOutputReturned'] = False else: # Valid output returned from delete_user response['message'] = "User HARD deleted from the DB." response['result'] = True return response if not flag_user_hard_delete: # SOFT delete from Datastore # Update the Active flag to False in Datastore updated_user = {"active": False} entityKind = utilities.get_value_by_entityKind_and_key( env['config_entityKind'], "datastore_kind_users")['config_value'] updated_user = datastoreoperations.update_datastore_entity( entityKind, id, updated_user) if not updated_user['validOutputReturned']: # Error returned from update_datastore_entity response['message'] = updated_user['message'] response['validOutputReturned'] = False else: # Valid output returned from update_datastore_entity response['message'] = "User SOFT deleted from the DB." response['result'] = True return response
def update_datastore_linear_equations(input_linear_equations): print("Entering update_datastore_linear_equations...") # Update Datastore Entity id = input_linear_equations['datastore_id'] updated_entity = { "last_modified_timestamp": datetime.datetime.now(), "answer": input_linear_equations['answer'], "timeTaken": input_linear_equations['timeTaken'], "userAnswerCorrect": input_linear_equations['userAnswerCorrect'] } # Get entityKind config from Datastore entityKind = utilities.get_value_by_entityKind_and_key(env['config_entityKind'],"datastore_kind_linear_equations")['config_value'] status = datastoreoperations.update_datastore_entity(entityKind,id,updated_entity) return status
def update_datastore_basic_arithmatic_operations( input_basic_arithematic_operation): print("Entering update_datastore_basic_arithmatic_operations...") # Update Datastore Entity id = input_basic_arithematic_operation['datastore_id'] updated_entity = { "last_modified_timestamp": datetime.datetime.now(), "timeTaken": input_basic_arithematic_operation['timeTaken'], "questions": input_basic_arithematic_operation['questions'], "userAnswerCorrect": input_basic_arithematic_operation['userAnswerCorrect'] } # Get entityKind config from Datastore entityKind = utilities.get_value_by_entityKind_and_key( env['config_entityKind'], datastore_kind_basic_arithematic_operations_key)['config_value'] status = datastoreoperations.update_datastore_entity( entityKind, id, updated_entity) return status
def update_datastore_config_from_json(): print("Entering update_config...") entityKind = env['config_entityKind'] envVariables = config.read_configurations_from_config_file() # Process countTotal = len(envVariables) countCreate = 0 countUpdate = 0 countNoChange = 0 for (key,value) in envVariables.items(): # Check whether the key already exists in the DB. read = utilities.read_datastore_and_get_id(entityKind,"key",key) if not read['id']: # Key not found. Create. create = utilities.create_key_value_pair_in_datastore(entityKind,key,value) countCreate += 1 elif read['id']: # Key found. # Check whether the json supplied value has changed. entity = datastoreoperations.get_datastore_entity(entityKind,read['id']) if value == entity['entity']['value']: # json supplied value has NOT changed. Update NOT needed. pass else: # json supplied value has changed. Update. jsonEntity = { "key": key, "value": value } update = datastoreoperations.update_datastore_entity(entityKind,read['id'],jsonEntity) countUpdate += 1 else: # Will never get here. pass # Calculate count of no change countNoChange = countTotal - countCreate - countUpdate print("Process completed. Total Configurations:{}. Created:{}, Updated:{}, No Change:{}.".format(countTotal, countCreate, countUpdate, countNoChange))
def update_user(entityKind, user_details): print("Entering update_user...") # Initialize the response dictionary response = { "entity": None, "message": "", "validOutputReturned": True, "userDetails": {} } # Validate User Data attribute_validation = validate_user_attributes(user_details) if not attribute_validation['validOutputReturned']: # Error returned from validate_user_attributes response['message'] = attribute_validation['message'] response['validOutputReturned'] = False # Return. Don't move forward. return response # Valid output returned from validate_user_attributes if not attribute_validation['result']: # Attribute validation result failed. Cannot proceed with update. response['message'] = attribute_validation['message'] # Return. Don't move forward. return response # All attributes are valid. # Check whether the user is already in the DB. user = isValidUser(entityKind, user_details['username'].lower()) if not user['validOutputReturned']: # Error returned from isValidUser response['message'] = user['message'] # Return. Don't move forward. return response # Valid output returned from isValidUser if not user['result']: # User does not exist. Cannot update. response['message'] = user['message'] # Return. Don't move forward. return response # User exists; proceed with update. id = user['userDetails']['id'] updated_user_entity = { # username should always be stored in lowercase. "username": user_details['username'].lower(), "password": user_details['password'], # first_name and last_name should always be stored with first letter Capital. "first_name": user_details['first_name'].title(), "last_name": user_details['last_name'].title(), # email should always be stored in lowercase. "email": user_details['email'].lower(), "forgot_password_question": user_details['forgot_password_question'], "forgot_password_answer": user_details['forgot_password_answer'], "active": True, "last_modified_timestamp": datetime.datetime.now() } entityKind = utilities.get_value_by_entityKind_and_key( env['config_entityKind'], "datastore_kind_users")['config_value'] entity = datastoreoperations.update_datastore_entity( entityKind, id, updated_user_entity) if not entity['validOutputReturned']: # Error returned from update_datastore_entity response['message'] = entity['message'] # Return. Don't move forward. return response # Valid output returned from update_datastore_entity response['message'] = "User updated successfully in the DB." response['entity'] = entity['entity'] return response