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 get(self, uuid): try: uuid = str(uuid) api = utils.get_linkedin_api(uuid) # Get Request Token result = api.request_token() if result == False: raise api.get_error() # Generate Authorisation URL authorisation_url = api.get_authorize_url(request_token=api._request_token) # Store the request token and request token secret against the Employee datastore.set_employee(uuid, dict(oauth_request_token=api._request_token, oauth_request_token_secret=api._request_token_secret)) # Issue the Email to the Employee mailservice.send_linkedin_connection_request(uuid, authorisation_url) # If all is well, redirect back to the Employee view self.redirect('/employees/'+uuid) except (Exception), e: logging.error('LinkedInOAuthRequestController : get() : caught exception') logging.error(e) self.error(500)
def get_employee_profile(uuid): try: employee_profile = None # Get the Employee employee = model_employee.Employee.get_by_key_name(uuid) if employee is None: logging.error('LinkedInGetSkillsController : get() : No Employee with UUID '+uuid+' found.') self.error(500) return # Recreate the API instance, then set the API access token/secret to be the stored Employee access token/secret api = get_linkedin_api(uuid) if api is False: return False api._access_token = employee.access_token api._access_token_secret = employee.access_token_secret # Get current User profile employee_profile = api.get_profile(fields=['id,public-profile-url,first-name,last-name,skills:(id,skill:(name),proficiency:(level,name),years:(id,name))']) logging.debug(employee_profile) # Save the LinkedIn Skills list if employee_profile.skills is not None and len(employee_profile.skills) > 0: skill_keys = datastore.set_employee_skills(uuid, employee_profile.skills) # Save the OAuth Verifier and the Skills against the Employee datastore.set_employee(uuid, dict(skills=skill_keys)) return True except (Exception), e: raise e
def get(self, uuid): try: is_admin = users.is_current_user_admin() # NOTE: The URL will have a query string appended : "/oauth-response/EMPLOYEE_UUID?oauth_token=21903a3c-b69e-4c55-a7a7-425474a84bfd&oauth_verifier=17604" uuid = str(uuid) employee_profile = None # Get the Employee employee = model_employee.Employee.get_by_key_name(uuid) if employee is None: logging.error('LinkedInOAuthResponseController : get() : No Employee with UUID '+uuid+' found.') self.error(500) return # Recreate the API instance, then set the API request token/secret to be the stored Employee request token/secret api = utils.get_linkedin_api(uuid) api._request_token = employee.oauth_request_token api._request_token_secret = employee.oauth_request_token_secret # The OAuth Token should match the one we sent oauth_token = str(self.request.get('oauth_token')) # The Verifier is needed for future API calls for this Employee oauth_verifier = str(self.request.get('oauth_verifier')) # Get Access Token result = api.access_token(verifier=oauth_verifier) if result == False: logging.error('LinkedInOAuthResponseController : get() : '+api.get_error()) self.error(500) return # Get current User profile employee_profile = api.get_profile(fields=['id,first-name,last-name,skills:(id,skill:(name),proficiency:(level,name),years:(id,name)),picture-url']) logging.debug(employee_profile) # Save the LinkedIn Skills list skill_keys = list() if employee_profile.skills is not None and len(employee_profile.skills) > 0: skill_keys = datastore.set_employee_skills(uuid, employee_profile.skills) # Save the OAuth Verifier and the Skills against the Employee datastore.set_employee(uuid, dict(oauth_verifier=oauth_verifier, access_token=api._access_token, access_token_secret=api._access_token_secret, skills=skill_keys, picture_url=employee_profile.picture_url)) # Send the Employee to the OAuth Response view args = dict(env=os.environ, employee=employee, is_admin=is_admin) path = os.path.join(os.path.dirname(__file__),'../views/employees/oauth-response.html') self.response.out.write(template.render(path, args)) except (Exception), e: logging.error('LinkedInOAuthResponseController : get() : caught exception') logging.error(e) self.error(500)
def post(self): try: is_admin = users.is_current_user_admin() employee_properties = model_employee.Employee.properties() uuid = None if self.request.get('id') is not None and self.request.get('id') != '': uuid = str(self.request.get('id')) arguments = dict() for arg in self.request.arguments(): arguments[str(arg)] = str(self.request.get(arg)).strip().strip('<!>') # Validate the form input errors = utils.validate_employee_input(arguments) if isinstance(errors, dict): logging.debug('CreateEmployeeController : post() : Errors found for Employee create.') args = dict(env=os.environ, employee_properties=employee_properties, errors=errors, is_admin=is_admin) path = os.path.join(os.path.dirname(__file__),'../views/employees/create.html') self.response.out.write(template.render(path, args)) else: logging.debug('CreateEmployeeController : post() : Validated, so saving employee') uuid = datastore.set_employee(uuid, arguments) # Redirect to the Employee view self.redirect('/employees/'+uuid) except (Exception), e: logging.error('CreateEmployeeController : post()') logging.error(e) self.error(500)