Beispiel #1
0
    def _questionnaire_response_item_answer(value):

        # Create the item
        answer = QuestionnaireResponseItemAnswer()

        # Check type
        if type(value) is str:
            answer.valueString = str(value)

        elif type(value) is bool:
            answer.valueBoolean = value

        elif type(value) is int:
            answer.valueInteger = value

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

        elif type(value) is datetime.date:
            answer.valueDate = FHIRDate(value.isoformat())

        else:
            logger.warning('Unhandled answer type: {} - {}'.format(
                type(value), value))

            # Cast it as string
            answer.valueString = str(value)

        return answer
Beispiel #2
0
    def _related_contract(patient, date, patient_name, related_person,
                          related_person_name, related_person_signature,
                          questionnaire_response):

        # Build it
        contract = Contract()
        contract.status = 'executed'
        contract.issued = FHIRDate(date)
        contract.id = uuid.uuid4().urn

        # Signer
        contract_signer = ContractSigner()
        contract_signer.type = FHIR._coding(
            'http://hl7.org/fhir/ValueSet/contract-signer-type', 'CONSENTER',
            'Consenter')
        contract_signer.party = FHIR._reference_to(related_person)

        # Signature
        signature = Signature()
        signature.type = [
            FHIR._coding('http://hl7.org/fhir/ValueSet/signature-type',
                         '1.2.840.10065.1.12.1.7', 'Consent Signature')
        ]
        signature.when = FHIRDate(date)
        signature.contentType = 'text/plain'
        signature.blob = FHIR._blob(related_person_signature)
        signature.whoReference = FHIR._reference_to(related_person)
        signature.whoReference.display = related_person_name

        # Refer to the patient
        patient_reference = FHIRReference({
            'reference':
            '{}/{}'.format(patient.resource_type, patient.id),
            'display':
            patient_name
        })
        signature.onBehalfOfReference = patient_reference

        # Add references
        contract_signer.signature = [signature]
        contract.signer = [contract_signer]
        contract.bindingReference = FHIR._reference_to(questionnaire_response)

        return contract
Beispiel #3
0
    def _qr_item_answer(self) -> QuestionnaireResponseItemAnswer:
        """
        Returns a QuestionnaireResponseItemAnswer.
        """
        # Look things up
        raw_answer = self.answer
        answer_type = self.answer_type

        # Convert the value
        if raw_answer is None:
            # Deal with null values first, otherwise we will get
            # mis-conversion, e.g. str(None) == "None", bool(None) == False.
            fhir_answer = None
        elif answer_type == FHIRAnswerType.BOOLEAN:
            fhir_answer = bool(raw_answer)
        elif answer_type == FHIRAnswerType.DATE:
            fhir_answer = FHIRDate(
                format_datetime(raw_answer, DateFormat.FHIR_DATE)).as_json()
        elif answer_type == FHIRAnswerType.DATETIME:
            fhir_answer = FHIRDate(raw_answer.isoformat()).as_json()
        elif answer_type == FHIRAnswerType.DECIMAL:
            fhir_answer = float(raw_answer)
        elif answer_type == FHIRAnswerType.INTEGER:
            fhir_answer = int(raw_answer)
        elif answer_type == FHIRAnswerType.QUANTITY:
            fhir_answer = Quantity(
                jsondict={
                    Fc.VALUE: float(raw_answer)
                    # More sophistication is possible -- units, for example.
                }).as_json()
        elif answer_type == FHIRAnswerType.STRING:
            fhir_answer = str(raw_answer)
        elif answer_type == FHIRAnswerType.TIME:
            fhir_answer = FHIRDate(
                format_datetime(raw_answer, DateFormat.FHIR_TIME)).as_json()
        elif answer_type == FHIRAnswerType.URI:
            fhir_answer = str(raw_answer)
        else:
            raise NotImplementedError(
                f"Don't know how to handle FHIR answer type {answer_type}")

        # Build the FHIR object
        return QuestionnaireResponseItemAnswer(
            jsondict={answer_type.value: fhir_answer})
Beispiel #4
0
 def get_fhir_resource(self, resource: str, *args, **kwargs):
     if resource != CapabilityStatement.resource_type:
         ... # throw an error
     cs: CapabilityStatement = CapabilityStatement()
     cs.fhirVersion = '4.0.0'
     cs.status = 'active'
     cs.acceptUnknown = 'false'
     cs.format = ['json']
     cs.kind = 'json'
     date = FHIRDate()
     date.date = datetime.today()
     cs.date = date
     rest: CapabilityStatementRest = CapabilityStatementRest()
     cs.rest = [rest]
     rest.mode = "server"
     rest.resource = []
     for rule in current_app.url_map.iter_rules():
         print(rule)
     return cs
