Пример #1
0
def vread(request, resource_type, id, vid):

    interaction_type = 'vread'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type, interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    """VRead Interaction"""
    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/12345/_history/1
    if request.method != 'GET':
        msg = "HTTP method %s not supported at this URL." % (request.method)
        return kickout_400(msg)

    #testing direct response
    return FHIR_BACKEND.vread(request, resource_type, id, vid)

    od = OrderedDict()
    od['request_method']= request.method
    od['interaction_type'] = "vread"
    od['resource_type']    = resource_type
    od['id'] = id
    od['vid'] = vid
    od['note'] = "This is only a stub for future implementation"

    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #2
0
def update(request, resource_type, id):
    """Update FHIR Interaction"""
    # Example client use in curl:
    # curl -X PUT -H "Content-Type: application/json" --data @test.json
    #                  http://127.0.0.1:8000/fhir/Practitioner/12345

    interaction_type = 'update'
    # Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type,
                                                      interaction_type)
    if deny:
        # If not allowed, return a 4xx error.
        return deny

    # Replace Section below with call to function in fhir_io_mongo or pluggable backend
    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method'] = request.method
        od['interaction_type'] = "update"
    od['resource_type'] = resource_type
    od['id'] = id
    if DF_EXTRA_INFO:
        od['note'] = "This is only a stub for future implementation"
    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #3
0
def delete(request, resource_type, id):
    """Delete FHIR Interaction"""
    # Example client use in curl:
    # curl -X DELETE -H "Content-Type: application/json" --data @test.json http://127.0.0.1:8000/fhir/Practitioner/12345
    interaction_type = 'delete'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type,
                                                      interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    #testing direct response
    return FHIR_BACKEND_DELETE.delete(request, resource_type, id)

    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method'] = request.method
        od['interaction_type'] = interaction_type
    od['resource_type'] = resource_type
    od['id'] = id
    if DF_EXTRA_INFO:
        od['note'] = "This is only a stub for future implementation"
    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #4
0
def search(request, resource_type):
    interaction_type = 'search'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type, interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    """Search Interaction"""
    # Example client use in curl:
    # curl -X GET  http://127.0.0.1:8000/fhir/Practitioner?foo=bar
    if request.method != 'GET':
        msg = "HTTP method %s not supported at this URL." % (request.method)
        return kickout_400(msg)

    if settings.DEBUG:
        print("FHIR_BACKEND in search:",FHIR_BACKEND_FIND )
    return FHIR_BACKEND_FIND.find(request, resource_type)


    # Move to fhir_io_mongo (Plugable back-end)
    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method']= request.method
        od['interaction_type'] = "search"
    od['resource_type']    = resource_type
    if DF_EXTRA_INFO:
        od['search_params'] = request.GET
        od['note'] = "This is only a stub for future implementation"
    
    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #5
0
def read(request, resource_type, id):
    """Read FHIR Interaction"""
    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/1234
    
    interaction_type = 'read'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type, interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    if settings.DEBUG:
        print("Backend:", FHIR_BACKEND_VIEW)
    #testing direct response
    return FHIR_BACKEND_VIEW.read(request, resource_type, id)

    # move to fhir_io_mongo (pluggable backend)

    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method']= request.method
        od['interaction_type'] = interaction_type
    od['resource_type']    = resource_type
    od['id'] = id
    if DF_EXTRA_INFO:
        od['note'] = "This is only a stub for future implementation"
    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #6
0
def read(request, resource_type, id):
    """Read FHIR Interaction"""
    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/1234

    interaction_type = 'read'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type,
                                                      interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    if settings.DEBUG:
        print("Backend:", FHIR_BACKEND_VIEW)
    #testing direct response
    return FHIR_BACKEND_VIEW.read(request, resource_type, id)

    # move to fhir_io_mongo (pluggable backend)

    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method'] = request.method
        od['interaction_type'] = interaction_type
    od['resource_type'] = resource_type
    od['id'] = id
    if DF_EXTRA_INFO:
        od['note'] = "This is only a stub for future implementation"
    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #7
