Exemplo n.º 1
0
def history(request, resource_type, id):
    interaction_type = '_history'
    # 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

    # Read Search Interaction
    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/12345/_history
    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.history(request, resource_type, id)

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

    return HttpResponse(json.dumps(od, indent=4),
                        content_type='application/json')
Exemplo 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
    # FIXME: FHIR_BACKEND not defined, it will raise a `NameError`
    # return FHIR_BACKEND.vread(request, resource_type, id, vid)
    return deny

    # FIXME: this part of code is unreachable
    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')
Exemplo n.º 3
0
def history(request, resource_type, id):
    interaction_type = '_history'

    cx = get_crosswalk(request.user)
    # cx will be the crosswalk record or None
    rr = get_resourcerouter(cx)

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

    # Read Search Interaction
    # Example client use in curl:
    # curl  -X GET http://127.0.0.1:8000/fhir/Practitioner/12345/_history
    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.history(request, resource_type, id)

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

    return HttpResponse(json.dumps(od, indent=4),
                        content_type='application/json')
Exemplo n.º 4
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
    # FIXME: FHIR_BACKEND not defined, it will raise a `NameError`
    # return FHIR_BACKEND.vread(request, resource_type, id, vid)
    return deny

    # FIXME: this part of code is unreachable
    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')
Exemplo n.º 5
0
def search_simple(request, resource_type):
    """Route to search FHIR Interaction"""

    if request.method == 'GET':
        # Search
        return generic_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)
Exemplo n.º 6
0
def oauth_read_or_update_or_delete(request, resource_type, id, *args, **kwargs):
    """
    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)
Exemplo n.º 7
0
def oauth_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)
Exemplo n.º 8
0
def search_simple(request, resource_type):
    """Route to search FHIR Interaction"""

    if request.method == 'GET':
        # Search
        return generic_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)
    logger_info.info(msg)

    return kickout_400(msg)
Exemplo n.º 9
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)
    logger_info.info(msg)

    return kickout_400(msg)
Exemplo n.º 10
0
def search(request, resource_type):
    """
    Search Interaction
    Example client use in curl:
    curl -X GET  http://127.0.0.1:8000/fhir/Practitioner?foo=bar
    """
    # FIXME: variable not used
    # interaction_type = 'search'

    if request.method != 'GET':
        msg = 'HTTP method %s not supported at this URL.' % (request.method)
        return kickout_400(msg)

    # Move to fhir_io_mongo (Plugable back-end)
    od = OrderedDict()
    od['request_method'] = request.method
    od['interaction_type'] = 'search'
    od['resource_type'] = resource_type
    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')
Exemplo n.º 11
0
def search_simple(request, resource_type, *args, **kwargs):
    """Route to search FHIR Interaction"""

    if request.method == 'GET':
        # Search
        logger.debug("searching with Resource:"
                     "%s and Id:%s" % (resource_type, id))

        return read_search(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)
    # logger_info.info(msg)
    logger.debug(msg)

    return kickout_400(msg)
Exemplo n.º 12
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

    We need to deal with possible multiple resourceType or filter by
    FHIRServer from Crosswalk
    """
    # TODO: Filter by FHIRServer
    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(resourceType=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.decode('utf-8'),
                           object_pairs_hook=OrderedDict)
            if not isinstance(j, dict):
                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'): # throws error if id not in OrderedDict
        if 'id' in j:
            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()
        od['request_method'] = request.method
        od['interaction_type'] = 'create'
        od['resource_type'] = resource_type
        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')
Exemplo n.º 13
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.decode('utf-8'), object_pairs_hook=OrderedDict)
            if not isinstance(j, dict):
                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'): # throws error if id not in OrderedDict
        if 'id' in j:
            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()
        od['request_method'] = request.method
        od['interaction_type'] = 'create'
        od['resource_type'] = resource_type
        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')