Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IA-3776 Add OU change requests to bulk upload #1859

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions iaso/tasks/process_mobile_bulk_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
from hat.sync.views import create_instance_file, process_instance_file
from iaso.api.instances import import_data as import_instances
from iaso.api.mobile.org_units import import_data as import_org_units
from iaso.api.org_unit_change_requests.serializers import OrgUnitChangeRequestWriteSerializer
from iaso.api.org_unit_change_requests.views import OrgUnitChangeRequestViewSet
from iaso.api.storage import import_storage_logs
from iaso.models import Project, Instance
from iaso.utils.s3_client import download_file

INSTANCES_JSON = "instances.json"
ORG_UNITS_JSON = "orgUnits.json"
STORAGE_LOGS_JSON = "storageLogs.json"
OU_CHANGE_REQUESTS_JSON = "changeRequests.json"

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,6 +98,15 @@ def process_mobile_bulk_upload(api_import_id, project_id, task=None):
storage_logs_data = read_json_file_from_zip(zip_ref, STORAGE_LOGS_JSON)
import_storage_logs(storage_logs_data, user)

if OU_CHANGE_REQUESTS_JSON in zip_ref.namelist():
logger.info("Processing OU change requests")
ou_change_requests_data = read_json_file_from_zip(zip_ref, OU_CHANGE_REQUESTS_JSON)
for ou_cr in ou_change_requests_data:
serializer = OrgUnitChangeRequestWriteSerializer(data=ou_cr)
serializer.is_valid()
# TODO: this doesn't work (yet)
OrgUnitChangeRequestViewSet().perform_create(serializer)

except Exception as e:
logger.exception("Exception! Rolling back import: " + str(e))
api_import.has_problem = True
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"uuid": "cb420acf-5443-4f5a-8ae9-ce9a271f1d10",
"org_unit_id": "1",
"created_at": 1.733226351453e9,
"updated_at": 1.733226351453e9,
"new_name": "KASAI One",
"new_groups": [],
"new_aliases": []
}
]
36 changes: 33 additions & 3 deletions iaso/tests/tasks/test_process_mobile_bulk_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,7 @@ def test_storage_logs(self, mock_download_file):
self.assertEqual(self.api_import.import_type, "bulk")
self.assertFalse(self.api_import.has_problem)

# Instances (Submissions) + Entity were created
self.assertEqual(m.Entity.objects.count(), 1)
entity = m.Entity.objects.get(uuid=entity_uuid)
self.assertEqual(m.Instance.objects.count(), 1)
instance = m.Instance.objects.get(uuid=entity_uuid)

# Storage logs
Expand All @@ -672,3 +669,36 @@ def test_storage_logs(self, mock_download_file):
self.assertEqual(write_log.org_unit_id, 1)
self.assertEqual(write_log.entity, entity)
self.assertEqual(list(write_log.instances.all()), [instance])

def test_change_requests(self, mock_download_file):
entity_uuid = "5475bfcf-5a3f-4170-9d88-245d89352362"
files_for_zip = [
"instances.json",
"changeRequests.json",
entity_uuid, # the folder with XML submission
]
with zipfile.ZipFile(f"/tmp/{entity_uuid}.zip", "w", zipfile.ZIP_DEFLATED) as zipf:
add_to_zip(zipf, zip_fixture_dir("storage_logs_and_change_requests"), files_for_zip)

mock_download_file.return_value = f"/tmp/{entity_uuid}.zip"

self.assertEqual(m.OrgUnitChangeRequest.objects.count(), 0)

process_mobile_bulk_upload(
api_import_id=self.api_import.id,
project_id=self.project.id,
task=self.task,
_immediate=True,
)

mock_download_file.assert_called_once()

# check Task status and result
self.task.refresh_from_db()
self.assertEqual(self.task.status, m.SUCCESS)
self.api_import.refresh_from_db()
self.assertEqual(self.api_import.import_type, "bulk")
self.assertFalse(self.api_import.has_problem)

# Org Unit Change Requests
self.assertEqual(m.OrgUnitChangeRequest.objects.count(), 1)
Loading