Skip to content

Commit

Permalink
Split replies into batches to avoid exceeding API list size in RapidP…
Browse files Browse the repository at this point in the history
…ro backend
  • Loading branch information
rowanseymour committed Oct 8, 2016
1 parent 417eaa8 commit 12c4cbb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
31 changes: 17 additions & 14 deletions casepro/backend/rapidpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,23 @@ def push_outgoing(self, org, outgoing, as_broadcast=False):
return

if as_broadcast:
contact_uuids = []
urns = []
for msg in for_backend:
if msg.contact:
contact_uuids.append(msg.contact.uuid)
if msg.urn:
urns.append(msg.urn)
text = outgoing[0].text
broadcast = client.create_broadcast(text=text, contacts=contact_uuids, urns=urns)

for msg in for_backend:
msg.backend_broadcast_id = broadcast.id

Outgoing.objects.filter(pk__in=[o.id for o in for_backend]).update(backend_broadcast_id=broadcast.id)
# we might not be able to send all as a single broadcast, so we batch
for batch in chunks(for_backend, self.BATCH_SIZE):
contact_uuids = []
urns = []

for msg in batch:
if msg.contact:
contact_uuids.append(msg.contact.uuid)
if msg.urn:
urns.append(msg.urn)
text = outgoing[0].text
broadcast = client.create_broadcast(text=text, contacts=contact_uuids, urns=urns)

for msg in batch:
msg.backend_broadcast_id = broadcast.id

Outgoing.objects.filter(pk__in=[o.id for o in batch]).update(backend_broadcast_id=broadcast.id)
else:
for msg in for_backend:
contact_uuids = [msg.contact.uuid] if msg.contact else []
Expand Down
22 changes: 22 additions & 0 deletions casepro/backend/tests/test_rapidpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,28 @@ def test_push_outgoing(self, mock_create_broadcast, mock_send_raw_email):

self.assertNotCalled(mock_create_broadcast)

# test when trying to send more than 100 messages
mock_create_broadcast.side_effect = [
TembaBroadcast.create(id=205, text="That's great"),
TembaBroadcast.create(id=206, text="That's great")
]

big_send = []
for o in range(105):
big_send.append(self.create_outgoing(self.unicef, self.user1, None, 'B', "Hello", None, urn="tel:%d" % o))

self.backend.push_outgoing(self.unicef, big_send, as_broadcast=True)

# should be sent as two batches
batch1_urns = ["tel:%d" % o for o in range(0, 99)]
batch2_urns = ["tel:%d" % o for o in range(99, 105)]

mock_create_broadcast.assert_has_calls([
call(text="Hello", contacts=[], urns=batch1_urns),
call(text="Hello", contacts=[], urns=batch2_urns)
])
mock_create_broadcast.reset_mock()

@patch('dash.orgs.models.TembaClient2.bulk_add_contacts')
def test_add_to_group(self, mock_add_contacts):
self.backend.add_to_group(self.unicef, self.bob, self.reporters)
Expand Down

0 comments on commit 12c4cbb

Please sign in to comment.