Skip to content

Commit

Permalink
Merge branch 'main' into add-structure-map-tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
brandyodhiambo authored Feb 7, 2024
2 parents 1b9d8a4 + 7813b2b commit 33836f9
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 56 deletions.
8 changes: 4 additions & 4 deletions importer/csv/locations/locations_full.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,status,method,version,id,parentName,parentID,type,physicalType
City1,active,update,1,ba787982-b973-4bd5-854e-eacbe161e297,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,jurisdiction,jurisdiction
Building1,active,update,1,0a04f1c2-de2a-4869-bab2-763d017e5316,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,building,building
City2,active,create,1,,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,jurisdiction,jurisdiction
name,status,method,id,parentName,parentID,type,physicalType
City1,active,update,ba787982-b973-4bd5-854e-eacbe161e297,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,jurisdiction,jurisdiction
Building1,active,update,0a04f1c2-de2a-4869-bab2-763d017e5316,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,building,building
City2,active,create,,test location-1,18fcbc2e-4240-4a84-a270-7a444523d7b6,jurisdiction,jurisdiction
92 changes: 51 additions & 41 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,33 @@ def create_user_resources(user_id, user):

# custom extras for organizations
def organization_extras(resource, payload_string):
_, active, *_, alias = resource
try:
if resource[5]:
payload_string = payload_string.replace("$alias", resource[5])
if alias:
payload_string = payload_string.replace("$alias", alias)
else:
obj = json.loads(payload_string)
del obj["resource"]["alias"]
payload_string = json.dumps(obj, indent=4)
except IndexError:
obj = json.loads(payload_string)
del obj["resource"]["alias"]
payload_string = json.dumps(obj, indent=4)

try:
payload_string = payload_string.replace("$active", resource[1])
payload_string = payload_string.replace("$active", active)
except IndexError:
payload_string = payload_string.replace("$active", "true")
return payload_string


