def test_update_or_create_from_temba(self): temba_contact_field = TembaContactField.create(key='foo', label='Bar', value_type='T') field = ContactField.update_or_create_from_temba(self.nigeria, temba_contact_field) self.assertEqual(field.key, 'foo') self.assertEqual(field.label, 'Bar') updated_field = ContactField.update_or_create_from_temba(self.nigeria, temba_contact_field) self.assertEqual(field.pk, updated_field.pk)
def test_update_or_create_from_temba(self): temba_contact_field = TembaContactField.create(key='foo', label='Bar', value_type='T') field = ContactField.update_or_create_from_temba( self.nigeria, temba_contact_field) self.assertEqual(field.key, 'foo') self.assertEqual(field.label, 'Bar') updated_field = ContactField.update_or_create_from_temba( self.nigeria, temba_contact_field) self.assertEqual(field.pk, updated_field.pk)
def test_get_contact_fields(self): field_keys = ContactField.get_contact_fields(self.nigeria) self.assertEqual(field_keys, ['occupation']) with patch('django.core.cache.cache.get') as cache_get_mock: cache_get_mock.return_value = None field_keys = ContactField.get_contact_fields(self.nigeria) self.assertEqual(field_keys, ['occupation']) cache_get_mock.return_value = ['occupation'] with patch('ureport.contacts.models.ContactField.fetch_contact_fields') as mock_fetch: ContactField.get_contact_fields(self.nigeria) self.assertFalse(mock_fetch.called)
def test_kwargs_from_temba(self): temba_contact_field = TembaContactField.create(key='foo', label='Bar', value_type='T') kwargs = ContactField.kwargs_from_temba(self.nigeria, temba_contact_field) self.assertEqual(kwargs, dict(org=self.nigeria, key='foo', label='Bar', value_type='T')) # try creating contact from them ContactField.objects.create(**kwargs)
def fetch_contacts_task(org_id=None, fetch_all=False): r = get_redis_connection() key = 'fetch_contacts' lock_timeout = 3600 if org_id: key = 'fetch_contacts:%d' % org_id lock_timeout = 300 if not r.get(key): with r.lock(key, timeout=lock_timeout): active_orgs = Org.objects.filter(is_active=True) if org_id: active_orgs = Org.objects.filter(pk=org_id) for org in active_orgs: start = time.time() last_fetched_key = Contact.CONTACT_LAST_FETCHED_CACHE_KEY % org.id after = cache.get(last_fetched_key, None) if after: after = json_date_to_datetime(after) if fetch_all: after = None try: if after is None: Boundary.fetch_boundaries(org) ContactField.fetch_contact_fields(org) Boundary.get_boundaries(org) ContactField.get_contact_fields(org) Contact.fetch_contacts(org, after=after) print "Task: fetch_contacts for %s took %ss" % (org.name, time.time() - start) except Exception as e: import traceback traceback.print_exc() logger.exception("Error fetching contacts: %s" % str(e))
def test_get_contact_fields(self): field_keys = ContactField.get_contact_fields(self.nigeria) self.assertEqual(field_keys, ['occupation']) with patch('django.core.cache.cache.get') as cache_get_mock: cache_get_mock.return_value = None field_keys = ContactField.get_contact_fields(self.nigeria) self.assertEqual(field_keys, ['occupation']) cache_get_mock.return_value = ['occupation'] with patch( 'ureport.contacts.models.ContactField.fetch_contact_fields' ) as mock_fetch: ContactField.get_contact_fields(self.nigeria) self.assertFalse(mock_fetch.called)
def test_kwargs_from_temba(self): temba_contact_field = TembaContactField.create(key='foo', label='Bar', value_type='T') kwargs = ContactField.kwargs_from_temba(self.nigeria, temba_contact_field) self.assertEqual( kwargs, dict(org=self.nigeria, key='foo', label='Bar', value_type='T')) # try creating contact from them ContactField.objects.create(**kwargs)
def test_fetch_contact_fields(self): ContactField.objects.create(org=self.nigeria, key='old', label='Old', value_type='T') field_keys = ContactField.fetch_contact_fields(self.nigeria) self.assertEqual(field_keys, ['occupation']) self.assertIsNone(ContactField.objects.filter(key='old', org=self.nigeria).first()) contact_field = ContactField.objects.get() self.assertEqual(contact_field.org, self.nigeria) self.assertEqual(contact_field.key, 'occupation') self.assertEqual(contact_field.label, 'Activité') self.assertEqual(contact_field.value_type, 'T')
def test_fetch_contact_fields(self): ContactField.objects.create(org=self.nigeria, key='old', label='Old', value_type='T') field_keys = ContactField.fetch_contact_fields(self.nigeria) self.assertEqual(field_keys, ['occupation']) self.assertIsNone( ContactField.objects.filter(key='old', org=self.nigeria).first()) contact_field = ContactField.objects.get() self.assertEqual(contact_field.org, self.nigeria) self.assertEqual(contact_field.key, 'occupation') self.assertEqual(contact_field.label, 'Activité') self.assertEqual(contact_field.value_type, 'T')