Exemple #1
0
def add():
    """Add patient to database"""

    formatted_patient = controllers.check_request(current_app.db, request)
    if isinstance(formatted_patient, int): # some error must have occurred during validation
        return controllers.bad_request(formatted_patient)

    match_external = False
    if controllers.get_nodes(database=current_app.db):
        match_external = True

    # else import patient to database
    modified, inserted, matching_obj= backend_add_patient(mongo_db=current_app.db, patient=formatted_patient,
        match_external=match_external)
    message = {}

    if modified:
        message['message'] = 'Patient was successfully updated.'
    elif inserted:
        message['message'] = 'Patient was successfully inserted into database.'
    else:
        message['message'] = 'Database content is unchanged.'

    # 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(matching_obj.get('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(message)
    resp.status_code = 200
    return resp
def test_notify_match_external(match_objs, mock_sender, mock_mail):

    match_obj = match_objs[0]  #an external match object with results
    assert match_obj['match_type'] == 'external'

    # When calling the function that sends external match notifications
    notify_complete = True  # test notification of complete patient data by email
    notify_match_external(match_obj, mock_sender, mock_mail, notify_complete)

    # make sure send method was called
    assert mock_mail._send_was_called

    # and that mail object message was set correctly
    assert mock_mail._message
Exemple #3
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
def asynch_match_response():
    """Receives and handles asynchronous match responses.
    This implies that patientMatcher has already submitted a match request
    to a server x and has received not results, but a query id in response.
    Query id was previously saved into patientMatcher database.
    This delayed response from server x contains match results and
    the same query id and server x source key.
    """
    LOG.info("Receiving an asynchronous request.")
    data = controllers.check_async_request(current_app.db, request)
    # returned data after check_async_request is a dictionary if request is valid
    if isinstance(data,
                  int):  # some error must have occurred during validation
        return controllers.bad_request(data)

    # create a match object from specifics you first sent to the node
    async_match_obj = async_match(current_app.db, data)
    if async_match_obj:
        # save match object to database
        LOG.info("saving match object to database")
        current_app.db["matches"].insert_one(async_match_obj)

        # Remove query_id from async responses, this is not used any more
        current_app.db["async_responses"].delete_one(
            {"query_id": data["query_id"]})
    else:
        message = "Error: could not create a valid match object from request data"
        resp = jsonify({"message": message})
        resp.status_code = 200
        return resp

    # notify matches if there are matching patients and app is configured to do so
    if len(async_match_obj["results"]) > 0 and current_app.config.get(
            "MAIL_SERVER"):
        # send an email to patient's contact:
        notify_match_external(
            match_obj=async_match_obj,
            admin_email=current_app.config.get("MAIL_USERNAME"),
            mail=current_app.mail,
            notify_complete=current_app.config.get("NOTIFY_COMPLETE"),
        )

    resp = jsonify({"message": "results received, many thanks!"})
    resp.status_code = 200
    return resp