Esempio n. 1
0
 def test_check_file_length_true_upper(self):
     """
     Asserts True - showing that the file length is within the
     acceptable range. This test is checking an upper file length
     """
     file_length = 15999999
     self.assertTrue(
         file_uploader.check_file_length(file_length,
                                         FlaskTest.max_file_size,
                                         FlaskTest.min_file_size))
Esempio n. 2
0
 def test_check_file_length_false_upper(self):
     """
     Asserts False - showing that that the file length is not within
     the acceptable range. This test is checking the upper file length
     boundary
     """
     file_length = 16000001
     self.assertFalse(
         file_uploader.check_file_length(file_length,
                                         FlaskTest.max_file_size,
                                         FlaskTest.min_file_size))
Esempio n. 3
0
 def test_check_file_length_true_upper_boundary(self):
     """
     Asserts True - showing that the file length is within the
     acceptable range. This test is checking the upper file length
     boundary
     """
     file_length = 16 * 1000 * 1000
     self.assertTrue(
         file_uploader.check_file_length(file_length,
                                         FlaskTest.max_file_size,
                                         FlaskTest.min_file_size))
Esempio n. 4
0
def index_post_request(upload_path):
    """This function handles how the index.html page processes data.txt and
        serves the page when a post request is received.

        It will first check if there is a file request, and if so will iterate
        through each file validating them to ensure that they have a file name,
        are of the right extension and are between two file sizes.

        If they fail any validation, the file is disregarded.
        Else, file is saved into a directory unique for the user's session.
        Then, calls functions to extract and process the citations within the
        files that have been successfully saved, using a loader based on
        the file extension.

    Args:
        upload_path (string): The upload directory - a combination of /
        "upload/" + the session name

    Variables:
        files_processed (dictionary): Tracks the amount of files processed.
        If True (meaning a file has been successfully processed) >= 1,
        a zip is created and the download button is shown to the user]
    Returns:
        render_template: Serves the index.html page
        send_from_directory: Downloads the results zip for the client
    """
    if request.files:
        files_processed = {"True": 0, "False": 0}
        files = request.files.getlist("uploaded_file")

        # loop, as possibility of multiple file uploads
        for file_to_upload in files:
            # Gets the length of the file
            file_to_upload.seek(0, os.SEEK_END)
            file_length = file_to_upload.tell()
            # reset pointer to start of file, otherwise will be empty
            file_to_upload.seek(0)
            # Secures file name against user input
            file_name = secure_filename(file_to_upload.filename)
            # Checks the file name isn't blank
            if file_uploader.check_file_name_empty(file_name) is True:
                logging.info("Error uploading " + file_to_upload.filename +
                             "from " + str(session['public_user']) +
                             "- empty file name.")
                files_processed['False'] += 1
                continue
            # Checks the file has an allowed extension
            elif file_uploader.allowed_ext(file_to_upload.filename,
                                           config.ALLOWED_EXTENSIONS) is False:
                logging.info("Error uploading " + file_to_upload.filename +
                             "from " + str(session['public_user']) +
                             "- extension not supported.")
                files_processed['False'] += 1
                continue
            # Checks file size
            elif file_uploader.check_file_length(
                    file_length, config.MAX_FILE_SIZE,
                    config.MIN_FILE_SIZE) is False:
                logging.info("Error uploading " + file_to_upload.filename +
                             "from " + str(session['public_user']) +
                             file_to_upload.filename + " invalid file size.")
                files_processed['False'] += 1
                continue
            else:  # Else, passes all validation and is saved.
                files_processed['True'] += 1
                file_name = file_uploader.check_existing_file_name(
                    file_name, "uploads/")
                file_path = upload_path + "/" + file_name
                file_to_upload.save(file_path)
                citations = file_processing.extract_citations(file_path)
                results = file_processing.start_citation_analysis(citations)
                file_processing.write_citations_to_file_json(
                    results, upload_path)

        # If files have been processed,
        #       return a render with the file download.
        if files_processed['True'] >= 1:
            # file_processing.generate_results_chart(
            # upload_path)
            file_processing.create_zip(upload_path, config.RESULTS_ZIP)
            return True
        else:  # Else, normal redirect.
            return False
        # If user clicked download results button
    else:  # If no files request, redirect to index.
        return redirect(request.url)