0
def vread(request, resource_type, id, vid):

    interaction_type = 'vread'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type,
                                                      interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny
    """VRead Interaction"""
    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/12345/_history/1
    if request.method != 'GET':
        msg = "HTTP method %s not supported at this URL." % (request.method)
        return kickout_400(msg)

    #testing direct response
    return FHIR_BACKEND.vread(request, resource_type, id, vid)

    od = OrderedDict()
    od['request_method'] = request.method
    od['interaction_type'] = "vread"
    od['resource_type'] = resource_type
    od['id'] = id
    od['vid'] = vid
    od['note'] = "This is only a stub for future implementation"

    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #8
0
def search(request, resource_type):
    interaction_type = 'search'
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type,
                                                      interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny
    """Search Interaction"""
    # Example client use in curl:
    # curl -X GET  http://127.0.0.1:8000/fhir/Practitioner?foo=bar
    if request.method != 'GET':
        msg = "HTTP method %s not supported at this URL." % (request.method)
        return kickout_400(msg)

    if settings.DEBUG:
        print("FHIR_BACKEND in search:", FHIR_BACKEND_FIND)
    return FHIR_BACKEND_FIND.find(request, resource_type)

    # Move to fhir_io_mongo (Plugable back-end)
    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method'] = request.method
        od['interaction_type'] = "search"
    od['resource_type'] = resource_type
    if DF_EXTRA_INFO:
        od['search_params'] = request.GET
        od['note'] = "This is only a stub for future implementation"

    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #9
0
def delete(request, resource_type, id):
    """Delete FHIR Interaction"""
    # Example client use in curl:
    # curl -X DELETE -H "Content-Type: application/json" --data @test.json http://127.0.0.1:8000/fhir/Practitioner/12345
    interaction_type = "delete"
    # Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type, interaction_type)
    if deny:
        # If not allowed, return a 4xx error.
        return deny

    # testing direct response
    return FHIR_BACKEND_DELETE.delete(request, resource_type, id)

    od = OrderedDict()
    if DF_EXTRA_INFO:
        od["request_method"] = request.method
        od["interaction_type"] = interaction_type
    od["resource_type"] = resource_type
    od["id"] = id
    if DF_EXTRA_INFO:
        od["note"] = "This is only a stub for future implementation"
    return HttpResponse(json.dumps(od, indent=4), content_type="application/json")
Пример #10
0
def update(request, resource_type, id):
    """Update FHIR Interaction"""
    # Example client use in curl:
    # curl -X PUT -H "Content-Type: application/json" --data @test.json
    #                  http://127.0.0.1:8000/fhir/Practitioner/12345
    
    interaction_type = 'update'
    # Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type, interaction_type)
    if deny:
        # If not allowed, return a 4xx error.
        return deny

    # Replace Section below with call to function in fhir_io_mongo or pluggable backend
    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method']= request.method
        od['interaction_type'] = "update"
    od['resource_type']    = resource_type
    od['id'] = id
    if DF_EXTRA_INFO:
        od['note'] = "This is only a stub for future implementation"
    return HttpResponse(json.dumps(od, indent=4),
                        content_type="application/json")
