def editRelationshipForm(model_name):
    
    # ensure model name exists
    if model_name not in getModelNames():
        flash(f"Model '{model_name}' does not exist", "error")
        return redirect(url_for('models'))

    # Ensure there was a POST request
    if request.method != "POST":
        # send error message as a flash message
        flash("Nothing sent in POST", "error")
        return redirect(url_for('dashboard', model_name=model_name))
    
    # load model
    model = UMLModel()
    model.load_model(model_name + ".json", directory=DATA_FOLDER)

    # ensure class1 exists
    if request.form.get('class_name1') not in model.classes:
        return f"Class '{request.form.get('class_name1')}' does not exist"
    # ensure class2 exists
    if request.form.get('class_name2') not in model.classes:
        return f"Class '{request.form.get('class_name2')}' does not exist"

    # grab class data
    data = {}
    data["classes"] = model.get_data()

    # add current relationships data
    data["relationship_type"] = request.form.get('relationship_type')
    data["class_name1"] = request.form.get('class_name1')
    data["class_name2"] = request.form.get('class_name2')

    # Build modal form inputs
    return render_template("editRelationshipForm.html", data=data)
def editForm(model_name):
    
    # ensure model name exists
    if model_name not in getModelNames():
        flash(f"Model '{model_name}' does not exist", "error")
        return redirect(url_for('models'))

    # Ensure there was a POST request
    if request.method != "POST":
        # send error message as a flash message
        flash("Nothing sent in POST", "error")
        return redirect(url_for('dashboard', model_name=model_name))
    
    # load model
    model = UMLModel()
    model.load_model(model_name + ".json", directory=DATA_FOLDER)

    # ensure class exists
    if request.form.get('class_name') not in model.classes:
        return f"Class '{request.form.get('class_name')}' does not exist"

    # grab class data
    data = model.classes[request.form.get('class_name')].get_raw_data()

    # index methods for parameter matching
    for class_name in data:
        i = 0
        for methodi in range(len(data["methods"])):
            data["methods"][methodi]["index"] = i
            i+=1

    # Build modal form inputs
    return render_template("modalForm.html", data=data)
def saveCardPosition(model_name):
    # ensure model name exists
    if model_name not in getModelNames():
        flash(f"Model '{model_name}' does not exist", "error")
        return redirect(url_for('models'))

    # Ensure there was a POST request
    if request.method != "POST":
        # send error message as a flash message
        flash("Nothing sent in POST", "error")
        return redirect(url_for('dashboard', model_name=model_name))

    # load model
    model = UMLModel()
    model.load_model(model_name + ".json", directory=DATA_FOLDER)

    # Grab the position data from the POST request 
    class_data = {
        "filename" : model_name + ".json",
        "directory" : DATA_FOLDER,
        "class_name" : request.form['class_name'],
        "x" : request.form['x'],
        "y": request.form['y'],
        "zindex": request.form['zindex']
    }

    # create command 
    command = SetClassPositonGUICommand(UMLModel(), class_data)
    # save backup
    command.saveBackup()
    # execute command
    response = command.execute()

    # ensure response - this occurs if someone forgot to return a status from a command
    if not response:
        print(f"ERROR: Command did not give a status")
        # send error message as a flash message
        flash("Command did not give a status; This is most likely due to a bug", "error")
        return redirect(url_for('dashboard', model_name=model_name))

    status, msg = response

    # command was not successful 
    if not status:
        # command failed
        print(f"ERROR: {msg}")
        # send error message as a flash message
        flash(msg, "error")
        return redirect(url_for('dashboard', model_name=model_name))

    # add to history
    command_history.push(command)

    # send success message as a flash message
    print(f"SUCCESS: {msg}")
    flash(msg, "success")
    return redirect(url_for('dashboard', model_name=model_name))
def dashboard(model_name):

    # ensure model name exists
    if model_name not in getModelNames():
        flash(f"Model '{model_name}' does not exist", "error")
        return redirect(url_for('models'))

    # Get current model
    model = UMLModel()
    status, msg = model.load_model(model_name + ".json", directory=DATA_FOLDER)

    if not status:
        flash(msg, "error")
        return render_template("dashboard.html", data={})

    # Setup template data
    data = {
        "model_name" : model_name,
        "classes" : []
    }

    # add each class
    i = 0 
    for class_name in model.classes:
        data["classes"] += [model.classes[class_name].get_raw_data()]
        # Give class its index 
        data["classes"][i]["index"] = i+1
        data["classes"][i]["json"] = json.dumps(model.classes[class_name].get_raw_data())
        i += 1

    return render_template("dashboard.html", data=data)
    def test_prompt_exit(self):

        # Test 1: fully exit
        exits = False
        try:
            # give 'no' response for prompt_exit to grab and exit
            sys.stdin = io.StringIO("no\n")
            prompt_exit(UMLModel())
        except SystemExit:
            exits = True
        # make sure it exited
        self.assertTrue(exits)

        # Test 2: cancel exit
        exits = False
        try:
            # give 'cancel' response for prompt_exit
            sys.stdin = io.StringIO("cancel\n")
            status, msg = prompt_exit(UMLModel())
        except SystemExit:
            exits = True
        # make sure it didnt exit
        self.assertFalse(exits)
        self.assertEqual(msg, "Exit aborted")

        # Test 3: save model
        model1 = UMLModel()
        model1.create_class("class1")
        model1.create_class("class2")
        exits = False
        captured = io.StringIO()
        try:
            # give 'yes' response for prompt_exit to grab and exit
            sys.stdin = io.StringIO("yes\ntest_prompt_exit.json\n")
            prompt_exit(model1, os.getcwd() + "/code/data/")
        except SystemExit:
            exits = True
        # make sure it exited
        self.assertTrue(exits)
        # make sure it saved
        model2 = UMLModel()
        model2.load_model("test_prompt_exit.json", os.getcwd() + "/code/data/")
        self.assertEqual(model1.classes.keys(), model2.classes.keys())
