def get(self): try: # no_prompt: Simple flag for the Search Input form field. True = do not show the list of skill search terms. The skills dict name is the same for the search and search results view no_prompt = True skills = dict() db_skills = appcache.get_all_skills() skill_properties = model_skill.Skill.properties() # TODO: Use the procedure from SkillsResultsController, as it's better, and we can still get the count value from the length of the skills list # ([ST] is that what we need here ?) # To avoid duplicates, add the skill to a dict with a counter value, corresponding to the number of Employee instances that have this Skill for skill in db_skills: if skills.has_key(skill.name_index): # Increment the Employee count for this skill count = skills[skill.name_index]['count'] + 1 skills[skill.name_index] = {'skill':skill, 'count':count} else: # Create a dict object for this Skill skills[skill.name_index] = {'skill':skill, 'count':1} # Sort the Dict into a List of Tuples. This maintains the output as an iterable key-value pair, rather than return a list of just the keys skills = sorted(skills.items()) args = dict(env=os.environ, skills=skills, no_prompt=no_prompt, skill_properties=skill_properties) path = os.path.join(os.path.dirname(__file__),'../views/skills/search.html') self.response.out.write(template.render(path, args)) except (Exception), e: logging.error('SkillsSearchController : get()') logging.error(e) self.error(500)
def get(self): try: # Get Employees employees = appcache.get_all_employees() employees = sorted(employees, key=lambda employee: employee.date_last_modified, reverse=True) employees = employees[0:5] # Get Skills skills = dict() db_skills = appcache.get_all_skills() # To avoid duplicate skills in display, add the skill to a dict with a Skill list value, corresponding to Skill:Employee instances of that skill for skill in db_skills: if skills.has_key(skill.name_index): # Increment the Employee count for this skill skills_list = skills[skill.name_index]['skills'] skills_list.append(skill) skills[skill.name_index] = {'skill':skill, 'skills':skills_list} else: # Create a dict object for this Skill skills_list = list() skills_list.append(skill) skills[skill.name_index] = {'skill':skill, 'skills':skills_list} # Sort the Dict into a List of Tuples, sorted by key name. This maintains the output as an iterable key-value pair, rather than returning a list of just the keys skills = sorted(skills.items(), key=lambda skill: skill[1]['skill'].created, reverse=True) # Get the last 5 skills created skills = skills[0:5] # Get latest Skills Searches skills_searches = appcache.get_skills_searches() if skills_searches is not None: skills_searches = sorted(skills_searches.items(), reverse=True)[:10] args = dict(env=os.environ, employees=employees, skills=skills, skills_searches=skills_searches) path = os.path.join(os.path.dirname(__file__),'../views/index.html') self.response.out.write(template.render(path, args)) except (Exception), e: logging.error('HomeController : get()') logging.error(e) self.error(500)
def get(self): try: skills_requested = list() skills_requested_full_name = list() skills = dict() db_skills = appcache.get_all_skills() # Add the Search to Memcache appcache.set_skills_searches(self.request) skill_arguments = str(self.request.get('skills')).strip().strip(r'<>!').split(',') skill_properties = model_skill.Skill.properties() # If no Skills have been searched for, just redirect to the Search view from the Results view # NOTE: There would be 1 entry as we have used .split() to return a List instance if len(skill_arguments) <= 1 and skill_arguments[0] == '': self.redirect('/skills') return # Arguments should be a list of Skills for skill in skill_arguments: skill = skill.strip() if skill is not None and skill != '': skills_requested.append(skill.lower()) skills_requested_full_name.append(skill) # Match Skills with the requested names # NOTE: Matches exact string names, not a Regular Expression db_skills.filter('name_index IN ', skills_requested) # Filter by Proficiency proficiency_requested = list() proficiency_arguments = str(self.request.get('proficiency')).strip().strip(r'<>!').split(',') for proficiency in proficiency_arguments: proficiency = proficiency.strip() if proficiency is not None and proficiency != '': proficiency_requested.append(proficiency) # Match Skills with the requested Proficiency # NOTE: Matches exact string names, not a Regular Expression if len(proficiency_requested) > 0: db_skills.filter('proficiency_name IN ', proficiency_requested) # To avoid duplicate skills in display, add the skill to a dict with a Skill list value, corresponding to Skill:Employee instances of that skill for skill in db_skills: if skills.has_key(skill.name_index): # Collect the existing Skills skills_list = skills[skill.name_index]['skills'] # Add the current Skill in iteration skills_list.append(skill) # Sort the Skills list by Employee last_name skills_list = sorted(skills_list, key=lambda skill: skill.employee.last_name, reverse=False) # Assign it back to the dict entry skills[skill.name_index]['skills'] = skills_list else: # Create a dict object for this Skill skills_list = list() skills_list.append(skill) skills[skill.name_index] = {'skill':skill, 'skills':skills_list} # Now add back in any filtered out requested Skills, e.g. because we don't have anyone with that Skill for required_skill in skills_requested_full_name: if not skills.has_key(required_skill.lower()): skills[required_skill.lower()] = {'skill':{'name':required_skill}, 'skills':None} # Sort the Dict into a List of Tuples, sorted by key name of ther Skill. This maintains the output as an iterable key-value pair, rather than returning a list of just the keys skills = sorted(skills.items()) # Add some bespoke display helpers. These help with the grammar of written statements within for loops, where we need to know the penultimate item in a loop skills_penultimate = len(skills)-1 proficiency_penultimate = len(proficiency_requested)-1 args = dict( env=os.environ, skills=skills, skills_penultimate=skills_penultimate, proficiency_requested=proficiency_requested, proficiency_penultimate=proficiency_penultimate, skill_properties=skill_properties) path = os.path.join(os.path.dirname(__file__),'../views/skills/results.html') self.response.out.write(template.render(path, args)) except (Exception), e: logging.error('SkillsResultsController : get()') logging.error(e) self.error(500)
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