Beispiel #1
0
def set_employee(uuid, arguments):
	try:
		employee = appcache.get_employee(uuid)
		properties = model_employee.Employee.properties()
	   	if employee is None:
			employee = model_employee.Employee(key_name=uuid, id=uuid)
			logging.debug('datastore.py : set_employee() : Creating new Employee with '+uuid)
		else:
			logging.debug('datastore.py : set_employee() : Employee '+uuid+' already exists')
		
		# For each of the arguments received...
		for key, value in arguments.items():
			# Ensure it is an attribute of the Employee model subclass
			if properties.has_key(key) and value is not None and value != '':
				# If of type boolean...
				if properties[key].data_type is bool:
			   		setattr(employee, key, bool(arguments[key]))
				else:
					setattr(employee, key, arguments[key])
        
		# Set the Date Last Modified timestamp
		employee.date_last_modified = datetime.datetime.now()
		# Save the Employee
		employee.put()
		
		# Save the Employee to memcache
		saved = appcache.set_employee(uuid, employee)
		
		# Reset all memcached Employees
		saved_employees = appcache.set_all_employees()
		
		return uuid
	except (CapabilityDisabledError, Exception), e:
		logging.error(e)
		raise e 
Beispiel #2
0
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