コード例 #1
0
ファイル: controllers.py プロジェクト: ousamg/patientMatcher
def check_request(database, request):
    """Check if request is valid, if it is return MME formatted patient
       Otherwise return error code.
    """
    check_result = None

    # check that request is using a valid auth token
    if not authorize(database, request):
        LOG.info("Request is not authorized")
        return 401

    try: # make sure request has valid json data
        request_json = request.get_json(force=True)
    except Exception as err:
        LOG.info("Json data in request is not valid:{}".format(err))
        return 400

    try: # validate json data against MME API
        validate_api(json_obj=request_json, is_request=True)
    except Exception as err:
        LOG.info("Patient data does not conform to API:{}".format(err))
        return 422

    formatted_patient = mme_patient(json_patient=request_json['patient'],
        convert_to_ensembl = True)
    return formatted_patient
コード例 #2
0
def nodes():
    """Get a list of all nodes connected to this MME server"""
    resp = None
    if not authorize(current_app.db, request):
        message = STATUS_CODES[401]['message']
        resp = jsonify(message)
        resp.status_code = 401
        return resp

    LOG.info('Authorized client requests list of connected nodes..')
    results = controllers.get_nodes(database=current_app.db)
    resp = jsonify(results)
    resp.status_code = 200
    return resp
コード例 #3
0
def delete(patient_id):
    #check if request is authorized
    resp = None
    if not authorize(current_app.db, request): # not authorized, return a 401 status code
        message = STATUS_CODES[401]['message']
        resp = jsonify(message)
        resp.status_code = 401
        return resp

    LOG.info('Authorized client is removing patient with id {}'.format(patient_id))
    message = controllers.delete_patient(current_app.db, patient_id)
    resp = jsonify(message)
    resp.status_code = 200
    return resp
コード例 #4
0
def match_external(patient_id):
    """Trigger a patient matching on external nodes by providing a patient ID"""
    resp = None
    message = {}
    if not authorize(current_app.db, request): # not authorized, return a 401 status code
        message['message'] = STATUS_CODES[401]['message']
        resp = jsonify(message)
        resp.status_code = 401
        return resp

    LOG.info('Authorized clients is matching patient with ID {} against external nodes'.format(patient_id))
    query_patient = controllers.patient(current_app.db, patient_id)

    if not query_patient:
        LOG.error('ERROR. Could not find any patient with ID {} in database'.format(patient_id))
        message['message'] = "ERROR. Could not find any patient with ID {} in database".format(patient_id)
        resp = jsonify(message)
        resp.status_code = 200
        return resp

    node = request.args.get('node')

    # if search should be performed on a specific node, make sure node is in database
    if node and current_app.db['nodes'].find_one({'_id':node}) is None:
        LOG.info('ERROR, theres no node with id "{}" in database'.format(request.args['node']))
        message['message'] = 'ERROR. Could not find any connected node with id {} in database'.format(request.args['node'])
        resp = jsonify(message)
        resp.status_code = 200
        return resp

    matching_obj = controllers.match_external(current_app.db, query_patient, node)

    if not matching_obj:
        message['message'] = "Could not find any other node connected to this MatchMaker server"
        resp = jsonify(message)
        resp.status_code = 200
        return resp

    results = matching_obj.get('results')

    # if patient is matching any other patient on other nodes
    # and match notifications are on
    if current_app.config.get('MAIL_SERVER') and matching_obj and len(results):
        # send an email to patient's contact:
        notify_match_external(match_obj=matching_obj, admin_email=current_app.config.get('MAIL_USERNAME'),
            mail=current_app.mail, notify_complete=current_app.config.get('NOTIFY_COMPLETE'))

    resp = jsonify({'results':results})
    resp.status_code = 200
    return resp
コード例 #5
0
def heartbeat():
    """Get the server specs"""
    resp = None
    if authorize(current_app.db, request):
        LOG.info('Authorized client requests heartbeat..')
        LOG.info('current_app is {}'.format(current_app))
        disclaimer = current_app.config.get('DISCLAIMER')
        result = controllers.heartbeat(disclaimer)
        resp = jsonify(result)
        resp.status_code = 200

    else:  # not authorized, return a 401 status code
        return controllers.bad_request(401)

    return resp
コード例 #6
0
def metrics():
    """Get database metrics"""
    resp = None
    if authorize(current_app.db, request):
        LOG.info('Authorized client requests metrics..')
        results = controllers.metrics(database=current_app.db)
        resp = jsonify({
            'metrics': results,
            'disclaimer': current_app.config.get('DISCLAIMER')
        })
        resp.status_code = 200

    else:  # not authorized, return a 401 status code
        return controllers.bad_request(401)

    return resp
コード例 #7
0
def matches(patient_id):
    """Get all matches (external and internal) for a patient ID"""
    LOG.info('getting all matches for patient {}'.format(patient_id))
    resp = None
    message = {}
    if not authorize(current_app.db, request): # not authorized, return a 401 status code
        message = STATUS_CODES[401]['message']
        resp = jsonify(message)
        resp.status_code = 401
        return resp

    # return only matches with at least one result
    results = patient_matches(current_app.db, patient_id)
    if results:
        message = json.loads(json_util.dumps({'matches' : results}))
    else:
        message['message'] = "Could not find any matches in database for patient ID {}".format(patient_id)
    resp = jsonify(message)
    resp.status_code = 200
    return resp