Example #1
0
    def view_post_index():
        '''
        Function shall handle POST /VIEW requests from the client.
        Function expects no parameters to be passed in.
        Function shall obtain the action type and target from the
        body sent with the POST web request. The function shall
        determine whether to call the save client state and return
        function, to call the show run sequence prompt function, or
        to call the show run from component prompt funciton.
        Function shall return the output of one of the functions
        based on the body sent.
        '''
        # Get objects

        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(username)
        client_state = getCurrentClientState(username)
        # Get POST object
        # body = ... obtain POST object sent
        body = request.data
        current_app.logger.debug("POST REQUEST:" + str(request) + \
                "\nBody Sent: " + \
                str(request.data))

        # Make sure we are on View Sequence
        if client_state["screen"] != "VIEW":
            raise Exceptions.StateMisalignmentException()

        # Parse the JSON string in the body and extract the action type and target
        body_JSON = json.loads(body)
        action_type = str(body_JSON["action"]["type"])
        action_target_id = str(body_JSON["action"]["targetid"])

        # Call the base sequence POST handler first
        if handle_post_base_sequence(client_state, username,
                action_type, action_target_id):
            return save_client_state_and_return(
                    client_state, username)

        # Handle View Sequence specific requests
        if action_type == "BUTTONCLICK":
            if action_target_id == "EDITSEQUENCE":
                # Switch states to Edit Sequence
                client_state["screen"] = "EDIT"
                return save_client_state_and_return(
                        client_state, username)
            elif action_target_id == "RUNSEQUENCE":
                # Show the Run Sequence prompt
                return show_run_sequence_prompt(client_state,
                        username, client_state["sequenceid"])
            elif action_target_id == "RUNSEQUENCEHERE":
                # Show the Run Sequence From Component prompt
                return show_run_sequence_from_component_prompt(
                        client_state, username,
                        client_state["sequenceid"],
                        client_state["componentid"])
        # Unhandled use case
        raise Exceptions.StateMisalignmentException()
Example #2
0
    def sequence_index(sequence_id):
        '''
        Funciton shall take in a sequence id as a 
        parameter.
        Function shall return the sequence object
        as a jSON object.
        Function shall try to obtain the sequence based
        on the id or try to handle the unknown sequence id.
        All components shall be added to the return object
        as a part of a sequence's components.
        '''

        current_app.logger.debug(str(request))
        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(str(username))
        client_state = getCurrentClientState(str(username))
        current_app.logger.debug(str(request) + \
                "\nClient state: " + str(client_state) + \
                "\nServer state: " + str(server_state))
        sequence = None
        # Load the sequence
        try:
            sequence = sequence_manager.GetSequence(username, int(sequence_id),
                                                    False)
        except Exceptions.SequenceNotFoundException:
            return handle_sequence_not_found(client_state, username,
                                             int(sequence_id))

        #current_app.logger.debug("In /sequence function, client state: " + \
        #        str(client_state) + \
        #        "\nSequence found: " + str(sequence))

        # Copy a subset of the sequence data
        new_components = []
        for old_comp in sequence["components"]:
            new_comp = {
                "type": "sequencecomponent",
                "note": old_comp["note"],
                "id": old_comp["id"],
                "componenttype": old_comp["componenttype"],
                "validationerror": False
            }
            if old_comp.has_key("validationerror"):
                new_comp["validationerror"] = old_comp["validationerror"]
            new_components.append(new_comp)
        sequence["components"] = new_components

        sequence.update({"type": "sequence"})
        # Return cleaned sequence
        current_app.logger.debug("In /sequence function, new sequence: " + \
                str(sequence))
        return jsonify(sequence)
Example #3
0
    def home_post_index():
        '''
        Function shall handle POST /HOME requests from the client.
        Function expects no parameters to be passed in.
        Function shall return the output of save client
        and return function.
        Function checks the body sent with the POST request
        and obtains the action type and target, then handles
        what should be updated on the client state variable
        before returning via save client state and return.
        '''
        # Get objects

        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(username)
        client_state = getCurrentClientState(username)
        # Get POST object
        # body = ... obtain POST object sent
        body = request.data
        current_app.logger.debug("POST REQUEST:" + str(request) + \
                 "\nBody Sent: " + str(request.data) + \
                 "\nClient State: " + str(client_state) + \
                 "\nServer State: " + str(server_state))

        # Make sure we are on the home page
        if client_state["screen"] != "HOME":
            raise Exceptions.StateMisalignmentException()

        # Parse the JSON string in the body
        JSON_body = json.loads(body)

        # Check which option the user selected
        action_type = str(JSON_body["action"]["type"])
        action_target_id = str(JSON_body["action"]["targetid"])
        if action_type == "BUTTONCLICK":
            if action_target_id == "SEQUENCER":
                # Switch states to the last Select Sequence screen
                client_state = direct_to_last_select_screen(client_state)
                return save_client_state_and_return(
                        client_state, username)
            elif action_target_id == "VIEWRUN":
                # Switch to Run Sequence
                client_state["screen"] = "RUN"
                client_state["sequenceid"] = (
                        server_state["runstate"]["sequenceid"])
                client_state["componentid"] = (
                        server_state["runstate"]["componentid"])
                return save_client_state_and_return(
                        client_state, username)
        raise Exceptions.StateMisalignmentException()