Beispiel #5
0
    def _contract(patient,
                  date,
                  patient_name,
                  patient_signature,
                  questionnaire_response=None):

        # Build it
        contract = Contract()
        contract.status = 'executed'
        contract.issued = FHIRDate(date)
        contract.id = uuid.uuid4().urn

        # Signer
        signer = ContractSigner()
        signer.type = FHIR._coding(
            'http://hl7.org/fhir/ValueSet/contract-signer-type', 'CONSENTER',
            'Consenter')
        signer.party = FHIR._reference_to(patient)

        # Signature
        signature = Signature()
        signature.type = [
            FHIR._coding('http://hl7.org/fhir/ValueSet/signature-type',
                         '1.2.840.10065.1.12.1.7', 'Consent Signature')
        ]
        signature.when = FHIRDate(date)
        signature.contentType = 'text/plain'
        signature.blob = FHIR._blob(patient_signature)
        signature.whoReference = FHIR._reference_to(patient)
        signature.whoReference.display = patient_name

        # Add references
        signer.signature = [signature]
        contract.signer = [signer]

        # Add questionnaire if passed
        if questionnaire_response:
            contract.bindingReference = FHIR._reference_to(
                questionnaire_response)

        return contract
Beispiel #6
0
    def _consent(patient, date, exceptions=[], related_person=None):

        # Make it
        consent = Consent()
        consent.status = 'proposed'
        consent.id = uuid.uuid4().urn
        consent.dateTime = FHIRDate(date)
        consent.patient = FHIR._reference_to(patient)

        # Policy
        policy = ConsentPolicy()
        policy.authority = 'HMS-DBMI'
        policy.uri = 'https://hms.harvard.edu'
        consent.policy = [policy]

        # Check for a related person consenting
        if related_person:

            # Add them
            consent.consentingParty = [FHIR._reference_to(related_person)]

        # Period
        period = Period()
        period.start = FHIRDate(date)

        # Add items
        consent.period = period
        consent.purpose = [
            FHIR._coding('http://hl7.org/fhir/v3/ActReason', 'HRESCH',
                         'healthcare research')
        ]

        # Add exceptions
        consent.except_fhir = exceptions

        return consent
Beispiel #7
0
    def _composition(patient, date, text, resources=[]):

        # Build it
        composition = Composition()
        composition.id = uuid.uuid4().urn
        composition.status = 'final'
        composition.subject = FHIR._reference_to(patient)
        composition.date = FHIRDate(date)
        composition.title = 'Signature'
        composition.author = [
            FHIRReference({'reference': 'Device/hms-dbmi-ppm-consent'})
        ]

        # Composition type
        coding = Coding()
        coding.system = 'http://loinc.org'
        coding.code = '83930-8'
        coding.display = 'Research Consent'

        # Convoluted code property
        code = CodeableConcept()
        code.coding = [coding]

        # Combine
        composition.type = code

        # Add sections
        composition.section = []

        # Add text
        narrative = Narrative()
        narrative.div = text
        narrative.status = 'additional'
        text_section = CompositionSection()
        text_section.text = narrative
        composition.section.append(text_section)

        # Add related section resources
        for resource in resources:

            # Add consent
            consent_section = CompositionSection()
            consent_section.entry = [FHIR._reference_to(resource)]
            composition.section.append(consent_section)

        return composition
Beispiel #8
0
    def _questionnaire_response(questionnaire,
                                patient,
                                date=datetime.datetime.utcnow().isoformat(),
                                answers={},
                                author=None):

        # Build the response
        response = QuestionnaireResponse()
        response.id = uuid.uuid4().urn
        response.questionnaire = FHIR._reference_to(questionnaire)
        response.source = FHIR._reference_to(patient)
        response.status = 'completed'
        response.authored = FHIRDate(date)
        response.author = FHIR._reference_to(author if author else patient)

        # Collect response items flattened
        response.item = FHIR._questionnaire_response_items(
            questionnaire, answers)

        # Set it on the questionnaire
        return response
Beispiel #9
0
def _ToFhirDate(dt):
    if not dt:
        return None
    return FHIRDate.with_json(dt.isoformat())