Beispiel #1
0
def check_appointment_reponse(schema, id, date):
    connection = make_db_connection()
    cur = connection.cursor()

    # If both responses were success
    query = """
        SELECT * FROM {}.appointments_temp WHERE id = {} AND 
        anesthetist_appointment_response is true AND
        surgeon_appointment_response is true AND
        appointment_success is null
    """.format(schema, id)
    cur.execute(query)
    response = cur.fetchone()

    connection.close()
    if response:
        return commit_appointment(id, schema, date)

    # If at least one of them failed
    connection = make_db_connection()
    cur = connection.cursor()
    query = """
        SELECT * FROM {}.appointments_temp WHERE id = {} AND 
        (anesthetist_appointment_response is false OR
        surgeon_appointment_response is false)
    """.format(schema, id)
    cur.execute(query)
    response = cur.fetchone()

    connection.close()
    if response:
        return abort(id, schema, date)
Beispiel #2
0
def _check_appointment(id, date, schema, restore=False):
    connection = make_db_connection()
    cur = connection.cursor()

    # Check agenda
    check_if_appointment_exist = """
        SELECT * FROM {}.appointments WHERE appointment_date = '{}' 
    """.format(schema, date)
    cur.execute(check_if_appointment_exist)
    response = cur.fetchone()

    if response and schema == 'hospital':
        return abort(id, schema, date, False)
    elif response:
        return abort(id, schema, date)

    # Check temporary agenda
    check_if_appointment_temp_exist = """
        SELECT * FROM {}.appointments_temp WHERE appointment_date = '{}' 
        AND appointment_success is null AND id != {}
    """.format(schema, date, id)
    cur.execute(check_if_appointment_temp_exist)
    response = cur.fetchone()
    connection.close()

    if response:
        return "you are on queue"

    # Else check the valid agenda
    return _myself_success(id, schema, date)
Beispiel #3
0
def commit_appointment(id, schema, date):
    connection = make_db_connection()
    cur = connection.cursor()

    # Check temporary agenda
    query = """
        UPDATE {}.appointments_temp SET appointment_success = '1'
        WHERE id = {}
    """.format(schema, id)
    cur.execute(query)

    query = """
        INSERT INTO {}.appointments (id, appointment_date) 
        VALUES ({}, '{}')
    """.format(schema, id, date)
    cur.execute(query)

    # Query all other appointments on queue
    query = """
        UPDATE {}.appointments_temp SET appointment_success = '0'
        WHERE appointment_date = '{}' AND id != {} 
    """.format(schema, date, id)
    cur.execute(query)
    connection.commit()
    connection.close()

    if schema == 'hospital':
        _call_your_friend(id, schema, 'SURGEON', date, 1)
        _call_your_friend(id, schema, 'ANESTHETIST', date, 1)

    return 'appointment successs'
Beispiel #4
0
def receive_update_appointment(id, schema, persona, response):
    connection = make_db_connection()
    cur = connection.cursor()

    insert_appointment = """
        UPDATE {}.appointments_temp 
        SET {}_appointment_response = '{}'
        WHERE id = {}
    """.format(schema, persona, response, id)

    cur.execute(insert_appointment)
    connection.commit()
    connection.close()
Beispiel #5
0
def check_appointment_state(id):
    connection = make_db_connection()
    cur = connection.cursor()

    # Check temporary agenda
    query = """
        SELECT appointment_success FROM hospital.appointments_temp 
        WHERE id = {}
    """.format(id)

    cur.execute(query)
    response = cur.fetchone()[0]
    connection.close()
    return response
Beispiel #6
0
def check_appointment_status(schema, id):
    connection = make_db_connection()
    cur = connection.cursor()

    # Check temporary agenda
    query = """
        SELECT myself_success FROM {}.appointments_temp WHERE id = {} 
        AND appointment_success is null
    """.format(schema, id)

    cur.execute(query)
    response = cur.fetchone()
    connection.close()
    if response:
        return 1
Beispiel #7
0
def _continue_queue(date, schema):
    connection = make_db_connection()
    cur = connection.cursor()

    # Check temporary agenda
    query = """
        SELECT min(id) FROM {}.appointments_temp WHERE appointment_date = '{}' 
        AND appointment_success is null 
    """.format(schema, date)

    cur.execute(query)
    id = cur.fetchone()[0]
    connection.close()
    if id:
        _check_appointment(id, date, schema)
Beispiel #8
0
def abort(id, schema, date, send_response=True):
    if send_response == True:
        connection = make_db_connection()
        cur = connection.cursor()

        insert_appointment = """
            UPDATE {}.appointments_temp SET appointment_success = '0',
            myself_success = '0'
            WHERE id = {}
        """.format(schema, id)

        cur.execute(insert_appointment)
        connection.commit()
        connection.close()

        if schema == 'hospital':
            _call_your_friend(id, schema, 'SURGEON', date, 0)
            _call_your_friend(id, schema, 'ANESTHETIST', date, 0)
            _continue_queue(date, schema)
        else:
            _call_your_friend(id, schema, 'HOSPITAL', date, 0)
    else:
        connection = make_db_connection()
        cur = connection.cursor()

        insert_appointment = """
            UPDATE {}.appointments_temp SET appointment_success = '0',
            myself_success = '0'
            WHERE id = {}
        """.format(schema, id)

        cur.execute(insert_appointment)
        connection.commit()
        connection.close()

    return 'appointment denied'
Beispiel #9
0
def find_next_incomplete_appointments(schema):
    connection = make_db_connection()
    cur = connection.cursor()

    # Check temporary agenda
    query = """
        SELECT * FROM {}.appointments_temp 
            WHERE myself_success is null 
            AND id = (SELECT MIN(id) FROM {}.appointments_temp 
                        WHERE myself_success is null)
    """.format(schema, schema)

    cur.execute(query)
    response = cur.fetchone()
    connection.close()
    return response
Beispiel #10
0
def _myself_success(id, schema, date, restore=False):
    # UPDATE MYSELF SUCCESS
    connection = make_db_connection()
    cur = connection.cursor()

    check_if_appointment_exist = """
        UPDATE {}.appointments_temp SET myself_success = '1' WHERE id = {}
    """.format(schema, id)
    cur.execute(check_if_appointment_exist)
    connection.commit()
    connection.close()

    if restore:
        return call_check_appointment_state(id, date, schema)
    if schema == 'hospital':
        _call_your_friend(id, schema, 'ANESTHETIST', date, 2)
        _call_your_friend(id, schema, 'SURGEON', date, 2)
    else:
        _call_your_friend(id, schema, 'HOSPITAL', date, 1)

    return True
Beispiel #11
0
def insert_request(date, schema, id=None):
    time.sleep(2)
    connection = make_db_connection()
    cur = connection.cursor()

    if schema == 'hospital':
        insert_request = """
            INSERT INTO {}.appointments_temp (appointment_date)
            VALUES ('{}') RETURNING id;
        """.format(schema, date)
    else:
        insert_request = """
            INSERT INTO {}.appointments_temp (id, appointment_date)
            VALUES ({}, '{}') RETURNING id;
        """.format(schema, id, date)

    cur.execute(insert_request)
    connection.commit()
    id = cur.fetchone()[0]
    connection.close()

    return _check_appointment(id, date, schema)