Пример #11
0
def generic_read(request, interaction_type, resource_type, id, vid=None, *args, **kwargs):
    """
    Read from remote FHIR Server
    :param resourcetype:
    :param id:
    :return:


    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/1234

    """

    # interaction_type = 'read' or '_history' or 'vread'
    if settings.DEBUG:
        print("interaction_type:", interaction_type)
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type, interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    srtc = check_rt_controls(resource_type)
    # We get back an Supported ResourceType Control record or None

    if settings.DEBUG:
        if srtc:
            print("Parameter Rectrictions:", srtc.parameter_restriction())
        else:
            print("No Resource Controls found")

    if srtc:
        if srtc.force_url_id_override:
            key = crosswalk_id(request, id)
            # Crosswalk returns the new id or returns None
            if settings.DEBUG:
                print("crosswalk:", key)
        else:
            # No Id_Overide so use the original id
            key = id
    else:
        key = id

    # Do we have a key?
    if key == None:
        return kickout_404("FHIR_IO_HAPI:Search needs a valid Resource Id that is linked "
                           "to the authenticated user "
                           "(%s) which was not available" % request.user)

    # Now we get to process the API Call.

    if settings.DEBUG:
        print("Now we need to evaluate the parameters and arguments"
              " to work with ", key, "and ", request.user)
        print("GET Parameters:", request.GET, ":")

    mask = False
    if srtc:
        if srtc.force_url_id_override:
            mask = True

    in_fmt = "json"
    Txn = {'name': resource_type,
           'display': resource_type,
           'mask': mask,
           'server': settings.FHIR_SERVER,
           'locn': "/baseDstu2/"+resource_type+"/",
           'in_fmt': in_fmt,
           }

    skip_parm = []
    if srtc:
        skip_parm = srtc.parameter_restriction()

    #skip_parm = ['_id',
    #             'access_token', 'client_id', 'response_type', 'state']

    if settings.DEBUG:
        print('Masking the following parameters', skip_parm)
    # access_token can be passed in as a part of OAuth protected request.
    # as can: state=random_state_string&response_type=code&client_id=ABCDEF
    # Remove it before passing url through to FHIR Server

    pass_params = build_params(request.GET, skip_parm)
    if settings.DEBUG:
        print("Parameters:", pass_params)

    if interaction_type == "vread":
        pass_to = Txn['server'] + Txn['locn'] + key + "/" + "_history" + "/" + vid
    elif interaction_type == "_history":
        pass_to = Txn['server'] + Txn['locn'] + key + "/" + "_history"
    else:  # interaction_type == "read":
        pass_to = Txn['server'] + Txn['locn'] + key + "/"

    print("Here is the URL to send, %s now get parameters %s" % (pass_to,pass_params))

    if pass_params != "":
        pass_to = pass_to + pass_params

    # Now make the call to the backend API
    try:
        r = requests.get(pass_to)

    except requests.ConnectionError:
        if settings.DEBUG:
            print("Problem connecting to FHIR Server")
        messages.error(request, "FHIR Server is unreachable." )
        return HttpResponseRedirect(reverse_lazy('api:v1:home'))

    if r.status_code in [301, 302, 400, 403, 404, 500]:
        return error_status(r, r.status_code)

    text_out = ""
    print("r:", r.text)

    if '_format=xml' in pass_params:
        text_out= minidom.parseString(r.text).toprettyxml()
    else:
        text_out = r.json()

    od = OrderedDict()
    od['request_method']= request.method
    od['interaction_type'] = interaction_type
    od['resource_type']    = resource_type
    od['id'] = key
    if vid != None:
        od['vid'] = vid

    if settings.DEBUG:
        print("Query List:", request.META['QUERY_STRING'] )

    od['parameters'] = request.GET.urlencode()

    if settings.DEBUG:
        print("or:", od['parameters'])

    if '_format=xml' in pass_params.lower():
        fmt = "xml"
    elif '_format=json' in pass_params.lower():
        fmt = "json"
    else:
        fmt = ''
    od['format'] = fmt
    od['bundle'] = text_out
    od['note'] = 'This is the %s Pass Thru (%s) ' % (resource_type,key)

    if settings.DEBUG:
        od['note'] += 'using: %s ' % (pass_to)
        print(od)

    if od['format'] == "xml":
        if settings.DEBUG:
            print("We got xml back in od")
        return HttpResponse( tostring(dict_to_xml('content', od)),
                             content_type="application/%s" % od['format'])
    elif od['format'] == "json":
        if settings.DEBUG:
            print("We got json back in od")
        return HttpResponse(json.dumps(od, indent=4),
                            content_type="application/%s" % od['format'])

    if settings.DEBUG:
        print("We got a different format:%s" % od['format'])
    return render(request,
                  'fhir_io_hapi/default.html',
                  {'content': json.dumps(od, indent=4),
                   'output': od},
                  )
