Skip to content

Commit e278030

Browse files
authored
Merge pull request #130 from AfricasVoices/add_get_groups
add function to get rapidpro groups
2 parents 3ae29d4 + 8d2014b commit e278030

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

rapid_pro_tools/rapid_pro_client.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,48 @@ def get_raw_messages(self, created_after_inclusive=None, created_before_exclusiv
272272

273273
return raw_messages
274274

275+
def get_groups(self, uuid=None, name=None):
276+
"""
277+
Gets all matching contact groups from a rapid_pro workspace
278+
279+
:param uuid: group UUID to filter on. If None, returns all groups in the workspace.
280+
:type uuid: str | None
281+
:param name: group name to filter on. If None, returns all groups in the workspace.
282+
:type group name: str | None
283+
:return: List of groups matching the group query
284+
:rtype: list of temba_client.v2.types.Group
285+
"""
286+
return [group for group in self.rapid_pro.get_groups(uuid=uuid, name=name).all(retry_on_rate_exceed=True)]
287+
288+
def get_contacts(self, uuid=None, urn=None, group=None, deleted=None, before=None, after=None, reverse=None):
289+
"""
290+
Gets all matching contacts from a rapid_pro workspace
291+
292+
:param uuid: contact UUID to filter on. If None, returns all contacts in the workspace.
293+
:type uuid: str | None
294+
:param urn: contact URN
295+
:type urn: str | None
296+
:param group: contact group name or UUID to filter on. If None, returns all groups in the workspace.
297+
:type group: str | None
298+
:param deleted: return deleted contact only
299+
:type deleted: bool | None
300+
:param reverse: whether to return contacts ordered in reverse (oldest first).
301+
:type reverse: bool | None
302+
:param last_modified_before_exclusive: Start of the date-range to download contacts from.
303+
If set, only downloads messages modified on Rapid Pro before that date,
304+
otherwise downloads from the beginning of time.
305+
:type last_modified_before_exclusive: datetime
306+
:param last_modified_after_inclusive: Start of the date-range to download contacts from.
307+
If set, only downloads messages modified on Rapid Pro after that date,
308+
otherwise downloads from the beginning of time.
309+
:type last_modified_after_inclusive: datetime
310+
:return: List of contacts who match the query
311+
:rtype: list of temba_client.v2.types.Group
312+
"""
313+
return [contact for contact in self.rapid_pro.get_contacts(uuid=uuid, urn=urn, group=group, deleted=deleted,
314+
before=before, after=after, reverse=reverse).all(
315+
retry_on_rate_exceed=True)]
316+
275317
def send_message_to_urn(self, message, target_urn, interrupt=False):
276318
"""
277319
Sends a message to the given URN.
@@ -656,7 +698,7 @@ def update_raw_runs_with_latest_modified(self, flow_id, prev_raw_runs=None, raw_
656698
lambda run: run.id, prev_raw_data=prev_raw_runs, raw_export_log_file=raw_export_log_file
657699
)
658700

659-
def update_contact(self, urn, name=None, contact_fields=None):
701+
def update_contact(self, urn, name=None, contact_fields=None, groups=None):
660702
"""
661703
Updates a contact on the server.
662704
@@ -667,8 +709,12 @@ def update_contact(self, urn, name=None, contact_fields=None):
667709
:param contact_fields: Dictionary of field key to new field value | None. If None, no keys are updated.
668710
Keys present on the server contact but not in this dictionary are left unchanged.
669711
:type contact_fields: (dict of str -> str) | None
712+
:param groups: list of group objects or UUIDs. This will overwrite the groups in rapid_pro. If you intend to add
713+
fetch the contact and append the group to the existing groups list.
714+
:type groups: list | None
670715
"""
671-
return self._retry_on_rate_exceed(lambda: self.rapid_pro.update_contact(urn, name=name, fields=contact_fields))
716+
return self._retry_on_rate_exceed(lambda: self.rapid_pro.update_contact(urn, name=name,
717+
fields=contact_fields, groups=groups))
672718

673719
def get_fields(self):
674720
"""
@@ -726,6 +772,18 @@ def create_field(self, label, field_id=None):
726772

727773
return rapid_pro_field
728774

775+
def create_group(self, name):
776+
"""
777+
Creates a new contact group in a rapid_pro workspace. If the group exists, rapid pro will add a suffix 1,2.. and
778+
create a group with the modified name.
779+
780+
:param name: group name.
781+
:type name: str.
782+
:return: the new group.
783+
:rtype: temba_client.v2.types.Group
784+
"""
785+
return self._retry_on_rate_exceed(lambda: self.rapid_pro.create_group(name=name))
786+
729787
@classmethod
730788
def _retry_on_rate_exceed(cls, request):
731789
"""

0 commit comments

Comments
 (0)