From 66290b42b8559c94d2bd548eb9b7b7834dabdc85 Mon Sep 17 00:00:00 2001 From: Fleury Butoyi Date: Mon, 11 Nov 2024 16:54:02 +0200 Subject: [PATCH 1/2] Extend org unit associate_favorite_picture function to org units linked to type other than health facility --- setuper/create_submission_with_picture.py | 3 --- setuper/data_collection.py | 3 ++- setuper/org_unit_pictures.py | 31 +++++++++++------------ setuper/setuper.py | 2 ++ 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/setuper/create_submission_with_picture.py b/setuper/create_submission_with_picture.py index 624636912b..d6b4141875 100644 --- a/setuper/create_submission_with_picture.py +++ b/setuper/create_submission_with_picture.py @@ -1,7 +1,6 @@ import uuid from datetime import datetime from submissions import submission2xml, org_unit_gps_point, submission_org_unit_gps_point -from org_unit_pictures import associate_favorite_picture import random @@ -130,5 +129,3 @@ def create_submission_with_picture(account_name, iaso_client): "reference_instance_action": "flag", } iaso_client.patch(f"/api/orgunits/{org_unit_id}/", json=org_unit_reference_submission) - - associate_favorite_picture(iaso_client) diff --git a/setuper/data_collection.py b/setuper/data_collection.py index 05726ee2ba..77b9b2049e 100644 --- a/setuper/data_collection.py +++ b/setuper/data_collection.py @@ -44,7 +44,8 @@ def setup_instances(account_name, iaso_client): # fetch orgunit ids belong to the org unit type for org_unit_type_id in org_unit_type_ids: - limit = 10 + org_unit_type = iaso_client.get(f"/api/v2/orgunittypes/{org_unit_type_id}") + limit = org_unit_type["units_count"] orgunits = iaso_client.get("/api/orgunits/", params={"limit": limit, "orgUnitTypeId": org_unit_type_id})[ "orgunits" ] diff --git a/setuper/org_unit_pictures.py b/setuper/org_unit_pictures.py index def57531fa..d9f90e2a6e 100644 --- a/setuper/org_unit_pictures.py +++ b/setuper/org_unit_pictures.py @@ -3,20 +3,19 @@ def associate_favorite_picture(iaso_client): org_unit_types = iaso_client.get("/api/v2/orgunittypes/")["orgUnitTypes"] - health_facility_type = [out for out in org_unit_types if out["name"] == "Health facility/Formation sanitaire - HF"][ - 0 - ] - orgunits = iaso_client.get( - "/api/orgunits/", - params={"limit": health_facility_type["units_count"], "orgUnitTypeId": health_facility_type["id"]}, - )["orgunits"] - for orgunit in orgunits: - org_unit_id = orgunit["id"] - linked_pictures = iaso_client.get( - "/api/instances/attachments/", params={"orgUnitId": org_unit_id, "image_only": True} - ) - picture_ids = [picture["id"] for picture in linked_pictures] - default_image = random.choice(picture_ids) - favorite_image = {"default_image": default_image} - iaso_client.patch(f"/api/orgunits/{org_unit_id}/", json=favorite_image) + for org_unit_type in org_unit_types: + orgunits = iaso_client.get( + "/api/orgunits/", + params={"limit": org_unit_type["units_count"], "orgUnitTypeId": org_unit_type["id"]}, + )["orgunits"] + + for orgunit in orgunits: + org_unit_id = orgunit["id"] + linked_pictures = iaso_client.get( + "/api/instances/attachments/", params={"orgUnitId": org_unit_id, "image_only": True} + ) + picture_ids = [picture["id"] for picture in linked_pictures] + default_image = random.choice(picture_ids) + favorite_image = {"default_image": default_image} + iaso_client.patch(f"/api/orgunits/{org_unit_id}/", json=favorite_image) diff --git a/setuper/setuper.py b/setuper/setuper.py index b9baabf3ca..3a084457ca 100644 --- a/setuper/setuper.py +++ b/setuper/setuper.py @@ -11,6 +11,7 @@ from create_submission_with_picture import create_submission_with_picture from additional_projects import create_projects, link_new_projects_to_main_data_source from user_roles_permissions import create_user_role +from org_unit_pictures import associate_favorite_picture import string import random import argparse @@ -94,6 +95,7 @@ def create_account( if seed_instances: setup_instances(account_name, iaso_client=iaso_client) + associate_favorite_picture(iaso_client=iaso_client) if seed_entities: setup_entities(account_name, iaso_client=iaso_client) From 2952e10276a532e01ccfe748c8ae4c1a0ea1f7e9 Mon Sep 17 00:00:00 2001 From: Fleury Butoyi Date: Tue, 12 Nov 2024 15:25:25 +0200 Subject: [PATCH 2/2] Add reference submissions for Areas, Districts, Regions, and Country --- setuper/create_submission_with_picture.py | 23 ++++++++--------------- setuper/data_collection.py | 21 ++++++++++++++++++--- setuper/submissions.py | 18 +++++++++++++++++- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/setuper/create_submission_with_picture.py b/setuper/create_submission_with_picture.py index d6b4141875..d43f7d5fc0 100644 --- a/setuper/create_submission_with_picture.py +++ b/setuper/create_submission_with_picture.py @@ -1,6 +1,11 @@ import uuid from datetime import datetime -from submissions import submission2xml, org_unit_gps_point, submission_org_unit_gps_point +from submissions import ( + submission2xml, + org_unit_gps_point, + submission_org_unit_gps_point, + create_default_reference_submission, +) import random @@ -115,17 +120,5 @@ def create_submission_with_picture(account_name, iaso_client): "photo_fosa": fp_image, }, ) - # Adding default reference to the org unit - submission_linked_to_org_units = iaso_client.get( - f"/api/instances/?app_id={account_name}", - params={"orgUnitId": org_unit_id, "form_ids": form_id}, - )["instances"] - reference_submission = [ - submission["id"] for submission in submission_linked_to_org_units if submission["uuid"] == the_uuid - ] - org_unit_reference_submission = { - "id": org_unit_id, - "reference_instance_id": reference_submission[0], - "reference_instance_action": "flag", - } - iaso_client.patch(f"/api/orgunits/{org_unit_id}/", json=org_unit_reference_submission) + # Creating default reference submission for the org unit + create_default_reference_submission(account_name, iaso_client, org_unit_id, form_id, the_uuid) diff --git a/setuper/data_collection.py b/setuper/data_collection.py index 77b9b2049e..fc36dfb9d9 100644 --- a/setuper/data_collection.py +++ b/setuper/data_collection.py @@ -1,6 +1,11 @@ import uuid from datetime import datetime, timedelta -from submissions import submission2xml, org_unit_gps_point, picture_by_org_unit_type_name +from submissions import ( + submission2xml, + org_unit_gps_point, + picture_by_org_unit_type_name, + create_default_reference_submission, +) import random @@ -41,10 +46,17 @@ def setup_instances(account_name, iaso_client): ######## creating submissions/instances print("-- Downloading org units") - # fetch orgunit ids belong to the org unit type - for org_unit_type_id in org_unit_type_ids: org_unit_type = iaso_client.get(f"/api/v2/orgunittypes/{org_unit_type_id}") + org_unit_type["reference_forms_ids"] = [form_id] + org_unit_type["project_ids"] = [project["id"] for project in org_unit_type["projects"]] + org_unit_type["sub_unit_type_ids"] = [sub_unit["id"] for sub_unit in org_unit_type["sub_unit_types"]] + org_unit_type["allow_creating_sub_unit_type_ids"] = [ + sub_unit_type["id"] for sub_unit_type in org_unit_type["allow_creating_sub_unit_types"] + ] + # Update the org unit type with reference form + iaso_client.put(f"/api/v2/orgunittypes/{org_unit_type_id}/", json=org_unit_type) + limit = org_unit_type["units_count"] orgunits = iaso_client.get("/api/orgunits/", params={"limit": limit, "orgUnitTypeId": org_unit_type_id})[ "orgunits" @@ -181,4 +193,7 @@ def setup_instances(account_name, iaso_client): if count % 5 == 0: print("\t%d submissions done" % count) + # Creating default reference submission for the org unit + create_default_reference_submission(account_name, iaso_client, orgunit["id"], form_id, the_uuid) + print(iaso_client.get("/api/instances", params={"limit": 1})["count"], "instances created") diff --git a/setuper/submissions.py b/setuper/submissions.py index 84efb79ab7..a00088dffb 100644 --- a/setuper/submissions.py +++ b/setuper/submissions.py @@ -1,5 +1,5 @@ from dict2xml import dict2xml -import uuid +import uuid, random def submission2xml(submission_dict, form_id, form_version_id, gen_uuid=False): @@ -38,3 +38,19 @@ def picture_by_org_unit_type_name(org_unit_type_name): elif org_unit_type_name == "Health area/Aire de santé - AREA": picture_name = "health area.webp" return picture_name + + +def create_default_reference_submission(account_name, iaso_client, org_unit_id, form_id, uuid): + submissions_linked_to_org_unit = iaso_client.get( + f"/api/instances/?app_id={account_name}", + params={"orgUnitId": org_unit_id, "form_ids": form_id}, + )["instances"] + reference_submission = [ + submission["id"] for submission in submissions_linked_to_org_unit if submission["uuid"] == uuid + ] + org_unit_reference_submission = { + "id": org_unit_id, + "reference_instance_id": random.choice(reference_submission), + "reference_instance_action": "flag", + } + iaso_client.patch(f"/api/orgunits/{org_unit_id}/", json=org_unit_reference_submission)