PyMODM Autogenerated

This is an example implementation of a fhirbug server using mongodb via PyMODM.

Warning

This is meant as a demo implementation aimed to demonstrate some of the capabilities of fhirbug. IT IS NOT A PRODUCTION READY SERVER, please do not use it as one!

It uses models generated from the FHIR specification files so it covers pretty much every possible resource and can populate the database using FHIR’s official examples.

Installation

Running the example server locally

The only requirement besides the python packages is mongodb. If you are on Linux, installing it is probably as simple as:

$ sudo apt install mongodb-server

or equivalent. See detailed instructions for your platform here.

All python packages required are in requirements.txt, so, in a Python >= 3.6 environment, do:
$ pip install -r requirements.txt

and you should be set to go.

Starting the development server

In order to run the server locally for testing/debugging run:

$ python examples/pymodm_autogenerated/flask_app.py

from the project’s root directory.

Deploying using gunicorn

If you intend to put heavier load on the server,it is recommended to run it using a wsgi server. Fhirbug comes with gunicorn included in it’s requirements but you can use any alternative as long as it can serve a flak app (so all of them).

So, if you haven’t installed gunicorn from the server’s requirements.txt, do:

$ pip install gunicorn

and then:

$ cd examples/pymodm_autogenerated

$ gunicorn --bind 0.0.0.0:5001 flask_app:app

It is recommended to serve the app behind a proxy server like nginx. Have a look at https://gunicorn.org/#deployment for more details.

Deploying via docker

First, make sure you have Docker and Docker Compose installed.

Then, simply clone the git repository and start the instances:

$ git clone https://github.com/zensoup/fhirbug.git

$ cd fhirbug/examples/pymodm_autogenerated

$ sudo docker-compose up

By default, this will create one docker instance for mongodb and one for python. It will then download the example files from fhir.org and populate the database with the example resources. Finally, it will start serving the application, binding to port 5000.

If you do not want to generate example resources you can edit the DockerFile before running docker-compose up.

If you want to acces the mongo database from the host machine for debugging purposes edit docker-compose.yml to contain the following:

 version: '3'
 services:
   web:
     build: .
     ports:
         - "5000:5000"
     depends_on:
         - mongo
   mongo:
     image: "mongo:4"
     ports:
         - "27018:27017"

Then, you can connect to the db from the command line:

$ mongo mongodb://localhost:27018

Generating Example data

The demo server comes with a couple of scripts that download the FHIR specification files from http://hl7.org/fhir/ and populates the database with the examples included within it.

Note

If you are developing on fhirbug please note that these files are also used to generate the fixtures for some of fhirbug’s tests. So you should be aware that you may see failing tests if you change these files.

Downloading the specification files

To download the specification files fron the hl7 archive can either use:

$ python examples/pymodm_autogenerated/tools/download_examples.py examples/pymodm_autogenerated/test_cache

or:

$ cd examples/pymodm_autogenerated/

$ python tools/download_examples.py

This will create a folder called test_cache inside the dem server’s root directory.

Using your own sample data

You can feed the fixture generation script any valid FHIR JSON formatted data and it will seed the database using them, as long as you place inside the test_cache folder. For the script to recogninse them as seed data, they must fit the glob pattern *example*.json. So any set of files like example-1.json, example-2.json, etc would be recognized and parsed for use as seed data.

Populating the database

Once you have a cache folder with the seed data you want to use, run:

$ python examples/pymodm_autogenerated/generate_examples.py

This script will read the database configuration in examples/pymodm_autogenerated/settings.py and use that caonnection to write the documents.

Warning

This script drops the database before starting so you will loose any existing documents in that database.

Generating the Models

Note

These models have already been generated and live in examples/pymodm_autogenerated/mappings.py. This section only applies if you want to customize the way models are generated.

The script in examples/pymodm_autogenerated/tools/generate_pymodm_schema.py goes through all of fhirbug’s FHIR resource classes and creates code for the corresponding fhirbug Mappings.

You can call it simply by calling:

$ python examples/pymodm_autogenerated/tools/generate_pymodm_schema.py <output_path>

By default this will create a file in examples/pymodm_autogenerated called mappings.py. You can pass a path to the script to override this behavior.

Limitations

  • There is currently a bug in pymodm that does not allow cyclic references between models. This means that some resource attributes have not been included in the generated models.

    Namely, the attributes that are missing from the generated models are:

    CodeSystemConcept.concept
    CompositionSection.section
    ConsentProvision.provision
    ContractTerm.group
    ExampleScenarioProcessStep.process
    ExampleScenarioProcessStepAlternative.step
    Extension.extension
    FHIRReference.identifier
    GraphDefinitionLinkTarget.link
    ImplementationGuideDefinitionPage.page
    MedicinalProductAuthorizationProcedure.application
    MedicinalProductPackagedPackageItem.packageItem
    OperationDefinitionParameter.part
    ParametersParameter.part
    QuestionnaireItem.item
    QuestionnaireResponseItem.item
    QuestionnaireResponseItemAnswer.item
    RequestGroupAction.action
    resource.action
    StructureMapGroupRule.rule
    SubstanceSpecificationName.synonym
    SubstanceSpecificationName.translation
    ValueSetExpansionContains.contains
    

    plus all of the value attributes of the Extension resource:

    valueAddress, valueAge, valueAnnotation, valueAttachment, valueCodeableConcept, valueCoding, valueContactDetail, valueContactPoint, valueContributor, valueCount, valueDataRequirement, valueDistance, valueDosage, valueDuration, valueExpression, valueHumanName, valueIdentifier, valueMoney, valueParameterDefinition, valuePeriod, valueQuantity, valueRange, valueRatio, valueReference, valueRelatedArtifact, valueSampledData, valueSignature, valueTiming, valueTriggerDefinition, valueUsageContext,