Skip to content

Commit

Permalink
Merge pull request #163 from IDEMSInternational/iss161
Browse files Browse the repository at this point in the history
RapidPro: Add support to remove from all groups if left blank
  • Loading branch information
fagiothree authored Feb 21, 2025
2 parents be704ce + 922c5bb commit 73c4d22
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/rpft/parsers/creation/flowparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,17 @@ def _get_row_action(self, row):
scheme=row.urn_scheme or "tel",
)
elif row.type == "remove_from_group":
return RemoveContactGroupAction(
groups=[self._get_or_create_group(row.mainarg_groups[0], row.obj_id)]
)
if not row.mainarg_groups:
LOGGER.warning(f"Removing contact from ALL groups.")
return RemoveContactGroupAction(groups=[], all_groups=True)
elif row.mainarg_groups[0] == "ALL":
return RemoveContactGroupAction(groups=[], all_groups=True)
else:
return RemoveContactGroupAction(
groups=[
self._get_or_create_group(row.mainarg_groups[0], row.obj_id)
]
)
elif row.type == "save_flow_result":
return SetRunResultAction(
row.save_name, row.mainarg_value, category=row.result_category
Expand Down Expand Up @@ -551,7 +559,7 @@ def _get_or_create_group(self, name, uuid=None):
def _get_row_node(self, row):
if (
row.type in ["add_to_group", "remove_from_group", "split_by_group"]
and row.obj_id
and row.obj_id and row.mainarg_groups
):
self.rapidpro_container.record_group_uuid(row.mainarg_groups[0], row.obj_id)

Expand Down
6 changes: 3 additions & 3 deletions src/rpft/rapidpro/models/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,17 @@ def assign_global_uuids(self, uuid_dict):
group.assign_uuid(uuid_dict)

def main_value(self):
return self.groups[0].name
return self.groups[0].name if self.groups else ""

def render(self):
return NotImplementedError

def get_row_model_fields(self):
# abstract method
obj_id = [group.uuid for group in self.groups][0] if self.groups else ""
return {
"mainarg_groups": [group.name for group in self.groups],
"obj_id": [group.uuid for group in self.groups][0]
or "", # 0th element as obj_id is not yet a list.
"obj_id": obj_id or "", # 0th element as obj_id is not yet a list.
}


Expand Down
1 change: 1 addition & 0 deletions tests/input/no_switch_nodes.csv
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
9,"set_contact_language",8,,,,,,"eng",,,,,,,,,,,,,,,,,,"711ea340-dc20-4471-a0e4-6af2c4a278b9",,"execute_actions","280;1020"
10,"set_contact_name",9,,,,,,"John Doe",,,,,,,,,,,,,,,,,,"711ea340-dc20-4471-a0e4-6af2c4a278b9",,"execute_actions","280;1020"
11,"add_contact_urn",10,,,,,,"@results.internation_phone_number",,,,,,,,,,,,,,,,,,"768afc12-93e8-4cf8-9cdc-c92cce36c365",,,
12,"remove_from_group",11,,,,,,,,,,,,,,,,,,,,,,,,"12340fc2-f28c-4ad0-8c02-4afd63ad31ab",,,
1 change: 1 addition & 0 deletions tests/input/no_switch_nodes_without_row_ids.csv
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
,"set_contact_language",,,,,,,"eng",,,,,,,,,,,,,,,,,"711ea340-dc20-4471-a0e4-6af2c4a278b9",,"execute_actions","280;1020"
,"set_contact_name",,,,,,,"John Doe",,,,,,,,,,,,,,,,,"711ea340-dc20-4471-a0e4-6af2c4a278b9",,"execute_actions","280;1020"
,"add_contact_urn",,,,,,,"@results.internation_phone_number",,,,,,,,,,,,,,,,,"768afc12-93e8-4cf8-9cdc-c92cce36c365",,,
,"remove_from_group",,,,,,,,,,,,,,,,,,,,,,,,"12340fc2-f28c-4ad0-8c02-4afd63ad31ab",,,
17 changes: 17 additions & 0 deletions tests/output/all_test_flows.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@
"exits": [
{
"uuid": "51548997-3f36-487d-9498-b48e33907b49",
"destination_uuid": "12340fc2-f28c-4ad0-8c02-4afd63ad31ab"
}
]
},
{
"uuid": "12340fc2-f28c-4ad0-8c02-4afd63ad31ab",
"actions": [
{
"type": "remove_contact_groups",
"groups": [],
"all_groups": true,
"uuid": "1234d6d0-02a2-4edf-9025-8e00959e4d05"
}
],
"exits": [
{
"uuid": "d9482afc-9802-4c49-87e0-6b6512b19632",
"destination_uuid": null
}
]
Expand Down
12 changes: 9 additions & 3 deletions tests/test_flowparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_no_switch_node_rows(self):
nodes = output["nodes"]
all_node_uuids = [row.node_uuid for row in self.rows]
# Rows 0,1,2,3 and rows -3,-2 are actions joined into a single node.
expected_node_uuids = all_node_uuids[3:-2] + all_node_uuids[-1:]
expected_node_uuids = all_node_uuids[3:-3] + all_node_uuids[-2:]
self.assertEqual(
expected_node_uuids,
[node["uuid"] for node in nodes],
Expand All @@ -165,7 +165,7 @@ def test_no_switch_node_rows(self):
self.assertEqual(output["type"], "messaging")
self.assertEqual(output["language"], "eng")

self.assertEqual(len(nodes), 7)
self.assertEqual(len(nodes), 8)

node_0 = nodes[0]
self.assertEqual(len(node_0["actions"]), 4)
Expand Down Expand Up @@ -239,7 +239,13 @@ def test_no_switch_node_rows(self):

node_6 = nodes[6]
self.assertEqual(node_5["exits"][0]["destination_uuid"], node_6["uuid"])
self.assertIsNone(node_6["exits"][0]["destination_uuid"])

node_7 = nodes[7]
self.assertEqual(len(node_7["actions"]), 1)
self.assertEqual("remove_contact_groups", node_7["actions"][0]["type"])
self.assertEqual(len(node_7["actions"][0]["groups"]), 0)
self.assertTrue(node_7["actions"][0]["all_groups"])
self.assertIsNone(node_7["exits"][0]["destination_uuid"])

# Check UI positions/types of the first two nodes
render_ui = output["_ui"]["nodes"]
Expand Down
4 changes: 3 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def find_destination_uuid(current_node, context):
"enter_flow": (lambda x: x["flow"]["name"]),
"open_ticket": (lambda x: x["subject"]),
"play_audio": (lambda x: x["audio_url"]),
"remove_contact_groups": (lambda x: x["groups"][0]["name"]),
"remove_contact_groups": (
lambda x: x["groups"][0]["name"] if x["groups"] else "ALL"
),
"say_msg": (lambda x: x["text"]),
"send_broadcast": (lambda x: x["text"]),
"send_email": (lambda x: x["subject"]),
Expand Down

0 comments on commit 73c4d22

Please sign in to comment.