示例#6
0
    def test_load_model(self):
        
        expectedData = {
            "class1" : {
                "name" : "class1",
                "fields" : [{
                    "visibility" : "protected",
                    "type" : "int",
                    "name" : "number"
                }],
                "methods" : [],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            },
            "class2" : {
                "name" : "class2",
                "fields" : [],
                "methods" : [{
                    "visibility" : "public",
                    "type" : "string",
                    "name" : "getMsg",
                    "parameters" : [{
                        'type' : 'string',
                        'name' : 'msg'
                    }]
                }],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            },
            "class3" : {
                "name" : "class3",
                "fields" : [],
                "methods" : [],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            }
        }
        json_data = json.dumps(expectedData, indent=4)
        with open("code/server-data/test-load.json", "w") as file:
            file.write(json_data)

        # Test 1: load empty model
        model = UMLModel()
        model.load_model("test-load.json")
        # ensure data was loaded properly
        self.assertEqual(expectedData, model.get_data())

        # Test 2: load invalid data
        json_data = json.dumps({"class1": "toast"}, indent=4)
        with open("code/server-data/test-load.json", "w") as file:
            file.write(json_data)

        model = UMLModel()
        status, msg = model.load_model("test-load.json")
        # ensure data wasn't loaded
        self.assertEqual({}, model.get_data())
        self.assertFalse(status)

        # Test 3: load non-json-parsable file
        non_json_data = "not json data"
        with open("code/server-data/test-load.json", "w") as file:
            file.write(non_json_data)

        model = UMLModel()
        status, msg = model.load_model("test-load.json")
        # ensure data wasn't loaded
        self.assertEqual({}, model.get_data())
        self.assertFalse(status)

        # Test 4: load file that doesn't exist
        model = UMLModel()
        status, msg = model.load_model("iDontExist.json")
        # ensure data wasn't loaded
        self.assertEqual({}, model.get_data())
        self.assertFalse(status)
def upload():
    # Ensure there was a POST request
    if request.method != 'POST':
        # send error message as a flash message
        flash("Nothing sent in POST", "error")
        return redirect(url_for('models'))

    # Ensure POST has a 'file' 
    if 'file' not in request.files:
        # send error message as a flash message
        flash("No file provided", "error")
        return redirect(url_for('models'))
    
    # Ensure filename does not already exist
    if request.form.get("model_name") in getModelNames():
        flash(f"Model '{request.form.get('model_name')}' already exists", "error")
        return redirect(url_for('models'))

    # Grab file
    file = request.files['file']

    # Ensure there was a file submitted
    if file.filename == '':
        # send error message as a flash message
        flash("No file provided", "error")
        return redirect(url_for('models'))
    
    # Ensure file is a json file
    if not is_json(file.filename):
        # send error message as a flash message
        flash("File must be a JSON file", "error")
        return redirect(url_for('models'))
    
    # Save the file to the server as a temp file
    # User's filename is not used which avoids 
    # a security breach that could come from the filename
    file.save(DATA_FOLDER + TEMP_FILENAME)

    # Attempt to load the file with UMLModel 
    # To ensure the file will work as the model
    model = UMLModel()
    try:
        status, msg = model.load_model(TEMP_FILENAME, directory=DATA_FOLDER)
    except:
        # send error message as a flash message
        flash("File cannot be interpretted as a UMLModel", "error")
        return redirect(url_for('models'))

    # load model failed
    if not status:
        flash(msg, "error")
        return redirect(url_for('models'))

    # Save file as the new working model 
    with open(DATA_FOLDER + TEMP_FILENAME, "r") as src:
        with open(DATA_FOLDER + request.form.get("model_name") + ".json", "w") as dest:
            dest.writelines(src.readlines())

    # Redirect to the homepage to display 
    # the newly loaded model
    flash("File was successfully uploaded", "success")
    return redirect(url_for('models'))