def pull_messages(self, org, modified_after, modified_before, as_handled=False, progress_callback=None, resume_cursor: str = None): client = self._get_client(org) # all incoming messages created or modified in RapidPro in the time window query = client.get_messages(folder="incoming", after=modified_after, before=modified_before) fetches = query.iterfetches(retry_on_rate_exceed=True, resume_cursor=resume_cursor) counts, resume_cursor = sync_local_to_changes( org, MessageSyncer(backend=self.backend, as_handled=as_handled), fetches, [], progress_callback, time_limit=self.FETCH_TIME_LIMIT, ) return self._counts(counts) + (resume_cursor, )
def pull_contacts(self, org, modified_after, modified_before, progress_callback=None, resume_cursor: str = None): client = self._get_client(org) # all contacts created or modified in RapidPro in the time window active_query = client.get_contacts(after=modified_after, before=modified_before) fetches = active_query.iterfetches(retry_on_rate_exceed=True, resume_cursor=resume_cursor) # all contacts deleted in RapidPro in the same time window deleted_query = client.get_contacts(deleted=True, after=modified_after, before=modified_before) deleted_fetches = deleted_query.iterfetches(retry_on_rate_exceed=True) counts, resume_cursor = sync_local_to_changes( org, ContactSyncer(backend=self.backend), fetches, deleted_fetches, progress_callback, time_limit=self.FETCH_TIME_LIMIT, ) return self._counts(counts) + (resume_cursor, )
def pull_contacts(self, org, modified_after, modified_before, progress_callback=None): """ Pulls contacts modified in the given time window :param org: the org :param datetime modified_after: pull contacts modified after this :param datetime modified_before: pull contacts modified before this :param progress_callback: callable that will be called from time to time with number of contacts pulled :return: tuple of the number of contacts created, updated, deleted and ignored """ identity_store = self.identity_store # all identities created in the Identity Store in the time window new_identities = identity_store.get_identities( created_from=modified_after, created_to=modified_before) # all identities modified in the Identity Store in the time window modified_identities = identity_store.get_identities( updated_from=modified_after, updated_to=modified_before) identities_to_update = list(chain(modified_identities, new_identities)) # sync_local_to_changes() expects iterables for the 3rd and 4th args # Deleted identities are updated via the Identity Store callback return sync_local_to_changes(org, IdentityStoreContactSyncer(), [identities_to_update], [], progress_callback)
def pull_messages(self, org, modified_after, modified_before, as_handled=False, progress_callback=None): client = self._get_client(org) # all incoming messages created or modified in RapidPro in the time window query = client.get_messages(folder='incoming', after=modified_after, before=modified_before) fetches = query.iterfetches(retry_on_rate_exceed=True) return sync_local_to_changes(org, MessageSyncer(as_handled), fetches, [], progress_callback)
def pull_messages(self, org, modified_after, modified_before, as_handled=False, progress_callback=None): client = self._get_client(org, 2) # all incoming messages created or modified in RapidPro in the time window query = client.get_messages(folder='incoming', after=modified_after, before=modified_before) fetches = query.iterfetches(retry_on_rate_exceed=True) return sync_local_to_changes(org, MessageSyncer(as_handled), fetches, [], progress_callback)
def pull_contacts(self, org, modified_after, modified_before, progress_callback=None): client = self._get_client(org) # all contacts created or modified in RapidPro in the time window active_query = client.get_contacts(after=modified_after, before=modified_before) fetches = active_query.iterfetches(retry_on_rate_exceed=True) # all contacts deleted in RapidPro in the same time window deleted_query = client.get_contacts(deleted=True, after=modified_after, before=modified_before) deleted_fetches = deleted_query.iterfetches(retry_on_rate_exceed=True) return sync_local_to_changes(org, ContactSyncer(), fetches, deleted_fetches, progress_callback)
def pull_contacts(self, org, modified_after, modified_before, progress_callback=None): client = self._get_client(org, 2) # all contacts created or modified in RapidPro in the time window active_query = client.get_contacts(after=modified_after, before=modified_before) fetches = active_query.iterfetches(retry_on_rate_exceed=True) # all contacts deleted in RapidPro in the same time window deleted_query = client.get_contacts(deleted=True, after=modified_after, before=modified_before) deleted_fetches = deleted_query.iterfetches(retry_on_rate_exceed=True) return sync_local_to_changes(org, ContactSyncer(), fetches, deleted_fetches, progress_callback)
def test_sync_local_to_changes(self): Contact.objects.all().delete() # start with no contacts... fetches = MockClientQuery([ TembaContact.create(uuid="C-001", name="Anne", blocked=False), TembaContact.create(uuid="C-002", name="Bob", blocked=False), TembaContact.create(uuid="C-003", name="Colin", blocked=False), TembaContact.create(uuid="C-004", name="Donald", blocked=True) ]) deleted_fetches = MockClientQuery([]) # no deleted contacts this time self.assertEqual(sync_local_to_changes(self.unicef, self.syncer, fetches, deleted_fetches), (3, 0, 0, 1)) fetches = MockClientQuery([ TembaContact.create(uuid="C-005", name="Edward", blocked=False), # new contact TembaContact.create(uuid="C-006", name="Frank", blocked=False), # new contact ]) deleted_fetches = MockClientQuery([ TembaContact.create(uuid="C-001", name=None, blocked=None), # deleted ]) self.assertEqual(sync_local_to_changes(self.unicef, self.syncer, fetches, deleted_fetches), (2, 0, 1, 0)) fetches = MockClientQuery([ TembaContact.create(uuid="C-002", name="Bob", blocked=True), # blocked so locally invalid TembaContact.create(uuid="C-003", name="Colm", blocked=False), # changed name ]) deleted_fetches = MockClientQuery([]) self.assertEqual(sync_local_to_changes(self.unicef, self.syncer, fetches, deleted_fetches), (0, 1, 1, 0)) fetches = MockClientQuery([ TembaContact.create(uuid="CF-001", name="Anne", blocked=False), TembaContact.create(uuid="CF-002", name="Bob", blocked=False), TembaContact.create(uuid="CF-003", name="Colin", blocked=False), TembaContact.create(uuid="CF-004", name="Donald", blocked=True) ]) deleted_fetches = MockClientQuery([]) # no deleted contacts this time self.assertEqual(sync_local_to_changes(self.unicef, self.syncer2, fetches, deleted_fetches), (3, 0, 0, 1)) fetches = MockClientQuery([ TembaContact.create(uuid="CF-005", name="Edward", blocked=False), # new contact TembaContact.create(uuid="CF-006", name="Frank", blocked=False), # new contact ]) deleted_fetches = MockClientQuery([ TembaContact.create(uuid="CF-001", name=None, blocked=None), # deleted ]) self.assertEqual(sync_local_to_changes(self.unicef, self.syncer2, fetches, deleted_fetches), (2, 0, 1, 0)) fetches = MockClientQuery([ TembaContact.create(uuid="CF-002", name="Bob", blocked=True), # blocked so locally invalid TembaContact.create(uuid="CF-003", name="Colm", blocked=False), # changed name ]) deleted_fetches = MockClientQuery([]) self.assertEqual(sync_local_to_changes(self.unicef, self.syncer2, fetches, deleted_fetches), (0, 1, 1, 0))
def pull_contacts(self, org, modified_after, modified_before, progress_callback=None): """ Pulls contacts modified in the given time window :param org: the org :param datetime modified_after: pull contacts modified after this :param datetime modified_before: pull contacts modified before this :param progress_callback: callable that will be called from time to time with number of contacts pulled :return: tuple of the number of contacts created, updated, deleted and ignored """ identity_store = self.identity_store # all identities created in the Identity Store in the time window new_identities = identity_store.get_identities(created_from=modified_after, created_to=modified_before) # all identities modified in the Identity Store in the time window modified_identities = identity_store.get_identities(updated_from=modified_after, updated_to=modified_before) identities_to_update = list(chain(modified_identities, new_identities)) # sync_local_to_changes() expects iterables for the 3rd and 4th args # Deleted identities are updated via the Identity Store callback return sync_local_to_changes(org, IdentityStoreContactSyncer(), [identities_to_update], [], progress_callback)