Esempio n. 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")
Esempio n. 2
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")
Esempio n. 3
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")
Esempio n. 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")
Esempio n. 5
0
def error_status(r, status_code=404, reason="undefined error occured"):
    """
    Generate an error page
    based on fhir.utils.kickout_xxx
    :param reason:
    :param status_code:
    :return:
    """
    error_detail = r.text
    if settings.DEBUG:
        if r.text[0] == "<":
            error_detail = "xml:"
            error_detail += r.text
        else:
            error_detail = r.json()

    if reason == "undefined error occured":
        if status_code == 404:
            reason = "page not found"
            kickout_404(reason)
        elif status_code == 403:
            reason = "You are not authorised to access this page. Do you need to login?"
            kickout_403(reason)
        elif status_code == 400:
            reason = "There was a problem with the data"
            kickout_400(reason)
        elif status_code == 301:
            reason = "The requested page has been permanently moved"
            kickout_301(reason)
        elif status_code == 502:
            reason = "Bad gateway"
            kickout_502(reason)

    response = OrderedDict()

    response["errors"] = [reason, error_detail]
    response["code"] = status_code

    return HttpResponse(json.dumps(response, indent=4),
                        status=status_code,
                        content_type="application/json")
Esempio n. 6
0
def error_status(r, status_code=404, reason="undefined error occured"):
    """
    Generate an error page
    based on fhir.utils.kickout_xxx
    :param reason:
    :param status_code:
    :return:
    """
    error_detail = r.text
    if settings.DEBUG:
        if r.text[0] == "<":
            error_detail = "xml:"
            error_detail += r.text
        else:
            error_detail = r.json()

    if reason == "undefined error occured":
       if status_code == 404:
           reason = "page not found"
           kickout_404(reason)
       elif status_code == 403:
           reason = "You are not authorised to access this page. Do you need to login?"
           kickout_403(reason)
       elif status_code == 400:
           reason = "There was a problem with the data"
           kickout_400(reason)
       elif status_code == 301:
           reason = "The requested page has been permanently moved"
           kickout_301(reason)
       elif status_code == 502:
           reason = "Bad gateway"
           kickout_502(reason)

    response= OrderedDict()

    response["errors"] = [reason, error_detail]
    response["code"] = status_code

    return HttpResponse(json.dumps(response, indent = 4),
                        status=status_code,
                        content_type="application/json")
Esempio n. 7
0
def read_or_update_or_delete(request, resource_type, id):
    """Route to read, update, or delete based on HTTP method FHIR Interaction"""

    if request.method == 'GET':
        # Read
        return read(request, resource_type, id)
    elif request.method == 'PUT':
        # update
        return update(request, resource_type, id)
    elif request.method == 'DELETE':
        # delete
        return delete(request, resource_type, id)
    #else:
    # Not supported.
    msg = "HTTP method %s not supported at this URL." % (request.method)
    return kickout_400(msg)
Esempio n. 8
0
def create(request, resource_type):
    """Create FHIR Interaction"""
    # Example client use in curl:
    # curl -H "Content-Type: application/json" --data @test.json http://127.0.0.1:8000/fhir/Practitioner
    interaction_type = 'create'
    # re-route to hello if no resource type is given:
    if not resource_type:
        return hello(request)

    try:
        rt = SupportedResourceType.objects.get(resource_name=resource_type)
        if interaction_type not in rt.get_supported_interaction_types(
        ) and request.method == "GET":
            # GET means that this is a search so re-route
            return search(request, resource_type)

        elif interaction_type not in rt.get_supported_interaction_types():
            msg = "The interaction %s is not permitted on %s FHIR resources on this FHIR sever." % (
                interaction_type, resource_type)
            return kickout_403(msg)

    except SupportedResourceType.DoesNotExist:
        msg = "%s is not a supported resource type on this FHIR server." % (
            resource_type)
        return kickout_404(msg)

    # Catch all for GETs to re-direct to search if CREATE permission is valid
    if request.method == "GET":
        return search(request, resource_type)

    if request.method == 'POST':
        # Check if request body is JSON ------------------------
        try:
            j = json.loads(request.body, object_pairs_hook=OrderedDict)
            if type(j) != type({}):
                kickout_400(
                    "The request body did not contain a JSON object i.e. {}.")
        except:
            return kickout_400("The request body did not contain valid JSON.")

        if j.has_key('id'):
            return kickout_400(
                "Create cannot have an id. Perhaps you meant to perform an update?"
            )

        # check json_schema is valid
        try:
            json_schema = json.loads(rt.json_schema,
                                     object_pairs_hook=OrderedDict)

        except:
            return kickout_500(
                "The JSON Schema on the server did not contain valid JSON.")

        # Check jsonschema
        if json_schema:
            try:
                validate(j, json_schema)
            except ValidationError:
                msg = "JSON Schema Conformance Error. %s" % (str(
                    sys.exc_info()[1][0]))
                return kickout_400(msg)

        # write_to_mongo - TBD
        response = OrderedDict()
        response['id'] = str(uuid.uuid4())

        meta = OrderedDict()

        if j.get('meta').get('versionId'):
            meta['versionId'] = j.get('meta').get('versionId')
        else:
            meta['versionId'] = 1

        if j.get('meta').get('lastUpdated'):
            meta['lastUpdated'] = j.get('meta').get('lastUpdated')
        else:
            meta['lastUpdated'] = "%sZ" % (
                datetime.datetime.utcnow().isoformat())

        meta['id'] = response['id']
        response['meta'] = meta

        hr = HttpResponse(json.dumps(response, indent=4),
                          status=201,
                          content_type="application/json")
        hr['Location'] = "%s/%s/%s/_history/%s" % (
            "http://127.0.0.1:8000/fhir", resource_type, meta['id'],
            meta['versionId'])
        return hr

    #This is something other than GET or POST (i.e. a  GET)
    if request.method not in ("GET", "POST"):
        od = OrderedDict()
        if DF_EXTRA_INFO:
            od['request_method'] = request.method
            od['interaction_type'] = "create"

        od['resource_type'] = resource_type

        if DF_EXTRA_INFO:
            od['note'] = "Perform an HTTP POST to this URL with the JSON resource as the request body."

        return HttpResponse(json.dumps(od, indent=4),
                            content_type="application/json")