def send_linkedin_connection_request(uuid, authorisation_url): try: employee = appcache.get_employee(uuid) if employee is None: logging.error('send_linkedin_connection_request() : No Employee exists with UUID of '+uuid) return False if employee.email is None: logging.error('send_linkedin_connection_request() : Employee with UUID of '+uuid+' has no email address!') return False # Create Email mail_props = utils.load_property_file('mail') mail_body = mail_props.get('HTMLTemplate', 'body') mail_body = mail_body.replace('@employee.first_name@', employee.first_name) mail_body = mail_body.replace('@authorisation_url@', authorisation_url) message = mail.EmailMessage() message.sender = mail_props.get('Mail', 'sender') message.to = employee.email message.subject = mail_props.get('LinkedInRequest', 'subject') message.body='Hi '+employee.first_name+'\n\nRazorfish Skills Matrix would like to access your LinkedIn Profile.\n\nClick below to authorise the Razorfish Skills Matrix to access and retrieve your Skills from LinkedIn.\n\n'+authorisation_url+'\n\nThanks,\n\nRazorfish Resources.' message.html=mail_body message.send() datastore.set_employee(uuid, dict(oauth_request_issued=True)) return True except (Exception, apiproxy_errors.ApplicationError, apiproxy_errors.OverQuotaError), e: logging.error(e) raise e
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
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 get(self, uuid): try: is_admin = users.is_current_user_admin() employee_properties = model_employee.Employee.properties() skill_properties = model_skill.Skill.properties() uuid = str(uuid) skills = list() order = str(self.request.get('order')) errors = appcache.get_employee_create_errors(uuid) employee = appcache.get_employee(uuid) if employee is None: self.redirect('/employees') return # Get the skills for the Employee skills = appcache.get_employee_skills(uuid) # Sort Skills try: if skills is not None and len(skills) > 0: # If we have an explicit order, sort by that order if order == 'skill.name_index': skills = sorted(skills, key=lambda skill: skill.name_index, reverse=False) elif order == 'skill.proficiency_index': skills = sorted(skills, key=lambda skill: int(skill.proficiency_index), reverse=False) elif order == 'skill.years_id': skills = sorted(skills, key=lambda skill: int(skill.years_id), reverse=True) else: skills = sorted(skills, key=lambda skill: skill.name_index, reverse=False) except (Exception), e: # Do not redirect to the 500 error view from here, just catch the fact that the reference to the Skills is incorrect, # as the Skills no longer exist by those keys - e.g. in the situation where we have deleted the skills using the admin console logging.error(e) args = dict(env=os.environ, employee=employee, skills=skills, employee_properties=employee_properties, skill_properties=skill_properties, order=order, errors=errors, is_admin=is_admin) path = os.path.join(os.path.dirname(__file__),'../views/employees/view.html') self.response.out.write(template.render(path, args)) # Now flush any errors errors done = appcache.set_employee_create_errors(uuid, {})
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