Пример #12
0
def generic_read(request,
                 interaction_type,
                 resource_type,
                 id,
                 vid=None,
                 *args,
                 **kwargs):
    """
    Read from remote FHIR Server
    :param resourcetype:
    :param id:
    :return:


    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/1234

    """

    # interaction_type = 'read' or '_history' or 'vread'
    if settings.DEBUG:
        print("interaction_type:", interaction_type)
    #Check if this interaction type and resource type combo is allowed.
    deny = check_access_interaction_and_resource_type(resource_type,
                                                      interaction_type)
    if deny:
        #If not allowed, return a 4xx error.
        return deny

    srtc = check_rt_controls(resource_type)
    # We get back an Supported ResourceType Control record or None

    if settings.DEBUG:
        if srtc:
            print("Parameter Restrictions:", srtc.parameter_restriction())
        else:
            print("No Resource Controls found")

        print("Working with id:", id)

    key = id
    if srtc:
        if srtc.force_url_id_override:
            key = crosswalk_id(request, id)
            if key == None:
                if not id == None:
                    key = id

            # Crosswalk returns the new id or returns None
            if settings.DEBUG:
                print("crosswalk:", key, ":", request.user)
        else:
            # No Id_Overide so use the original id
            key = id
    else:
        key = id

    # Do we have a key?
    # if key == None:
    #     return kickout_404("FHIR_IO_HAPI:Search needs a valid Resource Id that is linked "
    #                        "to the authenticated user "
    #                        "(%s) which was not available" % request.user)

    # Now we get to process the API Call.

    if settings.DEBUG:
        print(
            "Now we need to evaluate the parameters and arguments"
            " to work with ", key, "and ", request.user)
        print("GET Parameters:", request.GET, ":")

    mask = False
    if srtc:
        if srtc.force_url_id_override:
            mask = True

    in_fmt = "json"
    Txn = {
        'name': resource_type,
        'display': resource_type,
        'mask': mask,
        'in_fmt': in_fmt,
    }

    skip_parm = []
    if srtc:
        skip_parm = srtc.parameter_restriction()

    #skip_parm = ['_id',
    #             'access_token', 'client_id', 'response_type', 'state']

    if settings.DEBUG:
        print('Masking the following parameters', skip_parm)
    # access_token can be passed in as a part of OAuth protected request.
    # as can: state=random_state_string&response_type=code&client_id=ABCDEF
    # Remove it before passing url through to FHIR Server

    pass_params = build_params(request.GET, skip_parm)
    if settings.DEBUG:
        print("Parameters:", pass_params)

    if interaction_type == "vread":
        pass_to = FhirServerUrl(
        ) + "/" + resource_type + "/" + key + "/" + "_history" + "/" + vid
    elif interaction_type == "_history":
        pass_to = FhirServerUrl(
        ) + "/" + resource_type + "/" + key + "/" + "_history"
    else:  # interaction_type == "read":
        pass_to = FhirServerUrl() + "/" + resource_type + "/" + key + "/"

    print("Here is the URL to send, %s now get parameters %s" %
          (pass_to, pass_params))

    if pass_params != "":
        pass_to += pass_params

    # Now make the call to the backend API
    try:
        r = requests.get(pass_to)

    except requests.ConnectionError:
        if settings.DEBUG:
            print("Problem connecting to FHIR Server")
        messages.error(request, "FHIR Server is unreachable.")
        return HttpResponseRedirect(reverse_lazy('api:v1:home'))

    if r.status_code in [301, 302, 400, 403, 404, 500]:
        return error_status(r, r.status_code)

    text_out = ""
    if settings.DEBUG:
        print("r:", r.text)

    if '_format=xml' in pass_params:
        text_out = minidom.parseString(r.text).toprettyxml()
    else:
        text_out = r.json()

    od = OrderedDict()
    if DF_EXTRA_INFO:
        od['request_method'] = request.method
        od['interaction_type'] = interaction_type
    od['resource_type'] = resource_type
    od['id'] = key
    if vid != None:
        od['vid'] = vid

    if settings.DEBUG:
        print("Query List:", request.META['QUERY_STRING'])

    if DF_EXTRA_INFO:
        od['parameters'] = request.GET.urlencode()
        if settings.DEBUG:
            print("or:", od['parameters'])

    if '_format=xml' in pass_params.lower():
        fmt = "xml"
    elif '_format=json' in pass_params.lower():
        fmt = "json"
    else:
        fmt = ''

    if DF_EXTRA_INFO:
        od['format'] = fmt
    od['bundle'] = text_out

    if DF_EXTRA_INFO:
        od['note'] = 'This is the %s Pass Thru (%s) ' % (resource_type, key)
        if settings.DEBUG:
            od['note'] += 'using: %s ' % (pass_to)
            print(od)

    if fmt == "xml":
        if settings.DEBUG:
            print("We got xml back in od")
        return HttpResponse(tostring(dict_to_xml('content', od)),
                            content_type="application/%s" % fmt)
    elif fmt == "json":
        if settings.DEBUG:
            print("We got json back in od")
        return HttpResponse(json.dumps(od, indent=4),
                            content_type="application/%s" % fmt)

    if settings.DEBUG:
        print("We got a different format:%s" % fmt)
    return render(
        request,
        'fhir_io_hapi/default.html',
        {
            'content': json.dumps(od, indent=4),
            'output': od
        },
    )