def set_employee_skills(uuid, skills_for_employee): try: # Get the Employee from the Datastore employee = appcache.get_employee(uuid) # If this fails, return False, as we do not have an Employee that exists yet if employee is None: logging.error('datastore.py : set_employee_skills() : No Employee with UUID of '+uuid+' exists, so cannot store Skills') return False # We need to delete all previous Skill instances for the Employee before creating new ones, to avoid duplicates skills = model_skill.Skill.get(employee.skills) try: if skills is not None and len(skills) > 0: logging.debug('datastore.py : set_employee_skills() : Employee with UUID of '+uuid+' has existing skills. These will now be deleted') db.delete(skills) except (Exception), e: logging.error('set_employee_skills() : Error deleting existing skills') logging.error(e) # Create a list of new Skill instances for that Employee # TODO: iterate through attributes of the Skill model instance, so that we don't have to manually set properties skills_for_db_transaction = list() for skill_required in skills_for_employee: skill = model_skill.Skill( id=skill_required.id, name=skill_required.name, name_index=skill_required.name_index, proficiency_index=skill_required.proficiency_index, proficiency_level=skill_required.proficiency_level, proficiency_name=skill_required.proficiency_name, years_id=skill_required.years_id, years_name=skill_required.years_name, employee=employee ) skills_for_db_transaction.append(skill) # Save the Skills to the datastore and get their Keys skill_keys = db.put(skills_for_db_transaction) # Update the Skills in memcache appcache.set_all_skills() appcache.set_employee_skills(uuid, skills_for_db_transaction) # Finally, return the Keys of the datastore write return skill_keys
def set_employee_skill(uuid, skill): try: # Get the Employee from the Datastore employee = appcache.get_employee(uuid) # If this fails, return False, as we do not have an Employee that exists yet if employee is None: logging.error('datastore.py : set_employee_skills() : No Employee with UUID of '+uuid+' exists, so cannot store Skills') return False skills = appcache.get_all_skills() skills.filter('employee = ', employee) skills.filter('name = ', skill.name) # Get the first result (there should be only one in any case) skill_to_add = skills.get() # If the Skills filter result is empty if skill_to_add is not None: logging.debug('set_employee_skill() : We have an existing Skill for Employee '+uuid+' with name of '+skill.name+'. Overwriting that Skill') # Else the Skill already exists. Rather than delete (additional datastore write) and create a new instance, let's just overwrite it else: logging.debug('set_employee_skill() : We don\'t have an existing Skill for Employee '+uuid+' with name of '+skill.name+'. Creating new Skill instance') # Set the Skill to add to be a new Skill model instance skill_to_add = model_skill.Skill() # Set Skill properties skill_to_add.name=skill.name skill_to_add.name_index=skill.name_index skill_to_add.proficiency_index=skill.proficiency_index skill_to_add.proficiency_level=skill.proficiency_level skill_to_add.proficiency_name=skill.proficiency_name skill_to_add.years_id=skill.years_id skill_to_add.years_name=skill.years_name skill_to_add.employee=employee # Save Skill to Datastore skill_to_add.put() # Save all Skills to memcache (also updates) skills = appcache.set_all_skills() # Query all Skills, returning Keys only # TODO: Use Memcache script for consistent separation of concerns employee_skills_keys = model_skill.Skill.all(keys_only=True) # Filter by Employee only # We are getting all Skill Keys for the Employee, instead of attempting to find a porentially previously existing Key in the employee.skils list and replacing it employee_skills_keys.filter('employee = ', employee) list_of_skill_keys = list() for skill_key in employee_skills_keys: list_of_skill_keys.append(skill_key) # Set Employee Skill Keys employee.skills = list_of_skill_keys # Set the Date Last Modified timestamp employee.date_last_modified = datetime.datetime.now() # Save Employee to Datastore employee.put() # Update memcache for Employee # We have to get it from Datastore again as the put() command will not update the Employee instance we created in this method employee = model_employee.Employee.get_by_key_name(uuid) appcache.set_employee(uuid, employee) # Update memecaceh with Employee Skills employee_skills = model_skill.Skill.get(employee.skills) appcache.set_employee_skills(uuid, employee_skills) # Update memcache for Employees appcache.set_all_employees() return uuid except (CapabilityDisabledError, Exception), e: logging.error(e) raise e