Giter Club home page Giter Club logo

Comments (3)

bwalsh avatar bwalsh commented on August 20, 2024

@yvesonline Hello. I was wondering if you resolved this and what approach you took?

from fhir.resources.

yvesonline avatar yvesonline commented on August 20, 2024

Yes @bwalsh we resolved this by directly using the Patient object, there wasn't a lot of added value in creating a dedicated MIO DiGA Patient class.

So we do something like this:

from datetime import datetime, timezone
from fhir.resources.patient import Patient

data = {
    'id': '...',
    'name': [
        {
            'text': '...',
            'use': 'official',
            'family': '...',
            'family__ext': {
                'extension': [
                    {
                        'url': 'http://hl7.org/fhir/StructureDefinition/humanname-own-name',
                        'valueString': '...',
                    },
                ],
            },
            'given': ['...'],
        },
    ],
    'identifier': [...],
    'meta': {
        'versionId': 1,
        'lastUpdated': datetime.now(tz=timezone.utc),
        'profile': [
            'https://fhir.kbv.de/StructureDefinition/KBV_PR_MIO_DIGA_Patient|1.0.0'
        ],
    },
    'telecom': [...],
    'communication': [...],
}

p = Patient(**data)

from fhir.resources.

bobvanderlinden avatar bobvanderlinden commented on August 20, 2024

I needed to define resources as part of Aidbox that weren't defined fhir.resources. It took some time to figure out, but eventually I came up with the following function:

def define_fhir_resource(
    fhirtypes,
    fhirtypesvalidators,
    cls: typing.Type[DomainResource],
) -> typing.Type[DomainResource]:
    resource_name = cls.__name__
    resource_type_name = f"{resource_name}Type"
    resource_validator_name = f"{resource_name.lower()}_validator"

    resource_type = type(
        resource_type_name,
        (fhirtypes.AbstractType,),
        {"__resource_type__": resource_name},
    )

    setattr(fhirtypes, resource_type_name, resource_type)
    fhirtypesvalidators.MODEL_CLASSES[resource_name] = (cls, None)

    def resource_validator(value):
        return fhirtypesvalidators.fhir_model_validator(resource_name, value)

    setattr(fhirtypesvalidators, resource_validator_name, resource_validator)

To use this to create a User resource:

from fhir.resources.R4B import fhirtypes
from fhir.resources.R4B import fhirtypesvalidators
from fhir.resources.R4B.domainresource import DomainResource
from pydantic import Field

class User(DomainResource):
    resource_type = Field("User", const=True)
    userName: str = Field(None, element_property=True)
    password: str = Field(None, element_property=True)

    @classmethod
    def elements_sequence(cls):
        return [
            "id",
            "meta",
            "implicitRules",
            "language",
            "text",
            "contained",
            "extension",
            "modifierExtension",
            "userName",
            "password",
        ]

define_fhir_resource(
    fhirtypes=fhirtypes, fhirtypesvalidators=fhirtypesvalidators, cls=User
)

This was needed because fhir.resources would try to look up UserType in its fhirtypes and user_validator in fhirtypesvalidators. The above isn't pretty, but I couldn't find a better solution.

from fhir.resources.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.