Example #4
0
    def sequence_index(sequence_id):
        """
        Funciton shall take in a sequence id as a 
        parameter.
        Function shall return the sequence object
        as a jSON object.
        Function shall try to obtain the sequence based
        on the id or try to handle the unknown sequence id.
        All components shall be added to the return object
        as a part of a sequence's components.
        """

        current_app.logger.debug(str(request))
        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(str(username))
        client_state = getCurrentClientState(str(username))
        current_app.logger.debug(
            str(request) + "\nClient state: " + str(client_state) + "\nServer state: " + str(server_state)
        )
        sequence = None
        # Load the sequence
        try:
            sequence = sequence_manager.GetSequence(username, int(sequence_id), False)
        except Exceptions.SequenceNotFoundException:
            return handle_sequence_not_found(client_state, username, int(sequence_id))

        # current_app.logger.debug("In /sequence function, client state: " + \
        #        str(client_state) + \
        #        "\nSequence found: " + str(sequence))

        # Copy a subset of the sequence data
        new_components = []
        for old_comp in sequence["components"]:
            new_comp = {
                "type": "sequencecomponent",
                "note": old_comp["note"],
                "id": old_comp["id"],
                "componenttype": old_comp["componenttype"],
                "validationerror": False,
            }
            if old_comp.has_key("validationerror"):
                new_comp["validationerror"] = old_comp["validationerror"]
            new_components.append(new_comp)
        sequence["components"] = new_components

        sequence.update({"type": "sequence"})
        # Return cleaned sequence
        current_app.logger.debug("In /sequence function, new sequence: " + str(sequence))
        return jsonify(sequence)
Example #5
0
    def component_index(sequence_id, component_id):
        """
        Function handles GET request for
        /Elixys/sequence/<sequenceid>/component/<componentid>.
        Function expects a sequence id and a component id to be
        passed in through the web request.
        Function shall either return a component as a JSON object
        or try to handle an exception.
        If the sequence id from the component found on the database 
        is the same as thesequence id passed in, the function returns. 
        If the sequence ids do not match, function calls a helper method
        to try to find the matching component.
        """
        auth = request.authorization
        username = str(auth.username)
        client_state = getCurrentClientState(username)
        server_state = get_server_state(str(username))
        current_app.logger.debug(
            "In /sequence/s_id/component/c_id"
            + "\nRequest for %s" % str(request)
            + "\nClient state: "
            + str(client_state)
            + "\nServer state: "
            + str(server_state)
        )

        # Handle GET /sequence/[sequenceid]/component/[componentid]
        # Get the component and verify the sequence ID
        try:
            component = sequence_manager.GetComponent(username, int(component_id), int(sequence_id))
        except Exceptions.ComponentNotFoundException:
            return handle_component_not_found(client_state, username, int(component_id))

        current_app.logger.debug(
            "\nSequence id: "
            + str(component["sequenceid"])
            + "\nPassed in sequence id: "
            + str(sequence_id)
            + "\nPassed in compo id : "
            + str(component_id)
            + "\nComponent JSON: "
            + str(component)
        )
        if int(component["sequenceid"]) == int(sequence_id):
            return jsonify(component)
        else:
            return handle_component_not_found(client_state, username, int(component_id))
    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 #7
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()
Example #8
0
    def component_index(sequence_id, component_id):
        '''
        Function handles GET request for
        /Elixys/sequence/<sequenceid>/component/<componentid>.
        Function expects a sequence id and a component id to be
        passed in through the web request.
        Function shall either return a component as a JSON object
        or try to handle an exception.
        If the sequence id from the component found on the database 
        is the same as thesequence id passed in, the function returns. 
        If the sequence ids do not match, function calls a helper method
        to try to find the matching component.
        '''
        auth = request.authorization
        username = str(auth.username)
        client_state = getCurrentClientState(username)
        server_state = get_server_state(str(username))
        current_app.logger.debug("In /sequence/s_id/component/c_id" +
                "\nRequest for %s" % str(request) + \
                "\nClient state: " + str(client_state) + \
                "\nServer state: " + str(server_state))

        # Handle GET /sequence/[sequenceid]/component/[componentid]
        # Get the component and verify the sequence ID
        try:
            component = sequence_manager.GetComponent(username,
                                                      int(component_id),
                                                      int(sequence_id))
        except Exceptions.ComponentNotFoundException:
            return handle_component_not_found(client_state, username,
                                              int(component_id))

        current_app.logger.debug(
                "\nSequence id: " + str(component['sequenceid']) + \
                "\nPassed in sequence id: " + str(sequence_id) + \
                "\nPassed in compo id : " + str(component_id) + \
                "\nComponent JSON: " + str(component))
        if int(component["sequenceid"]) == int(sequence_id):
            return jsonify(component)
        else:
            return handle_component_not_found(client_state, username,
                                              int(component_id))
