Example #1
0
def handle_post_base_sequence(client_state,
        username, action_type, action_target_id):
    '''
    Function expects a client state, a string username, a string
    action type, and a string action_target_id.
    Function shall return True if action type and
    action_target_id are valid or function shall return
    False.
    '''
    # Check which option the user selected
    if action_type == "BUTTONCLICK":
        if action_target_id == "SEQUENCER":
            # Switch states to the last Select Sequence screen
            direct_to_last_select_screen(client_state)
            return True
        elif action_target_id == "PREVIOUS":
            # Move to the previous component
            previous_component = db.get_previous_component(
                    client_state["componentid"])
            if previous_component != None:
                client_state["componentid"] = previous_component["id"]
            return True
        elif action_target_id == "NEXT":
            # Move to the next component
            next_component = db.get_next_component(
                    client_state["componentid"])
            if next_component != None:
                client_state["componentid"] = next_component["id"]
            return True
        else:
            # Check if the target ID corresponds to
            # one of our sequence components
            try:
                # Cast the action target ID to an integer
                # and fetch the corresponding component
                action_target_id = int(action_target_id)
                component = db.get_component(action_target_id)

                # Make sure the sequence IDs match
                if component["sequenceid"] != client_state["sequenceid"]:
                    return False

                # Move to the component
                client_state["componentid"] = component["id"]
                return True
            except ValueError:
                # Action target ID is not an integer
                pass
            except Exception("Component Not Found Exception"):
                # Interger does not correspond to a component ID
                pass

    # Tell the caller we didn't handle it
    return False
    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()
Example #3
0
    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()