示例#1
0
def _make_index_item(resource_type):
    """ """
    now_time = datetime.datetime.now()
    now_time.replace(tzinfo=pytz.UTC)

    tpl = {
        "access_scopes": [
            "patient/*.*",
            f"patient/{resource_type}.*",
            f"patient/{resource_type}.read",
            f"patient/{resource_type}.write",
            "user/*.*",
            f"user/{resource_type}.*",
            f"user/{resource_type}.read",
            f"user/{resource_type}.write",
        ],
        "access_users": ["root"],
    }

    with open(str(FHIR_EXAMPLE_RESOURCES / (resource_type + ".json")),
              "rb") as fp:
        data = json_loads(fp.read())

    tpl[resource_type] = data
    tpl["uid"] = uuid.uuid4().hex
    return tpl
示例#2
0
async def add_elasticsearch_doc(
    es_engine: ElasticsearchEngine,
    record: typing.Dict[str, typing.Any],
    create: bool = True,
):
    """ """
    index_name = es_engine.get_index_name()
    doc_type = es_engine.get_doc_type()
    conn = es_engine.connection.raw_connection
    resource_type = record["resource_type"]
    if create is False:
        await conn.delete(index=index_name, id=record["es_id"], doc_type=doc_type)
    body = {
        es_engine.calculate_field_index_name(resource_type): json_loads(
            record["resource"]
        ),
        "access_scopes": [
            "patient/*.*",
            f"patient/{resource_type}.*",
            f"patient/{resource_type}.read",
            f"patient/{resource_type}.write",
            "user/*.*",
            f"user/{resource_type}.*",
            f"user/{resource_type}.read",
            f"user/{resource_type}.write",
        ],
        "access_users": ["root"],
    }
    await conn.create(
        index=index_name, id=record["es_id"], body=body, doc_type=doc_type, refresh=True
    )
示例#3
0
def json_body(request):
    try:
        data = json_loads(request.get("BODY") or b"{}")
    except ValueError:
        raise DeserializationError("No JSON object could be decoded")
    if not isinstance(data, dict):
        raise DeserializationError("Malformed body")
    return data
示例#4
0
def get_elasticsearch_mapping(resource,
                              fhir_release="R4",
                              mapping_dir=None,
                              cache=True):
    """Elastic search mapping for FHIR resources"""
    fhir_release = FHIR_VERSION[fhir_release].name
    if mapping_dir is None:
        mapping_dir = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "browser",
            "static",
            "ES_MAPPINGS",
            fhir_release,
        )
    storage = FHIR_ES_MAPPINGS_CACHE.get(fhir_release)

    if resource not in storage or cache is False:
        file_location = None
        expected_filename = "{0}.mapping.json".format(resource)
        for root, dirs, files in os.walk(mapping_dir, topdown=True):
            for filename in files:
                if filename == expected_filename:
                    file_location = os.path.join(root, filename)
                    break

        if file_location is None:
            raise LookupError("Mapping files {0}/{1} doesn't exists.".format(
                mapping_dir, expected_filename))

        with open(os.path.join(root, file_location), "rb") as f:
            content = json_loads(f.read())
            assert filename.split(".")[0] == content["resourceType"]

            storage[resource] = content

    return storage[resource]["mapping"]
示例#5
0
 def loads(self, s):
     try:
         return json_loads(s)
     except (ValueError, TypeError) as e:
         raise SerializationError(s, e)