def test_newsletters_cached(self): models.Newsletter.objects.create( slug="slug", title="title", vendor_id="VEND1", active=False, languages="en-US, fr, de " ) # This should get the data cached newsletter_fields() # Now request it again and it shouldn't have to generate the # data from scratch. with patch("news.newsletters._get_newsletters_data") as get: newsletter_fields() self.assertFalse(get.called)
def test_cache_clear_on_delete(self): # Our caching of newsletter data doesn't result in wrong answers # when newsletters are deleted nl1 = models.Newsletter.objects.create( slug="slug", title="title", vendor_id="VEND1", active=False, languages="en-US, fr, de " ) vendor_ids = newsletter_fields() self.assertEqual([u"VEND1"], vendor_ids) # Now delete it nl1.delete() vendor_ids = newsletter_fields() self.assertEqual([], vendor_ids)
def test_cache_clearing(self): # Our caching of newsletter data doesn't result in wrong answers # when newsletters change models.Newsletter.objects.create( slug="slug", title="title", vendor_id="VEND1", active=False, languages="en-US, fr, de " ) vendor_ids = newsletter_fields() self.assertEqual([u"VEND1"], vendor_ids) # Now add another newsletter models.Newsletter.objects.create( slug="slug2", title="title2", vendor_id="VEND2", active=False, languages="en-US, fr, de " ) vendor_ids2 = set(newsletter_fields()) self.assertEqual(set([u"VEND1", u"VEND2"]), vendor_ids2)
def test_newsletters_cached(self): models.Newsletter.objects.create( slug='slug', title='title', vendor_id='VEND1', active=False, languages='en-US, fr, de ', ) # This should get the data cached newsletter_fields() # Now request it again and it shouldn't have to generate the # data from scratch. with patch('news.newsletters._get_newsletters_data') as get: newsletter_fields() self.assertFalse(get.called)
def test_cache_clear_on_delete(self): # Our caching of newsletter data doesn't result in wrong answers # when newsletters are deleted nl1 = models.Newsletter.objects.create( slug='slug', title='title', vendor_id='VEND1', active=False, languages='en-US, fr, de ', ) vendor_ids = newsletter_fields() self.assertEqual([u'VEND1'], vendor_ids) # Now delete it nl1.delete() vendor_ids = newsletter_fields() self.assertEqual([], vendor_ids)
def test_cache_clearing(self): # Our caching of newsletter data doesn't result in wrong answers # when newsletters change models.Newsletter.objects.create( slug='slug', title='title', vendor_id='VEND1', active=False, languages='en-US, fr, de ', ) vendor_ids = newsletter_fields() self.assertEqual([u'VEND1'], vendor_ids) # Now add another newsletter models.Newsletter.objects.create( slug='slug2', title='title2', vendor_id='VEND2', active=False, languages='en-US, fr, de ', ) vendor_ids2 = set(newsletter_fields()) self.assertEqual(set([u'VEND1', u'VEND2']), vendor_ids2)
def get_user_data(token=None, email=None, sync_data=False): """Return a dictionary of the user's data from Exact Target. Look them up by their email if given, otherwise by the token. If sync_data is set, create or update our local basket record if needed so we have a record of this email and the token that goes with it. Look first for the user in the master subscribers database, then in the optin database. If they're not in the master subscribers database but are in the optin database, then check the confirmation database too. If we find them in either the master subscribers or confirmation database, add 'confirmed': True to their data; otherwise, 'confirmed': False. Also, ['pending'] is True if they are in the double-opt-in database and not in the confirmed or master databases. If the user was not found, return None instead of a dictionary. If there was an error, result['status'] == 'error' and result['desc'] has more info; otherwise, result['status'] == 'ok' Review of results: None = user completely unknown, no errors talking to ET. otherwise, return value is:: { 'status': 'ok', # no errors talking to ET 'status': 'error', # errors talking to ET, see next field 'desc': 'error message' # details if status is error 'email': 'email@address', 'format': 'T'|'H', 'country': country code, 'lang': language code, 'token': UUID, 'created-date': date created, 'newsletters': list of slugs of newsletters subscribed to, 'confirmed': True if user has confirmed subscription (or was excepted), 'pending': True if we're waiting for user to confirm subscription 'master': True if we found them in the master subscribers table } """ newsletters = newsletter_fields() fields = [ 'EMAIL_ADDRESS_', 'EMAIL_FORMAT_', 'COUNTRY_', 'LANGUAGE_ISO2', 'TOKEN', 'CREATED_DATE_', ] for nl in newsletters: fields.append('%s_FLG' % nl) confirmed = True pending = False master = True try: # Look first in the master subscribers database for the user user_data = look_for_user(settings.EXACTTARGET_DATA, email, token, fields) # If we get back a user, then they have already confirmed. # If not, look for them in the database of unconfirmed users. if user_data is None: master = False confirmed = False user_data = look_for_user(settings.EXACTTARGET_OPTIN_STAGE, email, token, fields) if user_data is None: # No such user, as far as we can tell - if they're in # neither the master subscribers nor optin database, # we don't know them. return None # We found them in the optin database. But actually, they # might have confirmed but the batch job hasn't # yet run to move their data to the master subscribers # database; catch that case here by looking for them in the # Confirmed database. Do it simply; the confirmed database # doesn't have most of the user's data, just their token. if look_for_user(settings.EXACTTARGET_CONFIRMATION, None, user_data['token'], ['Token']): # Ah-ha, they're in the Confirmed DB so they did confirm confirmed = True else: # They're in the optin db, but not confirmed, so we wait pending = True user_data['confirmed'] = confirmed user_data['pending'] = pending user_data['master'] = master except NewsletterException as e: raise NewsletterException(str(e), error_code=errors.BASKET_NETWORK_FAILURE, status_code=400) except UnauthorizedException: raise NewsletterException('Email service provider auth failure', error_code=errors.BASKET_EMAIL_PROVIDER_AUTH_FAILURE, status_code=500) # We did find a user if sync_data: # if user not in our db create it, if token mismatch fix it. Subscriber.objects.get_and_sync(user_data['email'], user_data['token']) return user_data