Example #9
0
def handle_sequence_not_found(client_state, username, sequence_id):
    """
    Handles the error when the server fails to find a sequence.
    Function expects a client state object, a username, and the
    sequence id that raises the exception.
    Function shall return the state from GET /Elixys/state.
    """
    client_state = getCurrentClientState(username)

    current_app.logger.debug("Failed to find sequence: " + str(sequence_id) + "Client state: " + str(client_state))

    # Was it the sequence that the user is currently on?
    if client_state["sequenceid"] == int(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
    elixys_get_state = Elixys_Get_State()
    return elixys_get_state.state_index()
Example #10
0
    def runstate_index():
        '''
        Function shall handle GET ../runstate.
        Function expects no paramaters to be passed in.
        Function shall return the state as a JSON object.
        This state will contain components from the client
        state and the server state.
        '''

        # Handle GET /runstate request
        # Get the user information and server state
        current_app.logger.debug(str(request))

        auth = request.authorization
        user = db.get_user(str(auth.username))
        server_state = core_server.GetServerState(user)
        client_state = getCurrentClientState(auth.username)

        # Start the state object
        return_state = {
            "type": "state",
            "user": user,
            "serverstate": server_state,
            "clientstate": copy.deepcopy(client_state),
            "timestamp": time.time()
        }

        # Complete with either the run or home states
        if server_state["runstate"]["running"]:
            return_state.update(
                handle_get_state_run(client_state, str(auth.username)))
            return_state["clientstate"]["screen"] = "RUN"
        else:
            return_state.update(
                handle_get_state_run(client_state, str(auth.username)))
            return_state["clientstate"]["screen"] = "HOME"
            return_state["clientstate"]["prompt"]["show"] = False

        # Return the state
        return jsonify(return_state)
Example #11
0
    def reagent_post_index(s_id, r_id):
        '''
        Function shall handle all POST .../reagent/<reagent_id>
        web requests.
        Function expects integers sequence id and reagent id to
        be passed in as parameters.
        Function returns the output of save client state and
        return.
        The function obtains the data of the body sent with
        the POST request and updates the reagent on the database
        before calling save client state and return.
        '''
        # Get objects

        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(username)
        client_state = getCurrentClientState(username)
        # Get POST object
        body = request.data
        current_app.logger.debug("Body sent: " + str(body))

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

        # Save the reagent
        reagent = json.loads(body)
        db.update_reagent(
                int(r_id),
                reagent["name"],
                reagent["description"])

        # Flag the sequence validation as dirty
        db.update_sequence_dirty_flag(int(s_id), True)

        # Return the new state
        return save_client_state_and_return(
                client_state, username)
Example #12
0
def handle_sequence_not_found(client_state, username, sequence_id):
    """
    Handles the error when the server fails to find a sequence.
    Function expects a client state object, a username, and the
    sequence id that raises the exception.
    Function shall return the state from GET /Elixys/state.
    """
    client_state = getCurrentClientState(username)

    current_app.logger.debug("Failed to find sequence: " + str(sequence_id) + \
            "Client state: " + str(client_state))

    # Was it the sequence that the user is currently on?
    if client_state["sequenceid"] == int(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
    elixys_get_state = Elixys_Get_State()
    return elixys_get_state.state_index()
Example #13
0
    def runstate_index():
        """
        Function shall handle GET ../runstate.
        Function expects no paramaters to be passed in.
        Function shall return the state as a JSON object.
        This state will contain components from the client
        state and the server state.
        """

        # Handle GET /runstate request
        # Get the user information and server state
        current_app.logger.debug(str(request))

        auth = request.authorization
        user = db.get_user(str(auth.username))
        server_state = core_server.GetServerState(user)
        client_state = getCurrentClientState(auth.username)

        # Start the state object
        return_state = {
            "type": "state",
            "user": user,
            "serverstate": server_state,
            "clientstate": copy.deepcopy(client_state),
            "timestamp": time.time(),
        }

        # Complete with either the run or home states
        if server_state["runstate"]["running"]:
            return_state.update(handle_get_state_run(client_state, str(auth.username)))
            return_state["clientstate"]["screen"] = "RUN"
        else:
            return_state.update(handle_get_state_run(client_state, str(auth.username)))
            return_state["clientstate"]["screen"] = "HOME"
            return_state["clientstate"]["prompt"]["show"] = False

        # Return the state
        return jsonify(return_state)
Example #14
0
    def state_index(self=None):
        """
        This function requires authentication prior to executing.
        Function takes in no parameters.
        Function shall direct the client based on the client's
        current screen.
        Function returns the state to the client as a JSON object.
        """

        auth = request.authorization
        user = db.get_user(str(auth.username))
        server_state = get_server_state(str(auth.username))
        client_state = getCurrentClientState(str(auth.username))
        current_app.logger.debug(
            str(request) + "\nClientstate: " + str(client_state) + "\nServerstate: " + str(server_state)
        )
        # Is the remote user the one that is currently running the system?
        if str(auth.username) == server_state["runstate"]["username"]:
            # Yes, so make sure the user is on the run screen
            if client_state["screen"] != "RUN":
                # Update the client state
                client_state["screen"] = "RUN"
                db.update_user_client_state(str(auth.username), client_state)

        return_state = {
            "type": "state",
            "user": user,
            "serverstate": server_state,
            "clientstate": client_state,
            "timestamp": time.time(),
        }
        # Check which screen is active
        choice = str(client_state["screen"])

        current_app.logger.debug(
            "Request for state"
            + "\nREQUEST: "
            + str(request)
            + "\nClient state: "
            + str(client_state)
            + "\nServer state: "
            + str(server_state)
        )

        # current_app.logger.debug("Client State: "+ str(client_state) + \
        #        "\nChoice from client_state[screen]: " +str(choice))

        if choice == "HOME":
            return_state.update(handle_get_state_home(auth.username))
        elif choice == "SELECT_SAVEDSEQUENCES":
            return_state.update(handle_get_state_select_savedsequences(client_state, auth.username))
        elif choice == "SELECT_RUNHISTORY":
            return_state.update(handle_get_state_select_runhistory(client_state, auth.username))
        elif choice == "VIEW":
            return_state.update(handle_get_state_view(client_state, auth.username))
        elif choice == "EDIT":
            return_state.update(handle_get_state_edit(client_state, auth.username))
        elif choice == "RUN":
            return_state.update(handle_get_state_run(client_state, auth.username))
        else:
            current_app.logger.debug("Unknown screen: %s" % str(choice))
            raise Exception("Unknown screen: %s" % str(choice))
        # current_app.logger.debug("Return state: " + str(return_state))
        return jsonify(return_state)
Example #15
0
    def state_index(self=None):
        '''
        This function requires authentication prior to executing.
        Function takes in no parameters.
        Function shall direct the client based on the client's
        current screen.
        Function returns the state to the client as a JSON object.
        '''

        auth = request.authorization
        user = db.get_user(str(auth.username))
        server_state = get_server_state(str(auth.username))
        client_state = getCurrentClientState(str(auth.username))
        current_app.logger.debug(str(request) + "\nClientstate: " + \
                str(client_state) + "\nServerstate: " + str(server_state))
        # Is the remote user the one that is currently running the system?
        if str(auth.username) == server_state["runstate"]["username"]:
            # Yes, so make sure the user is on the run screen
            if client_state["screen"] != "RUN":
                # Update the client state
                client_state["screen"] = "RUN"
                db.update_user_client_state(str(auth.username), client_state)

        return_state = {
            "type": "state",
            "user": user,
            "serverstate": server_state,
            "clientstate": client_state,
            "timestamp": time.time()
        }
        # Check which screen is active
        choice = str(client_state["screen"])

        current_app.logger.debug("Request for state" +
                "\nREQUEST: " + str(request) + \
                "\nClient state: " + str(client_state) + \
                "\nServer state: " + str(server_state))

        #current_app.logger.debug("Client State: "+ str(client_state) + \
        #        "\nChoice from client_state[screen]: " +str(choice))

        if choice == "HOME":
            return_state.update(handle_get_state_home(auth.username))
        elif choice == "SELECT_SAVEDSEQUENCES":
            return_state.update(
                handle_get_state_select_savedsequences(client_state,
                                                       auth.username))
        elif choice == "SELECT_RUNHISTORY":
            return_state.update(
                handle_get_state_select_runhistory(client_state,
                                                   auth.username))
        elif choice == "VIEW":
            return_state.update(
                handle_get_state_view(client_state, auth.username))
        elif choice == "EDIT":
            return_state.update(
                handle_get_state_edit(client_state, auth.username))
        elif choice == "RUN":
            return_state.update(
                handle_get_state_run(client_state, auth.username))
        else:
            current_app.logger.debug("Unknown screen: %s" % str(choice))
            raise Exception("Unknown screen: %s" % str(choice))
        #current_app.logger.debug("Return state: " + str(return_state))
        return jsonify(return_state)
Example #16
0
    def prompt_post_index():
        '''
        Function shall handle POST /VIEW requests from the client.
        Function expects no parameters to be passed in.
        Function returns the output of the save client and
        return function.
        '''
        # Get objects

        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(username)
        client_state = getCurrentClientState(username)
        # Get POST object
        # body = ... obtain POST object sent
        body = request.data
        current_app.logger.debug("POST REQUEST:" + str(request) + \
                "\nBody Sent: " + str(request.data) + \
                "\nclientstate: " +  str(client_state) + \
                "\nserverstate: " + str(server_state))

        if (not client_state["prompt"]["show"]
                or not client_state["prompt"]["screen"].startswith("PROMPT")):
            current_app.logger.error("Client[prompt][show] is False, raising " +
                    "State Misalignment Exception")
            raise Exceptions.StateMisalignmentException()

        # Parse the JSON string in the body
        JSON_body = json.loads(body)

        # Extract the post parameters
        action_type = str(JSON_body["action"]["type"])
        action_target_id = str(JSON_body["action"]["targetid"])
        edit1 = str(JSON_body["edit1"])
        edit2 = str(JSON_body["edit2"])

        # The only recognized action from a prompt is a button click
        if action_type != "BUTTONCLICK":
            raise Exceptions.StateMisalignmentException()
        # Interpret the response in context of the client state
        if client_state["prompt"]["screen"] == "PROMPT_CREATESEQUENCE":
            if action_target_id == "OK":
                # Sequence name is required
                if edit1 == "":
                    raise Exception("Sequence name is required")

                # Create the new sequence
                configuration = db.get_configuration()
                sequence_id = db.create_sequence(
                        edit1, username, edit2,
                        "Saved", configuration["reactors"],
                        configuration["reagentsperreactor"])

                # Hide the prompt and move the client to the editing the new sequence
                client_state["prompt"]["show"] = False
                client_state["screen"] = "EDIT"
                client_state["sequenceid"] = sequence_id
                current_app.logger.debug("Created a new sequence, calling" +
                        "save_client...from POST /PROMPT" + \
                        "\nSequence id returned : " + str(sequence_id) + \
                        "\nClient State: " + str(client_state))
                return save_client_state_and_return(
                        client_state, username)
            if action_target_id == "CANCEL":
                # Hide the prompt
                client_state["prompt"]["show"] = False
                return save_client_state_and_return(
                        client_state, username)
        elif client_state["prompt"]["screen"] == "PROMPT_COPYSEQUENCE":
            if action_target_id == "OK":
                # Sequence name is required
                if edit1 == "":
                    raise Exception("Sequence name is required")

                # create a copy of the sequence in the database
                new_sequence_id = sequence_manager.CopySequence(
                        username,
                        int(client_state["sequenceid"]),
                        str(edit1),
                        str(edit2))

                # Hide the prompt and move the client to the saved sequences screen
                client_state["prompt"]["show"] = False
                client_state["screen"] = "SELECT_SAVEDSEQUENCES"
                client_state["lastselectscreen"] = "SAVED"
                return save_client_state_and_return(
                        client_state, username)
            if action_target_id == "CANCEL":
                # Hide the prompt
                client_state["prompt"]["show"] = False
                return save_client_state_and_return(
                        client_state, username)
        elif client_state["prompt"]["screen"] == "PROMPT_DELETESEQUENCE":
            if action_target_id == "YES":
                # Delete the sequence from the database
                db.delete_sequence(client_state["sequenceid"])

                # Hide the prompt
                client_state["prompt"]["show"] = False
                return save_client_state_and_return(
                        client_state, username)
            if action_target_id == "NO":
                # Hide the prompt
                client_state["prompt"]["show"] = False
                return save_client_state_and_return(
                        client_state, username)
        elif client_state["prompt"]["screen"] == "PROMPT_RUNSEQUENCE":
            if action_target_id == "YES":
                # Fetch the sequence from the database and make sure it is valid
                sequence = sequence_manager.GetSequence(
                        username, client_state["sequenceid"])
                if not sequence["metadata"]["valid"]:
                    raise Exceptions.InvalidSequenceException(
                            str(client_state['sequenceid']))
                # Run the sequence
                core_server.RunSequence(username, client_state["sequenceid"])

                # Hide the prompt and switch states to Run Sequence
                client_state["prompt"]["show"] = False
                client_state["screen"] = "RUN"

                current_app.logger.debug("From POST /PROMPT, calling save client")

                return save_client_state_and_return(
                        client_state, username)
            if action_target_id == "NO":
                # Hide the prompt
                client_state["prompt"]["show"] = False
                return save_client_state_and_return(
                        client_state, username)
        elif client_state["prompt"]["screen"] == "PROMPT_RUNSEQUENCEFROMCOMPONENT":
            if action_target_id == "YES":
                # Fetch the sequence from the database and make sure it is valid
                sequence = sequence_manager.GetSequence(
                        username,
                        int(client_state["sequenceid"]))
                if not sequence["metadata"]["valid"]:
                    raise Exceptions.InvalidSequenceException(client_state['sequenceid'])

                # Run the sequence from the component
                core_server.RunSequenceFromComponent(username,
                        client_state["sequenceid"],
                        client_state["componentid"])

                # Hide the prompt and switch states to Run Sequence
                client_state["prompt"]["show"] = False
                client_state["screen"] = "RUN"
                return save_client_state_and_return(
                        client_state, username)
            if action_target_id == "NO":
                # Hide the prompt
                client_state["prompt"]["show"] = False
                return save_client_state_and_return(
                        client_state, username)
        elif client_state["prompt"]["screen"] == "PROMPT_UNITOPERATION":
            # Currently unused
            return save_client_state_and_return(
                        client_state, username)
        elif client_state["prompt"]["screen"] == "PROMPT_SOFTERROR":
            # Pass the user's decision to the core server
            core_server.SetSoftErrorDecision(username, action_target_id)
        elif client_state["prompt"]["screen"] == "PROMPT_ABORTRUN":
            if action_target_id == "YES":
                # Abort the run and return to the home page
                core_server.AbortSequence(username)
                client_state["prompt"]["show"] = False
                client_state["screen"] = "HOME"
                return save_client_state_and_return(
                        client_state, username)
            if action_target_id == "NO":
                # Hide the prompt
                core_server.ShowAbortSequencePrompt(username, False)
                return save_client_state_and_return(
                        client_state, username)
        # Unhandled use case
        raise Exceptions.StateMisalignmentException()
Example #17
0
    def select_post_index():
        '''
        Function shall handle POST /SELECT requests from the client.
        Function expects no parameters to be passed in.
        Function checks what the action type and target from the
        body sent with the POST web request.
        Function returns either the output of the save client state
        and return function or returns the output of the prompt handler
        for a sequence run.
        '''
        # Get objects

        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(username)
        client_state = getCurrentClientState(username)
        # Get POST object
        # body = ... obtain POST object sent
        body = request.data
        current_app.logger.debug("POST REQUEST:" + str(request) + \
                "\nBody Sent: " + \
                str(request.data))

        # Make sure we are on Select Sequence
        if not client_state["screen"].startswith("SELECT"):
            raise Exceptions.StateMisalignmentException()

        # Parse the JSON string in the body
        body_JSON = json.loads(body)

        # Check which option the user selected
        action_type = str(body_JSON["action"]["type"])
        action_target_id = str(body_JSON["action"]["targetid"])
        sequence_id = body_JSON["sequenceid"]
        if action_type == "BUTTONCLICK":
            if action_target_id == "SEQUENCER":
                # Switch states to Home
                client_state["screen"] = "HOME"
                return save_client_state_and_return(
                    client_state, username)
            elif action_target_id == "VIEWSEQUENCE":
                # Switch states to View Sequence
                client_state["screen"] = "VIEW"
                client_state["sequenceid"] = sequence_id
                client_state["componentid"] = 0
                return save_client_state_and_return(
                    client_state, username)
            elif action_target_id == "EDITSEQUENCE":
                # Switch states to Edit Sequence
                client_state["screen"] = "EDIT"
                client_state["sequenceid"] = sequence_id
                client_state["componentid"] = 0
                return save_client_state_and_return(
                    client_state, username)
            elif action_target_id == "RUNSEQUENCE":
                # Show the Run Sequence prompt
                return show_run_sequence_prompt(
                        client_state, username, sequence_id)
            elif action_target_id == "NEWSEQUENCE":
                # Show the Create Sequence prompt
                client_state["prompt"]["screen"] = "PROMPT_CREATESEQUENCE"
                client_state["prompt"]["show"] = True
                client_state["prompt"]["title"] = "NEW SEQUENCE"
                client_state["prompt"]["text1"] = "SEQUENCE NAME"
                client_state["prompt"]["edit1"] = True
                client_state["prompt"]["edit1default"] = ""
                client_state["prompt"]["edit1validation"] = \
                        "type=string; required=true"
                client_state["prompt"]["text2"] = "SEQUENCE DESCRIPTION"
                client_state["prompt"]["edit2"] = True
                client_state["prompt"]["edit2validation"] = \
                        "type=string; required=false"
                client_state["prompt"]["edit2default"] = ""
                client_state["prompt"]["buttons"] = [{"type":"button",
                    "text":"OK",
                    "id":"OK"},
                    {"type":"button",
                    "text":"CANCEL",
                    "id":"CANCEL"}]
                return save_client_state_and_return(
                        client_state, username)
            elif action_target_id == "COPYSEQUENCE":
                # Show the Copy Sequence prompt
                sequence = sequence_manager.GetSequence(
                        username, sequence_id, False)
                client_state["prompt"]["screen"] = "PROMPT_COPYSEQUENCE"
                client_state["prompt"]["show"] = True
                client_state["prompt"]["title"] = "COPY SEQUENCE"
                client_state["prompt"]["text1"] = "SEQUENCE NAME"
                client_state["prompt"]["edit1"] = True
                client_state["prompt"]["edit1default"] = \
                        sequence["metadata"]["name"] + " Copy"
                client_state["prompt"]["edit1validation"] = \
                        "type=string; required=true"
                client_state["prompt"]["text2"] = "SEQUENCE DESCRIPTION"
                client_state["prompt"]["edit2"] = True
                client_state["prompt"]["edit2default"] = \
                        sequence["metadata"]["comment"]
                client_state["prompt"]["edit2validation"] = \
                        "type=string; required=false"
                client_state["prompt"]["buttons"] = [{"type":"button",
                    "text":"OK",
                    "id":"OK"},
                    {"type":"button",
                    "text":"CANCEL",
                    "id":"CANCEL"}]
                client_state["sequenceid"] = sequence_id
                return save_client_state_and_return(
                        client_state, username)
            elif action_target_id == "DELETESEQUENCE":
                # Show the Delete Sequence prompt
                sequence = sequence_manager.GetSequence(
                        username, sequence_id, False)
                client_state["prompt"]["screen"] = "PROMPT_DELETESEQUENCE"
                client_state["prompt"]["show"] = True
                client_state["prompt"]["title"] = "DELETE SEQUENCE"
                client_state["prompt"]["text1"] = \
                        "Are you sure that you want to " + \
                        "delete the sequence \"" + \
                        sequence["metadata"]["name"] + "\"?"
                client_state["prompt"]["edit1"] = False
                client_state["prompt"]["text2"] = ""
                client_state["prompt"]["edit2"] = False
                client_state["prompt"]["buttons"] = [{"type":"button",
                    "text":"YES",
                    "id":"YES"},
                    {"type":"button",
                    "text":"NO",
                    "id":"NO"}]
                client_state["sequenceid"] = sequence_id
                return save_client_state_and_return(
                        client_state, username)
        elif action_type == "TABCLICK":
            if action_target_id == "SAVEDSEQUENCES":
                # Switch states to the Saved Sequences tab
                client_state["screen"] = "SELECT_SAVEDSEQUENCES"
                client_state["lastselectscreen"] = "SAVED"
                client_state["sequenceid"] = sequence_id
                return save_client_state_and_return(
                        client_state, username)
            elif action_target_id == "RUNHISTORY":
                # Switch states to the Run History tab
                client_state["screen"] = "SELECT_RUNHISTORY"
                client_state["lastselectscreen"] = "HISTORY"
                client_state["sequenceid"] = sequence_id
                return save_client_state_and_return(
                        client_state, username)
        elif action_type == "HEADERCLICK":
            if client_state["screen"] == "SELECT_SAVEDSEQUENCES":
            # Change the select sequences sort order
                if action_target_id == "name":
                    if client_state["selectsequencesort"]["column"] == "name":
                        if client_state["selectsequencesort"]["mode"] == "down":
                            client_state["selectsequencesort"]["mode"] = "up"
                        else:
                            client_state["selectsequencesort"]["mode"] = "down"
                    else:
                        client_state["selectsequencesort"]["column"] = "name"
                        client_state["selectsequencesort"]["mode"] = "down"
                    return save_client_state_and_return(
                            client_state, username)
                elif action_target_id == "comment":
                    if client_state["selectsequencesort"]["column"] == "comment":
                        if client_state["selectsequencesort"]["mode"] == "down":
                            client_state["selectsequencesort"]["mode"] = "up"
                        else:
                            client_state["selectsequencesort"]["mode"] = "down"
                    else:
                        client_state["selectsequencesort"]["column"] = "comment"
                        client_state["selectsequencesort"]["mode"] = "down"
                    return save_client_state_and_return(
                            client_state, username)
                elif client_state["screen"] == "SELECT_RUNHISTORY":
                    # Change the run history sort order
                    if action_target_id == "name":
                        if client_state["runhistorysort"]["column"] == "name":
                            if client_state["runhistorysort"]["mode"] == "down":
                                client_state["runhistorysort"]["mode"] = "up"
                            else:
                                client_state["runhistorysort"]["mode"] = "down"
                        else:
                            client_state["runhistorysort"]["column"] = "name"
                            client_state["runhistorysort"]["mode"] = "down"
                        return save_client_state_and_return(
                                client_state, username)
                    elif action_target_id == "comment":
                        if client_state["runhistorysort"]["column"] == "comment":
                            if client_state["runhistorysort"]["mode"] == "down":
                                client_state["runhistorysort"]["mode"] = "up"
                            else:
                                client_state["runhistorysort"]["mode"] = "down"
                        else:
                            client_state["runhistorysort"]["column"] = "comment"
                            client_state["runhistorysort"]["mode"] = "down"
                        return save_client_state_and_return(
                                client_state, username)
                    elif action_target_id == "creator":
                        if client_state["runhistorysort"]["column"] == "creator":
                            if client_state["runhistorysort"]["mode"] == "down":
                                client_state["runhistorysort"]["mode"] = "up"
                            else:
                                client_state["runhistorysort"]["mode"] = "down"
                        else:
                            client_state["runhistorysort"]["column"] = "creator"
                            client_state["runhistorysort"]["mode"] = "down"
                        return save_client_state_and_return(
                                client_state, username)
                    elif action_target_id == "date&time":
                        if client_state["runhistorysort"]["column"] == "date&time":
                            if client_state["runhistorysort"]["mode"] == "down":
                                client_state["runhistorysort"]["mode"] = "up"
                            else:
                                client_state["runhistorysort"]["mode"] = "down"
                        else:
                            client_state["runhistorysort"]["column"] = "date&time"
                            client_state["runhistorysort"]["mode"] = "down"
                        return save_client_state_and_return(
                                client_state, username)
        # Unhandled use case
        raise Exceptions.StateMisalignmentException()
Example #18
0
    def component_post_index(s_id, c_id, unit=None):
        '''
        This function handles all POST for components from the
        client.
        Function expects an integer sequence_id, an integer component_id,
        and an optional parameter, insertion id.
        Function shall return the output of save client and return.
        Based on the body sent with the POST request, this function
        shall use sequence_manager to update the component passed in.
        '''

        auth = request.authorization
        username = str(auth.username)
        server_state = get_server_state(username)
        client_state = getCurrentClientState(username)
        # Get POST object
        body = request.data

        insertion_id = None
        if unit != None:
            insertion_id = int(unit)

        current_app.logger.debug('POST /sequence/s_id/component/c_id' +
                "\nRequest: " + str(request) + \
                "\nBody recieved: " + str(body) + \
                "\nInsertion ID: " + str(insertion_id))
        sequence_metdata = db.get_sequence_metadata(int(s_id))
        if sequence_metdata['sequencetype'] != 'Saved':
            raise Exception("Cannot edit sequence!")
        component = None

        if len(body) != 0:
            component = json.loads(body)
        if int(c_id) != 0:
            current_app.logger.debug("Updating component")
            # Check if insertion_id is none
            if insertion_id != None:
                sequence_manager.UpdateComponent(
                        username, int(s_id), int(c_id),
                        int(insertion_id), component)
            else:
                sequence_manager.UpdateComponent(
                        username, int(s_id), int(c_id),
                        None, component)

        else:
            # Check if insertion_id is None
            if insertion_id != None:
                c_id = sequence_manager.AddComponent(
                        username, int(s_id),
                        int(insertion_id), component)
                current_app.logger.debug("Adding component: " + \
                        str(c_id))
                client_state["componentid"] = c_id
            else :
                c_id = sequence_manager.AddComponent(
                        username, int(s_id),
                        None, component)
                current_app.logger.debug("Adding component: " + \
                        str(c_id))
                client_state["componentid"] = c_id

        return save_client_state_and_return(
                client_state, username)