Skip to content

Commit

Permalink
Add reference submissions for Areas, Districts, Regions, and Country
Browse files Browse the repository at this point in the history
  • Loading branch information
butofleury committed Nov 12, 2024
1 parent 66290b4 commit 2952e10
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
23 changes: 8 additions & 15 deletions setuper/create_submission_with_picture.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
21 changes: 18 additions & 3 deletions setuper/data_collection.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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")
18 changes: 17 additions & 1 deletion setuper/submissions.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 2952e10

Please sign in to comment.