# custom extras for locations
def location_extras(resource, payload_string):
name, *_, parentName, parentID, type, physicalType = resource
try:
if resource[4]:
payload_string = payload_string.replace("$parentName", resource[4]).replace(
"$parentID", resource[5]
if parentName:
payload_string = payload_string.replace("$parentName", parentName).replace(
"$parentID", parentID
)
else:
obj = json.loads(payload_string)
Expand All @@ -225,16 +231,16 @@ def location_extras(resource, payload_string):
payload_string = json.dumps(obj, indent=4)

try:
if resource[7] == "building":
if type == "building":
payload_string = payload_string.replace("$t_code", "bu").replace(
"$t_display", "Building"
)
elif resource[7] == "jurisdiction":
elif type == "jurisdiction":
payload_string = payload_string.replace("$t_code", "jdn").replace(
"$t_display", "Jurisdiction"
)
else:
logging.error("Unsupported location type provided for " + resource[0])
logging.error("Unsupported location type provided for " + name)
obj = json.loads(payload_string)
del obj["resource"]["type"]
payload_string = json.dumps(obj, indent=4)
Expand All @@ -244,16 +250,18 @@ def location_extras(resource, payload_string):
payload_string = json.dumps(obj, indent=4)

try:
if resource[8] == "building":
if physicalType == "building":
payload_string = payload_string.replace("$pt_code", "bu").replace(
"$pt_display", "Building"
)
elif resource[8] == "jurisdiction":
elif physicalType == "jurisdiction":
payload_string = payload_string.replace("$pt_code", "jdn").replace(
"$pt_display", "Jurisdiction"
)
else:
logging.error("Unsupported location physical type provided for " + resource[0])
logging.error(
"Unsupported location physical type provided for " + name
)
obj = json.loads(payload_string)
del obj["resource"]["type"]
payload_string = json.dumps(obj, indent=4)
Expand All @@ -274,15 +282,16 @@ def care_team_extras(
elements = []
elements2 = []

*_, organizations, participants = resource
if load_type == "min":
try:
if resource[5]:
elements = resource[5].split("|")
if organizations:
elements = organizations.split("|")
except IndexError:
pass
try:
if resource[6]:
elements2 = resource[6].split("|")
if participants:
elements2 = participants.split("|")
except IndexError:
pass
elif load_type == "full":
Expand Down Expand Up @@ -351,11 +360,12 @@ def care_team_extras(
def extract_matches(resource_list):
teamMap = {}
for resource in resource_list:
if resource[1].strip() and resource[3].strip():
if resource[1] not in teamMap.keys():
teamMap[resource[1]] = [resource[3] + ":" + resource[2]]
group_name, group_id, item_name, item_id = resource
if group_id.strip() and item_id.strip():
if group_id not in teamMap.keys():
teamMap[group_id] = [item_id + ":" + item_name]
else:
teamMap[resource[1]].append(resource[3] + ":" + resource[2])
teamMap[group_id].append(item_id + ":" + item_name)
else:
logging.error("Missing required id: Skipping " + str(resource))
return teamMap
Expand Down Expand Up @@ -459,16 +469,16 @@ def build_org_affiliation(resources, resource_list):
# This function is used to Capitalize the 'resource_type'
# and remove the 's' at the end, a version suitable with the API
def get_valid_resource_type(resource_type):
logging.info("Modify the string resource_type")
logging.debug("Modify the string resource_type")
modified_resource_type = resource_type[0].upper() + resource_type[1:-1]
return modified_resource_type


# This function gets the current resource version from the API
def get_resource_version(resource_id, resource_type):
logging.info("Getting resource version")
logging.debug("Getting resource version")
modified_resource_type = get_valid_resource_type(resource_type)
resource_url = config.fhir_base_url + "/" + modified_resource_type + "/" + resource_id
resource_url = "/".join([config.fhir_base_url, modified_resource_type, resource_id])
response = handle_request("GET", "", resource_url)
json_response = json.loads(response[0])
return json_response["meta"]["versionId"]
Expand All @@ -484,45 +494,46 @@ def build_payload(resource_type, resources, resource_payload_file):
payload_string = json_file.read()

for resource in resources:
name, status, method, id, *_ = resource
try:
if resource[2] == "create":
if len(resource[4].strip()) > 0:
if method == "create":
if len(id.strip()) > 0:
# use the provided id
unique_uuid = resource[4].strip()
identifier_uuid = resource[4] if resource[5] == "" else resource[5]
unique_uuid = id.strip()
identifier_uuid = id.strip()
else:
# generate a new uuid
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, resource[0]))
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, name))
identifier_uuid = unique_uuid
elif resource[2] == "update":
if len(resource[4].strip()) > 0:
elif method == "update":
if len(id.strip()) > 0:
# use the provided id
unique_uuid = resource[4].strip()
identifier_uuid = resource[4] if resource[5] == "" else resource[5]
unique_uuid = id.strip()
identifier_uuid = id.strip()
else:
# generate a new uuid
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, resource[0]))
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, name))
identifier_uuid = unique_uuid
except IndexError:
# default if method is not provided
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, resource[0]))
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, name))
identifier_uuid = unique_uuid

# ps = payload_string
ps = (
payload_string.replace("$name", resource[0])
payload_string.replace("$name", name)
.replace("$unique_uuid", unique_uuid)
.replace("$identifier_uuid", identifier_uuid)
)

try:
ps = ps.replace("$status", resource[1])
ps = ps.replace("$status", status)
except IndexError:
ps = ps.replace("$status", "active")

# Get resource versions from API
try:
version = get_resource_version(resource[3], resource_type)
version = get_resource_version(id, resource_type)
ps = ps.replace("$version", version)
except Exception:
ps = ps.replace("$version", "1")
Expand Down Expand Up @@ -764,11 +775,10 @@ def delete_resource(resource_type, resource_id, cascade):
else:
cascade = ""

r = handle_request(
"DELETE",
"",
config.fhir_base_url + "/" + resource_type + "/" + resource_id + cascade,
resource_url = "/".join(
[config.fhir_base_url, resource_type, resource_id + cascade]
)
r = handle_request("DELETE", "", resource_url)
logging.info(r.text)


Expand Down
1 change: 1 addition & 0 deletions importer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ requests-oauthlib==1.3.1
urllib3==2.0.3
backoff==2.2.1
pytest==7.4.2
jsonschema==4.21.1
Loading

0 comments on commit 33836f9

Please sign in to comment.