Skip to content

Commit

Permalink
fix(fhir): Fixed issues caused by fhirclient v4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
b32147 committed Jul 27, 2024
1 parent cc8f36a commit 12ced4e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
24 changes: 13 additions & 11 deletions ppmutils/fhir.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from fhirclient.models.domainresource import DomainResource
from fhirclient.models.fhirdate import FHIRDate
from fhirclient.models.period import Period
from fhirclient.models.fhirinstant import FHIRInstant
from fhirclient.models.fhirdatetime import FHIRDateTime
from fhirclient.models.patient import Patient
from fhirclient.models.flag import Flag
from fhirclient.models.bundle import Bundle, BundleEntry, BundleEntryRequest
Expand Down Expand Up @@ -4547,7 +4549,7 @@ def update_patient_enrollment(patient: Union[Patient, dict, str], status: str, s
flag.period.end = None

else:
now = FHIRDate(datetime.now(timezone.utc).isoformat())
now = FHIRDateTime(datetime.now(timezone.utc).isoformat())
period = Period()
period.start = now
flag.period = period
Expand All @@ -4560,7 +4562,7 @@ def update_patient_enrollment(patient: Union[Patient, dict, str], status: str, s

# Set an end date if a flag is present
if flag.period:
now = FHIRDate(datetime.now(timezone.utc).isoformat())
now = FHIRDateTime(datetime.now(timezone.utc).isoformat())
flag.period.end = now
else:
logger.debug(f"{prefix}: Flag has no period/start, cannot set end")
Expand All @@ -4573,7 +4575,7 @@ def update_patient_enrollment(patient: Union[Patient, dict, str], status: str, s

# Set an end date if a flag is present
if flag.period:
now = FHIRDate(datetime.now(timezone.utc).isoformat())
now = FHIRDateTime(datetime.now(timezone.utc).isoformat())
flag.period.end = now
else:
logger.debug(f"{prefix}: Flag has no period/start, cannot set end")
Expand Down Expand Up @@ -8923,7 +8925,7 @@ def questionnaire_response(
response.questionnaire = canonical_url.url
response.source = FHIR.Resources.reference_to(patient)
response.status = "completed"
response.authored = FHIRDate(date.isoformat())
response.authored = FHIRDateTime(date.isoformat())
response.author = FHIR.Resources.reference_to(author if author else patient)
response.subject = FHIR.Resources.reference_to(questionnaire)

Expand Down Expand Up @@ -9081,7 +9083,7 @@ def questionnaire_response_item_answer(
answer.valueInteger = value

elif type(value) is datetime:
answer.valueDateTime = FHIRDate(value.isoformat())
answer.valueDateTime = FHIRDateTime(value.isoformat())

elif type(value) is date:
answer.valueDate = FHIRDate(value.isoformat())
Expand Down Expand Up @@ -9193,7 +9195,7 @@ def consent(
consent = Consent()
consent.status = "proposed"
consent.id = uuid.uuid1().urn
consent.dateTime = FHIRDate(date.isoformat())
consent.dateTime = FHIRDateTime(date.isoformat())
consent.patient = FHIR.Resources.reference_to(patient)

# Policy
Expand Down Expand Up @@ -9257,7 +9259,7 @@ def contract(
# Build it
contract = Contract(strict=True)
contract.status = "executed"
contract.issued = FHIRDate(date.isoformat())
contract.issued = FHIRDateTime(date.isoformat())
contract.id = uuid.uuid1().urn
contract.subject = [FHIR.Resources.reference_to(patient)]

Expand All @@ -9275,7 +9277,7 @@ def contract(
"http://hl7.org/fhir/ValueSet/signature-type", "1.2.840.10065.1.12.1.7", "Consent Signature"
)
]
signature.when = FHIRDate(date.isoformat())
signature.when = FHIRInstant(date.isoformat())
signature.sigFormat = "text/plain"
signature.data = FHIR.Resources.blob(patient_signature)
signature.who = FHIR.Resources.reference_to(patient)
Expand Down Expand Up @@ -9331,7 +9333,7 @@ def related_contract(
# Build it
contract = Contract()
contract.status = "executed"
contract.issued = FHIRDate(date.isoformat())
contract.issued = FHIRDateTime(date.isoformat())
contract.id = uuid.uuid1().urn

# Signer
Expand All @@ -9348,7 +9350,7 @@ def related_contract(
"http://hl7.org/fhir/ValueSet/signature-type", "1.2.840.10065.1.12.1.7", "Consent Signature"
)
]
signature.when = FHIRDate(date.isoformat())
signature.when = FHIRInstant(date.isoformat())
signature.sigFormat = "text/plain"
signature.data = FHIR.Resources.blob(related_person_signature)
signature.who = FHIR.Resources.reference_to(related_person)
Expand Down Expand Up @@ -9398,7 +9400,7 @@ def composition(
composition.id = uuid.uuid1().urn
composition.status = "final"
composition.subject = FHIR.Resources.reference_to(patient)
composition.date = FHIRDate(date.isoformat())
composition.date = FHIRDateTime(date.isoformat())
composition.title = "Signature"
composition.author = [FHIRReference({"reference": "Device/hms-dbmi-ppm-consent"})]

Expand Down
30 changes: 16 additions & 14 deletions ppmutils/tests/test_fhir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
import re
import json
from datetime import datetime
from datetime import datetime, timezone

from ppmutils.ppm import PPM
from ppmutils.fhir import FHIR
Expand Down Expand Up @@ -310,7 +310,7 @@ def test_query_enrollment_participants(self):

# Check some properties
self.assertTrue(
PPM.Enrollment.enum(participants[random.randint(0, len(participants))]["enrollment"])
PPM.Enrollment.enum(participants[random.randint(0, len(participants) - 1)]["enrollment"])
is PPM.Enrollment.Accepted
)
self.assertTrue(participants[1]["email"] is not None)
Expand Down Expand Up @@ -362,10 +362,12 @@ def test_query_study_enrollment_participants(self):

# Check some properties
self.assertTrue(
PPM.Enrollment.enum(participants[random.randint(0, len(participants))]["enrollment"])
PPM.Enrollment.enum(participants[random.randint(0, len(participants) - 1)]["enrollment"])
is PPM.Enrollment.Consented
)
self.assertTrue(PPM.Study.enum(participants[random.randint(0, len(participants))]["study"]) is PPM.Study.RANT)
self.assertTrue(
PPM.Study.enum(participants[random.randint(0, len(participants) - 1)]["study"]) is PPM.Study.RANT
)
self.assertTrue(participants[1]["email"] is not None)

@responses.activate
Expand Down Expand Up @@ -864,7 +866,7 @@ def update_callback(request):
def test_update_patient_deceased_1(self):

# Set the deceased date
deceased = datetime.now()
deceased = datetime.now(timezone.utc)

# Start a data set
research_study, patient, flag, research_subject = FHIRData.participant(PPM.Study.NEER)
Expand Down Expand Up @@ -929,7 +931,7 @@ def update_callback(request):
def test_update_patient_deceased_3(self):

# Set the deceased date
deceased = datetime.now()
deceased = datetime.now(timezone.utc)

# Start a data set
research_study, patient, flag, research_subject = FHIRData.participant(PPM.Study.NEER)
Expand Down Expand Up @@ -1652,7 +1654,7 @@ def update_callback(request):
def test_update_research_subject_1(self):

# Set the dates
end = datetime.now()
end = datetime.now(timezone.utc)

# Start a data set
research_study, patient, flag, research_subject = FHIRData.participant(PPM.Study.NEER)
Expand Down Expand Up @@ -1724,8 +1726,8 @@ def test_update_research_subject_3(self):

# Set a date on research subject otherwise it won't be deleted
research_subject["period"] = {
"start": datetime.now().isoformat(),
"end": datetime.now().isoformat(),
"start": datetime.now(timezone.utc).isoformat(),
"end": datetime.now(timezone.utc).isoformat(),
}

def update_callback(request):
Expand Down Expand Up @@ -1755,7 +1757,7 @@ def update_callback(request):
def test_update_ppm_research_subject_1(self):

# Set the dates
end = datetime.now()
end = datetime.now(timezone.utc)

# Start a data set
research_study, patient, flag, research_subject = FHIRData.participant(PPM.Study.NEER)
Expand Down Expand Up @@ -1843,8 +1845,8 @@ def test_update_ppm_research_subject_3(self):

# Set a date on research subject otherwise it won't be deleted
research_subject["period"] = {
"start": datetime.now().isoformat(),
"end": datetime.now().isoformat(),
"start": datetime.now(timezone.utc).isoformat(),
"end": datetime.now(timezone.utc).isoformat(),
}

# Build the response handler
Expand Down Expand Up @@ -2082,7 +2084,7 @@ def create_bundle(resources, fhir_url):
return {
"resourceType": "Bundle",
"id": f"{uuid.uuid4()}",
"meta": {"lastUpdated": datetime.now().isoformat()},
"meta": {"lastUpdated": datetime.now(timezone.utc).isoformat()},
"type": "searchset",
"total": len(resources),
"link": [{"relation": "self", "url": fhir_url}],
Expand Down Expand Up @@ -2218,7 +2220,7 @@ def enrollment_flag(
patient,
study,
enrollment=PPM.Enrollment.Registered.value,
start=datetime.now(),
start=datetime.now(timezone.utc),
end=None,
):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
django
boto3
python-dateutil==2.*
fhirclient==4.*
fhirclient==4.2.*
requests==2.*
furl==2.*
requests-auth-aws-sigv4
Expand Down

0 comments on commit 12ced4e

Please sign in to comment.