def add_email_marketing_cookies(sender, response=None, user=None, **kwargs): # pylint: disable=unused-argument """ Signal function for adding any cookies needed for email marketing Args: response: http response object user: The user object for the user being changed Returns: response: http response object with cookie added """ email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return response post_parms = { 'id': user.email, 'fields': {'keys': 1}, 'vars': {'last_login_date': datetime.datetime.now().strftime("%Y-%m-%d")} } # get anonymous_interest cookie to capture usage before logon request = crum.get_current_request() if request: sailthru_content = request.COOKIES.get('anonymous_interest') if sailthru_content: post_parms['cookies'] = {'anonymous_interest': sailthru_content} time_before_call = datetime.datetime.now() sailthru_response = get_email_cookies_via_sailthru.delay(user.email, post_parms) try: # synchronous call to get result of an asynchronous celery task, with timeout sailthru_response.get(timeout=email_config.user_registration_cookie_timeout_delay, propagate=True) cookie = sailthru_response.result _log_sailthru_api_call_time(time_before_call) except TimeoutError as exc: log.error("Timeout error while attempting to obtain cookie from Sailthru: %s", unicode(exc)) return response except SailthruClientError as exc: log.error("Exception attempting to obtain cookie from Sailthru: %s", unicode(exc)) return response if not cookie: log.error("No cookie returned attempting to obtain cookie from Sailthru for %s", user.email) return response else: response.set_cookie( 'sailthru_hid', cookie, max_age=365 * 24 * 60 * 60, # set for 1 year domain=settings.SESSION_COOKIE_DOMAIN, path='/', ) log.info("sailthru_hid cookie:%s successfully retrieved for user %s", cookie, user.email) return response
def add_email_marketing_cookies(sender, response=None, user=None, **kwargs): # pylint: disable=unused-argument """ Signal function for adding any cookies needed for email marketing Args: response: http response object user: The user object for the user being changed Returns: response: http response object with cookie added """ email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return response post_parms = { 'id': user.email, 'fields': {'keys': 1}, 'vars': {'last_login_date': datetime.datetime.now().strftime("%Y-%m-%d")} } # get anonymous_interest cookie to capture usage before logon request = crum.get_current_request() if request: sailthru_content = request.COOKIES.get('anonymous_interest') if sailthru_content: post_parms['cookies'] = {'anonymous_interest': sailthru_content} time_before_call = datetime.datetime.now() sailthru_response = get_email_cookies_via_sailthru.delay(user.email, post_parms) try: # synchronous call to get result of an asynchronous celery task, with timeout sailthru_response.get(timeout=email_config.user_registration_cookie_timeout_delay, propagate=True) cookie = sailthru_response.result except TimeoutError as exc: log.error("Timeout error while attempting to obtain cookie from Sailthru: %s", unicode(exc)) return response except SailthruClientError as exc: log.error("Exception attempting to obtain cookie from Sailthru: %s", unicode(exc)) return response if not cookie: log.error("No cookie returned attempting to obtain cookie from Sailthru for %s", user.email) return response else: response.set_cookie( 'sailthru_hid', cookie, max_age=365 * 24 * 60 * 60, # set for 1 year domain=settings.SESSION_COOKIE_DOMAIN, path='/', ) _log_sailthru_api_call_time(time_before_call) return response
def test_get_cookies_via_sailthu(self, mock_sailthru): cookies = {'cookie': 'test_cookie'} mock_sailthru.return_value = SailthruResponse(JsonResponse({'keys': cookies})) post_parms = { 'id': self.user.email, 'fields': {'keys': 1}, 'vars': {'last_login_date': datetime.datetime.now().strftime("%Y-%m-%d")}, 'cookies': {'anonymous_interest': 'cookie_content'} } expected_cookie = get_email_cookies_via_sailthru.delay(self.user.email, post_parms) mock_sailthru.assert_called_with('user', {'fields': {'keys': 1}, 'cookies': {'anonymous_interest': 'cookie_content'}, 'id': TEST_EMAIL, 'vars': {'last_login_date': ANY}}) self.assertEqual(cookies['cookie'], expected_cookie.result)