def handle_component_not_found(client_state, username, component_id):
    """Handles the error when the server fails to find a component"""
    current_app.logger.debug("Failed to find component " + str(component_id) + \
            ". Trying to resolve issue...")

    # Was it the component that the user is currently on?
    if client_state["componentid"] == component_id:
        # Yes
        sequence_id = 0
        try:
            # Get the sequence
            sequence_id = client_state["sequenceid"]
            sequence = db.get_sequence(sequence_id)

            # Move the client to the first unit operation
            client_state["componentid"] = sequence["components"][0]["id"]
            db.update_user_client_state(username, client_state)
            current_app.logger.warn("Redirecting user to the " + \
                    "first component of sequence " + str(sequence_id))
        except Exceptions.SequenceNotFoundException as ex:
            # Sequence not found
            current_app.logger.error("Sequence Not Found Exception" + str(ex))
            return handle_sequence_not_found(client_state,
                    username, sequence_id)

    # Return the state
    elixys_get_state = Elixys_Get_State()
    return elixys_get_state.state_index()
def save_client_state_and_return(client_state, username):
    '''
    Function expects a client state and a string username.
    Function shall update the client state before calling
    GET /State from Elixys_Get_State().
    Function shall return the output of state_index() called
    from Elixys_Get_State.
    '''
    db.update_user_client_state(username, client_state)
    # Call GET /STATE and return
    get_state = Elixys_Get_State()
    return get_state.state_index()
def handle_reagent_not_found(client_state, username, reagent_id):
    """Handles the error when the server fails to find a reagent"""
    current_app.logger.debug("Failed to find reagent " + str(reagent_id))

    # This error should only occur if the user has
    # the sequence they are currently viewing delete out from
    # under them.  Redirect them to the last Select Sequence screen
    client_state = direct_to_last_select_screen(client_state)
    db.update_user_client_state(username, client_state)
    current_app.logger.warn("Redirecting user to select sequences page")

    # Return the state
    get_state = Elixys_Get_State()
    return get_state.state_index()
    def delete_index(s_id, c_id):
        '''
        Function handles a DELETE request from a web request
        in the form of:
        DELETE /Elixys/sequence/<sequenceid>/component/<componentid>
        Function expects two parameters, both integers, that are passed
        in from the web request.
        Function will delete the given sequence's component by it's id
        on the database. Function shall also update the client state
        by moving to the previous component on the "Edit Sequence"
        screen on the client.
        Function shall return the output of GET /Elixys/state.
        (See elixys_web_get.py)
        '''
        auth = request.authorization
        username = str(auth.username)
        client_state = getCurrentClientState(username)
        
        current_app.logger.debug("Handling request: " + str(request))
        
        # Make sure we can edit this sequence
        sequence_metadata = db.GetSequenceMetadata(username, int(s_id))
        if sequence_metadata["sequencetype"] != "Saved":
            raise Exception("Cannot edit sequence")

        # Is the user currently viewing this component?
        if ((client_state["sequenceid"] == int(s_id))
                and (client_state["componentid"] == int(c_id))):
            # Yes, so move the user to the previous component in the sequence
            previous_component = db.get_previous_component(int(c_id))
            if previous_component == None:
                raise Exception("Failed to find previous component")

            # Update the client state
            client_state["componentid"] = previous_component["id"]
            db.update_user_client_state(username, client_state)

        # Delete the sequence component
        sequence_manager.DeleteComponent(username, int(s_id), int(c_id))

        # Return the state
        current_app.logger.debug("Calling GET /state")
        get_state = Elixys_Get_State()
        return get_state.state_index()
    def delete_index(s_id, c_id):
        '''
        Function handles a DELETE request from a web request
        in the form of:
        DELETE /Elixys/sequence/<sequenceid>/component/<componentid>
        Function expects two parameters, both integers, that are passed
        in from the web request.
        Function will delete the given sequence's component by it's id
        on the database. Function shall also update the client state
        by moving to the previous component on the "Edit Sequence"
        screen on the client.
        Function shall return the output of GET /Elixys/state.
        (See elixys_web_get.py)
        '''
        auth = request.authorization
        username = str(auth.username)
        client_state = getCurrentClientState(username)

        current_app.logger.debug("Handling request: " + str(request))

        # Make sure we can edit this sequence
        sequence_metadata = db.GetSequenceMetadata(username, int(s_id))
        if sequence_metadata["sequencetype"] != "Saved":
            raise Exception("Cannot edit sequence")

        # Is the user currently viewing this component?
        if ((client_state["sequenceid"] == int(s_id))
                and (client_state["componentid"] == int(c_id))):
            # Yes, so move the user to the previous component in the sequence
            previous_component = db.get_previous_component(int(c_id))
            if previous_component == None:
                raise Exception("Failed to find previous component")

            # Update the client state
            client_state["componentid"] = previous_component["id"]
            db.update_user_client_state(username, client_state)

        # Delete the sequence component
        sequence_manager.DeleteComponent(username, int(s_id), int(c_id))

        # Return the state
        current_app.logger.debug("Calling GET /state")
        get_state = Elixys_Get_State()
        return get_state.state_index()
def handle_sequence_not_found(client_state, username, sequence_id):
    """Handles the error when the server fails to find a sequence"""
    current_app.logger.debug("Failed to find sequence %s " % str(sequence_id))

    # Was it the sequence that the user is currently on?
    if client_state["sequenceid"] == sequence_id:
        # Yes, so return the user to the last Select Sequence screen
        client_state = direct_to_last_select_screen(client_state)
        db.update_user_client_state(username, client_state)
        current_app.logger.error("Redirecting user to select sequences page")

    # Return the state
    return Elixys_Get_State.state_index()