Пример #1
0
def update_graphic_json_config_with_ui_changes():
    config_information_dict = request.get_json()
    page_id = config_information_dict[PAGE_ID]
    graphic_dict = graphic_component_dict_to_graphic_dict(
        config_information_dict[CONFIG_DICT])
    graphic_filename = config_information_dict[GRAPHIC_PATH]
    # sanitizing the string so it is valid filename
    if config_information_dict[GRAPHIC_STATUS] in [NEW, COPY]:
        # Given a graphic title from the user input, make a valid json filename
        graphic_filename_no_ext = sanitize_string(graphic_dict[GRAPHIC_TITLE])
        if os.path.exists(
                os.path.join(
                    current_app.config[CONFIG_FILE_FOLDER],
                    f"{graphic_filename_no_ext}.json",
                )):
            i = 0
            while os.path.exists(
                    os.path.join(
                        current_app.config[CONFIG_FILE_FOLDER],
                        f"{graphic_filename_no_ext}_{i}.json",
                    )):
                i += 1
            graphic_filename = f"{graphic_filename_no_ext}_{i}.json"
        else:
            graphic_filename = f"{graphic_filename_no_ext}.json"
        # make sure we are not overwriting something
        main_config_dict = load_main_config_dict_if_exists(current_app)
        page_dict = main_config_dict[AVAILABLE_PAGES][page_id]
        graphic_list = page_dict.get(GRAPHIC_CONFIG_FILES, [])
        graphic_list.append(graphic_filename)
        page_dict[GRAPHIC_CONFIG_FILES] = graphic_list
        save_main_config_dict(main_config_dict)

    graphic_filepath = os.path.join(current_app.config[CONFIG_FILE_FOLDER],
                                    graphic_filename)
    # remove and write new file to trigger file watcher and refresh flask app
    if os.path.exists(graphic_filepath):
        os.remove(graphic_filepath)
    with open(graphic_filepath, "w") as fout:
        json.dump(graphic_dict, fout, indent=4)
    return (
        json.dumps({
            "success": True,
            GRAPHIC_PATH: graphic_filename
        }),
        200,
        {
            "ContentType": "application/json"
        },
    )
Пример #2
0
def preview_graphic_json_config():
    config_information_dict = request.get_json()
    graphic_dict = graphic_component_dict_to_graphic_dict(
        config_information_dict[CONFIG_DICT])
    # get_data_for_page needs a graphic label
    plot_specs = get_data_for_page({PREVIEW: graphic_dict})
    # plot_specs is more nested than we want for the preveiw so get the plot_dict and pass that to the html
    return (
        json.dumps({
            "success": True,
            PREVIEW: plot_specs[0][JINJA_PLOT_INFO]
        }),
        200,
        {
            "ContentType": "application/json"
        },
    )
Пример #3
0
def test_graphic_component_dict_to_graphic_dict(graphic_json_fixture):
    test_config = graphic_json_fixture["graphic_1"]
    component_dict = graphic_dict_to_graphic_component_dict(test_config)
    new_test_config = graphic_component_dict_to_graphic_dict(component_dict)
    assert new_test_config == test_config