Skip to content

Commit

Permalink
fix: org unit favorite image and reference submissions for Areas, Dis…
Browse files Browse the repository at this point in the history
…tricts, Regions, and Country


Refs: IA-3593, IA-3584
  • Loading branch information
butofleury authored Nov 14, 2024
2 parents 3e357cd + 2952e10 commit c2e52be
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
26 changes: 8 additions & 18 deletions setuper/create_submission_with_picture.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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
from submissions import (
submission2xml,
org_unit_gps_point,
submission_org_unit_gps_point,
create_default_reference_submission,
)
import random


Expand Down Expand Up @@ -116,19 +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)

associate_favorite_picture(iaso_client)
# Creating default reference submission for the org unit
create_default_reference_submission(account_name, iaso_client, org_unit_id, form_id, the_uuid)
24 changes: 20 additions & 4 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,18 @@ 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:
limit = 10
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 @@ -180,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")
31 changes: 15 additions & 16 deletions setuper/org_unit_pictures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions setuper/setuper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
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 c2e52be

Please sign in to comment.