Skip to content

Commit

Permalink
fix(fhir): Fixes FHIR date formatting; includes flattened patient in …
Browse files Browse the repository at this point in the history
…query results

Merge pull request #5 from hms-dbmi/development
  • Loading branch information
b32147 committed Feb 8, 2019
2 parents ed90306 + 1019442 commit e0bcfa2
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions ppmutils/fhir.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ def _format_date(date_string, date_format):
to_zone = tz.gettz('America/New_York')
utc = date.replace(tzinfo=from_zone)

# If UTC time was 00:00:00, assume a time was missing and return the date as is so
# the ET conversion does not change the date.
if utc.hour == 0 and utc.minute == 0 and utc.second == 0:
return utc.strftime(date_format)

# Convert time zone to assumed ET
et = utc.astimezone(to_zone)

Expand Down Expand Up @@ -1091,25 +1096,10 @@ def query_patients(study=None, enrollment=None, active=True, testing=False):
'date_registered': date_registered,
}

# Check names
try:
patient_dict['firstname'] = patient.name[0].given[0]
except Exception:
patient_dict['firstname'] = "---"
logger.warning('No firstname for Patient/{}'.format(patient.id))
try:
patient_dict['lastname'] = patient.name[0].family
except Exception:
patient_dict['lastname'] = "---"
logger.warning('No lastname for Patient/{}'.format(patient.id))

# Add twitter handle
try:
for telecom in patient.telecom:
if telecom.system == "other" and telecom.value.startswith("https://twitter.com"):
patient_dict['twitter_handle'] = telecom.value
except Exception:
pass
# Wrap the patient resource in a fake bundle and flatten them
flattened_patient = FHIR.flatten_patient({'entry': [{'resource': patient.as_json()}]})
if flattened_patient:
patient_dict.update(flattened_patient)

# Add it
patients.append(patient_dict)
Expand Down Expand Up @@ -2487,7 +2477,7 @@ def flatten_participant(bundle):
if enrollment['enrollment'] == PPM.Enrollment.Accepted.value and enrollment.get('start'):

# Convert time zone to assumed ET
participant['enrollment_accepted_date'] = FHIR._format_date(enrollment['start'], '%-m/%-d/%Y')
participant['enrollment_accepted_date'] = FHIR._format_date(enrollment['start'], '%m/%d/%Y')

else:
participant['enrollment_accepted_date'] = ''
Expand Down Expand Up @@ -2624,6 +2614,10 @@ def flatten_questionnaire_response(bundle_dict, questionnaire_id):
# Add the answer
response[text] = answer

# Add the date that the questionnaire was completed
authored_date = questionnaire_response.authored.origval
formatted_authored_date = FHIR._format_date(authored_date, '%m/%d/%Y')

return response

@staticmethod
Expand Down Expand Up @@ -2891,7 +2885,7 @@ def flatten_consent_composition(bundle_json):
date_time = signed_consent.dateTime.origval

# Format it
consent_object["date_signed"] = FHIR._format_date(date_time, '%Y-%m-%d')
consent_object["date_signed"] = FHIR._format_date(date_time, '%m/%d/%Y')

# Exceptions are for when they refuse part of the consent.
if signed_consent.except_fhir:
Expand Down Expand Up @@ -3118,7 +3112,7 @@ def flatten_document_reference(resource):
# Get dates
reference['timestamp'] = FHIR._get_or(resource, ['indexed'])
if reference.get('timestamp'):
reference['date'] = FHIR._format_date(reference['timestamp'], '%m-%d-%Y')
reference['date'] = FHIR._format_date(reference['timestamp'], '%m/%d/%Y')

# Get data provider
reference['code'] = FHIR._get_or(resource, ['type', 'coding', 0, 'code'])
Expand Down

0 comments on commit e0bcfa2

Please sign in to comment.