From dccb6723e3b4d8315ae7f44b51c6f95ea3ae5a13 Mon Sep 17 00:00:00 2001 From: TheSaturnian <156409714+GnautSpace@users.noreply.github.com> Date: Tue, 7 Oct 2025 17:21:51 +0000 Subject: [PATCH 1/2] feat: Add create_document_reference_content helper --- healthchain/fhir/__init__.py | 2 + healthchain/fhir/helpers.py | 75 +++++++++++++++++++++++++++++++++++- tests/fhir/test_helpers.py | 51 ++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/healthchain/fhir/__init__.py b/healthchain/fhir/__init__.py index 82e68a2e..312c1edc 100644 --- a/healthchain/fhir/__init__.py +++ b/healthchain/fhir/__init__.py @@ -9,6 +9,7 @@ set_problem_list_item_category, read_content_attachment, create_document_reference, + create_document_reference_content, create_single_attachment, create_resource_from_dict, ) @@ -30,6 +31,7 @@ "set_problem_list_item_category", "read_content_attachment", "create_document_reference", + "create_document_reference_content", "create_single_attachment", "create_resource_from_dict", # Bundle operations diff --git a/healthchain/fhir/helpers.py b/healthchain/fhir/helpers.py index 444a6ea5..d8d9261a 100644 --- a/healthchain/fhir/helpers.py +++ b/healthchain/fhir/helpers.py @@ -280,7 +280,6 @@ def create_allergy_intolerance( return allergy -# TODO: create a function that creates a DocumentReferenceContent to add to the DocumentReference def create_document_reference( data: Optional[Any] = None, url: Optional[str] = None, @@ -327,6 +326,80 @@ def create_document_reference( return document_reference +def create_document_reference_content( + attachment_data: Optional[str] = None, + url: Optional[str] = None, + content_type: str = "text/plain", + language: Optional[str] = "en-US", + title: Optional[str] = None, + **kwargs +) -> Dict[str, Any]: + """Create a FHIR DocumentReferenceContent object. + + Creates a DocumentReferenceContent structure that can be added to a DocumentReference. + Either attachment_data or url must be provided. If attachment_data is provided, it will + be base64 encoded automatically. + + Args: + attachment_data: The content data (text that will be base64 encoded) + url: URL where the content can be accessed + content_type: MIME type (e.g., 'text/plain', 'text/html', 'application/pdf') (default: text/plain) + language: Language code (default: en-US) + title: Optional title for the content (default: "Attachment created by HealthChain") + **kwargs: Additional DocumentReferenceContent fields (e.g., format, profile) + + Returns: + Dict[str, Any]: A FHIR DocumentReferenceContent dictionary with attachment and optional language + + Example: + >>> # Create content with inline data + >>> content = create_document_reference_content( + ... attachment_data="Patient presents with fever...", + ... content_type="text/plain", + ... title="Clinical Note" + ... ) + >>> + >>> # Create content with URL reference + >>> content = create_document_reference_content( + ... url="https://example.com/document.pdf", + ... content_type="application/pdf", + ... title="Lab Report" + ... ) + >>> + >>> # Add content to a DocumentReference + >>> doc_ref = DocumentReference( + ... id="doc-1", + ... status="current", + ... content=[content] + ... ) + """ + if not attachment_data and not url: + logger.warning( + "No attachment_data or url provided for DocumentReferenceContent" + ) + + if title is None: + title = "Attachment created by HealthChain" + + attachment = create_single_attachment( + content_type=content_type, + data=attachment_data, + url=url, + title=title, + ) + + content: Dict[str, Any] = { + "attachment": attachment, + } + + if language: + content["language"] = language + + content.update(kwargs) + + return content + + def read_content_attachment( document_reference: DocumentReference, include_data: bool = True, diff --git a/tests/fhir/test_helpers.py b/tests/fhir/test_helpers.py index da0fb684..25872981 100644 --- a/tests/fhir/test_helpers.py +++ b/tests/fhir/test_helpers.py @@ -5,6 +5,7 @@ from fhir.resources.codeablereference import CodeableReference from fhir.resources.documentreference import DocumentReference from fhir.resources.attachment import Attachment +from fhir.resources.coding import Coding from datetime import datetime @@ -17,6 +18,7 @@ set_problem_list_item_category, create_single_attachment, create_document_reference, + create_document_reference_content, read_content_attachment, ) @@ -245,3 +247,52 @@ def test_read_attachment_with_url(): assert attachments[0]["metadata"]["content_type"] == "application/pdf" assert attachments[0]["metadata"]["title"] == "Test URL Doc" assert attachments[0]["metadata"]["creation"] is not None + + +def test_create_document_reference_content_with_data(): + """Test creating DocumentReferenceContent with inline data.""" + content = create_document_reference_content( + attachment_data="Test data", + content_type="text/plain", + title="Test Document" + ) + + assert "attachment" in content + assert content["attachment"].contentType == "text/plain" + assert content["attachment"].title == "Test Document" + assert content["attachment"].data is not None + assert content["language"] == "en-US" + + +def test_create_document_reference_content_with_url(): + """Test creating DocumentReferenceContent with URL.""" + content = create_document_reference_content( + url="https://example.com/doc.pdf", + content_type="application/pdf", + title="External Document" + ) + + assert "attachment" in content + assert content["attachment"].url == "https://example.com/doc.pdf" + assert content["attachment"].contentType == "application/pdf" + + +def test_create_document_reference_content_custom_language(): + """Test creating DocumentReferenceContent with custom language.""" + content = create_document_reference_content( + attachment_data="Données de test", + language="fr-FR" + ) + + assert content["language"] == "fr-FR" + + +def test_create_document_reference_content_with_kwargs(): + """Test creating DocumentReferenceContent with additional fields.""" + content = create_document_reference_content( + attachment_data="Test", + format=Coding(system="http://ihe.net/fhir/ValueSet/IHE.FormatCode.codesystem", + code="urn:ihe:pcc:handp:2008") + ) + + assert "format" in content \ No newline at end of file From 08c7e0921b5171d3c497273cc5d41e080b65df76 Mon Sep 17 00:00:00 2001 From: Adam Kells Date: Thu, 27 Nov 2025 19:33:15 +0000 Subject: [PATCH 2/2] pre-commit hooks --- healthchain/fhir/helpers.py | 20 +++++++++++--------- tests/fhir/test_helpers.py | 25 +++++++++++++------------ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/healthchain/fhir/helpers.py b/healthchain/fhir/helpers.py index 0c8bc0a9..95114272 100644 --- a/healthchain/fhir/helpers.py +++ b/healthchain/fhir/helpers.py @@ -373,14 +373,14 @@ def create_document_reference_content( content_type: str = "text/plain", language: Optional[str] = "en-US", title: Optional[str] = None, - **kwargs + **kwargs, ) -> Dict[str, Any]: """Create a FHIR DocumentReferenceContent object. - + Creates a DocumentReferenceContent structure that can be added to a DocumentReference. Either attachment_data or url must be provided. If attachment_data is provided, it will be base64 encoded automatically. - + Args: attachment_data: The content data (text that will be base64 encoded) url: URL where the content can be accessed @@ -388,10 +388,10 @@ def create_document_reference_content( language: Language code (default: en-US) title: Optional title for the content (default: "Attachment created by HealthChain") **kwargs: Additional DocumentReferenceContent fields (e.g., format, profile) - + Returns: Dict[str, Any]: A FHIR DocumentReferenceContent dictionary with attachment and optional language - + Example: >>> # Create content with inline data >>> content = create_document_reference_content( @@ -399,7 +399,7 @@ def create_document_reference_content( ... content_type="text/plain", ... title="Clinical Note" ... ) - >>> + >>> >>> # Create content with URL reference >>> content = create_document_reference_content( ... url="https://example.com/document.pdf", @@ -418,7 +418,7 @@ def create_document_reference_content( logger.warning( "No attachment_data or url provided for DocumentReferenceContent" ) - + if title is None: title = "Attachment created by HealthChain" @@ -432,13 +432,15 @@ def create_document_reference_content( content: Dict[str, Any] = { "attachment": attachment, } - + if language: content["language"] = language content.update(kwargs) - + return content + + def set_condition_category(condition: Condition, category: str) -> Condition: """ Set the category of a FHIR Condition to either 'problem-list-item' or 'encounter-diagnosis'. diff --git a/tests/fhir/test_helpers.py b/tests/fhir/test_helpers.py index 5c709197..75763b16 100644 --- a/tests/fhir/test_helpers.py +++ b/tests/fhir/test_helpers.py @@ -256,11 +256,9 @@ def test_read_attachment_with_url(): def test_create_document_reference_content_with_data(): """Test creating DocumentReferenceContent with inline data.""" content = create_document_reference_content( - attachment_data="Test data", - content_type="text/plain", - title="Test Document" + attachment_data="Test data", content_type="text/plain", title="Test Document" ) - + assert "attachment" in content assert content["attachment"].contentType == "text/plain" assert content["attachment"].title == "Test Document" @@ -273,9 +271,9 @@ def test_create_document_reference_content_with_url(): content = create_document_reference_content( url="https://example.com/doc.pdf", content_type="application/pdf", - title="External Document" + title="External Document", ) - + assert "attachment" in content assert content["attachment"].url == "https://example.com/doc.pdf" assert content["attachment"].contentType == "application/pdf" @@ -284,10 +282,9 @@ def test_create_document_reference_content_with_url(): def test_create_document_reference_content_custom_language(): """Test creating DocumentReferenceContent with custom language.""" content = create_document_reference_content( - attachment_data="Données de test", - language="fr-FR" + attachment_data="Données de test", language="fr-FR" ) - + assert content["language"] == "fr-FR" @@ -295,11 +292,15 @@ def test_create_document_reference_content_with_kwargs(): """Test creating DocumentReferenceContent with additional fields.""" content = create_document_reference_content( attachment_data="Test", - format=Coding(system="http://ihe.net/fhir/ValueSet/IHE.FormatCode.codesystem", - code="urn:ihe:pcc:handp:2008") + format=Coding( + system="http://ihe.net/fhir/ValueSet/IHE.FormatCode.codesystem", + code="urn:ihe:pcc:handp:2008", + ), ) - + assert "format" in content + + def test_create_resource_from_dict_success_and_failure(): cond_dict = { "id": "x",