From 19ac35980761639eeb993e2159649014b43b0b55 Mon Sep 17 00:00:00 2001 From: Wambere Date: Thu, 7 Mar 2024 17:03:41 +0300 Subject: [PATCH] Add optional geometry to location --- importer/csv/locations/locations_full.csv | 8 ++++---- importer/json_payloads/locations_payload.json | 4 ++++ importer/main.py | 18 +++++++++++++++++- importer/test_main.py | 8 ++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/importer/csv/locations/locations_full.csv b/importer/csv/locations/locations_full.csv index c810742c..6b50d2eb 100644 --- a/importer/csv/locations/locations_full.csv +++ b/importer/csv/locations/locations_full.csv @@ -1,4 +1,4 @@ -name,status,method,id,parentName,parentID,type,typeCode,physicalType,physicalTypeCode -City1,active,update,ba787982-b973-4bd5-854e-eacbe161e297,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,site,si,ward,wa -Building1,active,update,0a04f1c2-de2a-4869-bab2-763d017e5316,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,building,bu,building,bu -City2,active,create,,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,jurisdiction,jdn,jurisdiction,jdn \ No newline at end of file +name,status,method,id,parentName,parentID,type,typeCode,physicalType,physicalTypeCode,longitude,latitude +City1,active,update,ba787982-b973-4bd5-854e-eacbe161e297,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,site,si,ward,wa,36.81,-1.28 +Building1,active,update,0a04f1c2-de2a-4869-bab2-763d017e5316,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,building,bu,building,bu,, +City2,active,create,,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,jurisdiction,jdn,jurisdiction,jdn,, \ No newline at end of file diff --git a/importer/json_payloads/locations_payload.json b/importer/json_payloads/locations_payload.json index 38712c6c..341648c7 100644 --- a/importer/json_payloads/locations_payload.json +++ b/importer/json_payloads/locations_payload.json @@ -38,6 +38,10 @@ "display": "$pt_display" } ] + }, + "position": { + "longitude": "$longitude", + "latitude": "$latitude" } } } diff --git a/importer/main.py b/importer/main.py index 69a26d8b..3561dffb 100644 --- a/importer/main.py +++ b/importer/main.py @@ -258,13 +258,15 @@ def organization_extras(resource, payload_string): # custom extras for locations def location_extras(resource, payload_string): try: - name, *_, parentName, parentID, type, typeCode, physicalType, physicalTypeCode = resource + name, *_, parentName, parentID, type, typeCode, physicalType, physicalTypeCode, longitude, latitude = resource except ValueError: parentName = "parentName" type = "type" typeCode = "typeCode" physicalType = "physicalType" physicalTypeCode = "physicalTypeCode" + longitude = "longitude" + try: if parentName and parentName != "parentName": @@ -308,6 +310,20 @@ def location_extras(resource, payload_string): del obj["resource"]["physicalType"] payload_string = json.dumps(obj, indent=4) + try: + if longitude and longitude != "longitude": + payload_string = payload_string.replace('"$longitude"', longitude).replace( + '"$latitude"', latitude) + else: + obj = json.loads(payload_string) + del obj["resource"]["position"] + payload_string = json.dumps(obj, indent=4) + except IndexError: + obj = json.loads(payload_string) + del obj["resource"]["position"] + payload_string = json.dumps(obj, indent=4) + + return payload_string diff --git a/importer/test_main.py b/importer/test_main.py index f5f60cd5..fe5fc207 100644 --- a/importer/test_main.py +++ b/importer/test_main.py @@ -185,6 +185,13 @@ def test_build_payload_locations(self, mock_get_resource): } }, }, + "position": { + "type": "object", + "properties": { + "longitude": {"const": 36.81}, + "latitude": {"const": -1.28} + } + } }, "required": [ "resourceType", @@ -195,6 +202,7 @@ def test_build_payload_locations(self, mock_get_resource): "partOf", "type", "physicalType", + "position", ], } validate(payload_obj["entry"][0]["resource"], resource_schema)