Fix and Clean
This commit is contained in:
@@ -873,6 +873,28 @@ def _execute_custom_function(function_name, args, output_request):
|
||||
return f"$$$$ Unknown Custom Function: {function_name}"
|
||||
|
||||
|
||||
def _apply_date_format(strftime_template, iso_value):
|
||||
"""
|
||||
Parses an ISO 8601 date string and formats it using a strftime template.
|
||||
|
||||
Returns the formatted string, or None if the source value cannot be parsed.
|
||||
"""
|
||||
iso_formats = [
|
||||
"%Y-%m-%dT%H:%M:%S.%fZ",
|
||||
"%Y-%m-%dT%H:%M:%SZ",
|
||||
"%Y-%m-%dT%H:%M:%S.%f",
|
||||
"%Y-%m-%dT%H:%M:%S",
|
||||
"%Y-%m-%dT%H:%M",
|
||||
"%Y-%m-%d",
|
||||
]
|
||||
for fmt in iso_formats:
|
||||
try:
|
||||
return datetime.strptime(iso_value, fmt).strftime(strftime_template)
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def process_requests_mapping(output_request, request_data):
|
||||
"""Processes and adds the requests mapping fields to the output request dictionary."""
|
||||
for field in requests_mapping_config:
|
||||
@@ -946,7 +968,11 @@ def process_requests_mapping(output_request, request_data):
|
||||
# Post-processing: Apply field template
|
||||
field_template = field.get("field_template")
|
||||
if field_template and final_value not in ["undefined", "N/A"] and isinstance(final_value, (str, int, float, bool)):
|
||||
final_value = field_template.replace("$value", str(final_value))
|
||||
if "%" in field_template:
|
||||
formatted = _apply_date_format(field_template, str(final_value))
|
||||
final_value = formatted if formatted is not None else f"$$$$ Date Format Error: {final_value}"
|
||||
else:
|
||||
final_value = field_template.replace("$value", str(final_value))
|
||||
|
||||
if field_group not in output_request:
|
||||
output_request[field_group] = {}
|
||||
@@ -1139,8 +1165,8 @@ def _process_single_request(worklist_request, mapping_dict):
|
||||
request_detail = {}
|
||||
|
||||
# --- 2. Fetch professional names (prescriber + requester, deduplicated) ---
|
||||
prescriber_id = worklist_request.get("prescriber")
|
||||
requester_id = worklist_request.get("requester")
|
||||
prescriber_id = request_detail.get("prescriber")
|
||||
requester_id = request_detail.get("requester")
|
||||
|
||||
# Deduplicate IDs before API call
|
||||
unique_ids = list({pid for pid in [prescriber_id, requester_id] if pid})
|
||||
@@ -1150,7 +1176,7 @@ def _process_single_request(worklist_request, mapping_dict):
|
||||
request_detail["prescriberName"] = professionals.get(prescriber_id) if prescriber_id else None
|
||||
request_detail["requesterName"] = professionals.get(requester_id) if requester_id else None
|
||||
|
||||
# --- 3. Inject patient identity fields from worklist ---
|
||||
# --- 3. Inject patient identity fields from worklist (only source for these fields) ---
|
||||
identity = worklist_request.get("identity") or {}
|
||||
request_detail["lastname"] = identity.get("lastname")
|
||||
request_detail["firstname"] = identity.get("firstname")
|
||||
@@ -1162,7 +1188,7 @@ def _process_single_request(worklist_request, mapping_dict):
|
||||
request_detail["status"] = diagnostic_status
|
||||
|
||||
# --- 5. Center mapping: inject Center_Name from labeledOrganization ---
|
||||
labeled_org = worklist_request.get("labeledOrganization")
|
||||
labeled_org = request_detail.get("labeledOrganization")
|
||||
if labeled_org:
|
||||
org_normalized = labeled_org.strip().lower()
|
||||
request_detail["Center_Name"] = mapping_dict.get(org_normalized, labeled_org)
|
||||
@@ -1175,7 +1201,7 @@ def _process_single_request(worklist_request, mapping_dict):
|
||||
|
||||
# --- 7. Build meta for organization building and sorting ---
|
||||
request_meta = {
|
||||
"org_id": worklist_request.get("organization"),
|
||||
"org_id": request_detail.get("organization"),
|
||||
"org_name": labeled_org,
|
||||
"center_name": request_detail.get("Center_Name"),
|
||||
"status": request_detail.get("status"),
|
||||
@@ -1447,6 +1473,7 @@ def main():
|
||||
print("Sorting results...")
|
||||
|
||||
all_results.sort(key=lambda x: (
|
||||
x[1].get("center_name") or "",
|
||||
x[1].get("lastname") or "",
|
||||
x[1].get("firstname") or "",
|
||||
x[1].get("id") or ""
|
||||
|
||||
Reference in New Issue
Block a user