Esempio n. 1
0
def test_process_stylesheet_with_app_name():
    # The main file automatically appends / at the end of app_name

    stylesheet_string = '<link rel="stylesheet" href="style.css">'
    expected = '<link rel="stylesheet" href=" {% static \'main/style.css\' %} ">'
    result = processing_utils.process_line(stylesheet_string, 'main/')
    assert result == expected
Esempio n. 2
0
def process_file(directory: str,
                 filepath: str,
                 fname: str,
                 app_name: str = APP_NAME):
    """
    Prcocesses the file passed in as parameter, translating it into django
    friendly HTML.

    Saves the translated files in a newly created directory, "Modified_files"

    Parameters
    ----------
    directory : str\n
            directory that contains the file that is to be translated
    filepath : str\n
            path of the file that is to be translated
    fname : fname\n
            name of the file to be translated
    app_name: str\n
            name of the application to be used, defaults to none
    """

    fname = fname.split(".")[0]
    extension = "html"

    # Generate filename and directory of the files to be created
    save_path = os.path.join(directory, "Modified_files")
    save_path = os.path.join(save_path, fname)
    # Open a blank file to write translated HTML to
    f = open(save_path + "." + extension, "w+")
    f.write("{% load static %}")
    f.write("\n")

    try:
        # Opening the file
        with open(filepath) as fp:
            # Reading data line-by-line
            line = fp.readline()
            cnt = 1
            while line:
                # process the line extracted
                temp = process_line(line, app_name)
                line = fp.readline()
                cnt += 1
                # write the processed line to the newly created file
                f.write(temp)
    except IOError:
        logging.error("An error occurred trying to read the file.")
    finally:
        # Close the file to save changes
        f.close()

    logging.info("Succeeded.. Generated Modified_Files/" + fname + "." +
                 extension + " in the directory passed.")
Esempio n. 3
0
def test_process_line_on_inline_image():
    inline_image_string = '<div style="background-image: url(img/img.jpg);">'
    expected = '<div style="background-image: url( {% static \'img/img.jpg\' %} );">'
    result = processing_utils.process_line(inline_image_string, '')
    assert result == expected
Esempio n. 4
0
def test_process_line_on_image():
    image_string = '< img src = "img/img.jpg" alt = "Nice image" >'
    expected = '< img src = {% static \' "img/img.jpg" alt \' %} = "Nice image" >'
    result = processing_utils.process_line(image_string, '')
    assert result == expected
Esempio n. 5
0
def test_process_line_on_js():
    js_string = '<script src="js/bootstrap/bootstrap.min.js">'
    expected = '<script src=" {% static \'js/bootstrap/bootstrap.min.js\' %} ">'
    result = processing_utils.process_line(js_string, '')
    assert result == expected
Esempio n. 6
0
def test_process_line_on_stylesheet():
    stylesheet_string = '<link rel="stylesheet" href="style.css">'
    expected = '<link rel="stylesheet" href=" {% static \'style.css\' %} ">'
    result = processing_utils.process_line(stylesheet_string, '')
    assert result == expected
Esempio n. 7
0
def test_process_line_on_external_link():
    image_external = '<img class="img-profile rounded-circle"' \
                     ' src="https://source.unsplash.com/NftWwc-oY2M/60x60">'
    result = processing_utils.process_line(image_external, '')
    assert result == image_external