Esempio n. 1
0
def upload_data(return_page: str):
    warnings, errors = [], []

    data = request.files['data']

    with NamedTemporaryFile() as temp_file:
        data.save(temp_file.name)
        data_errors = validate_data(temp_file.name)

        if len(data_errors) > 0:
            errors.extend(data_errors)
        else:
            data_name = secure_filename(data.filename)
            original_data_path = os.path.join(app.config['DATA_FOLDER'],
                                              data_name)

            data_path = find_unique_path(original_data_path)
            if data_path != original_data_path:
                warnings.append(
                    name_already_exists_message('Data', original_data_path,
                                                data_path))

            shutil.copy(temp_file.name, data_path)

    warnings, errors = json.dumps(warnings), json.dumps(errors)

    return redirect(
        url_for(return_page,
                data_upload_warnings=warnings,
                data_upload_errors=errors))
Esempio n. 2
0
def upload_data(return_page: str):
    """
    Uploads a data .csv file.

    :param return_page: The name of the page to render to after uploading the dataset.
    """
    warnings, errors = [], []

    current_user = request.cookies.get('currentUser')

    if not current_user:
        # Use DEFAULT as current user if the client's cookie is not set.
        current_user = app.config['DEFAULT_USER_ID']

    dataset = request.files['dataset']

    with NamedTemporaryFile() as temp_file:
        dataset.save(temp_file.name)
        dataset_errors = validate_data(temp_file.name)

        if len(dataset_errors) > 0:
            errors.extend(dataset_errors)
        else:
            dataset_name = request.form['datasetName']
            # dataset_class = load_args(ckpt).dataset_type  # TODO: SWITCH TO ACTUALLY FINDING THE CLASS

            dataset_id, new_dataset_name = db.insert_dataset(
                dataset_name, current_user, 'UNKNOWN')

            dataset_path = os.path.join(app.config['DATA_FOLDER'],
                                        f'{dataset_id}.csv')

            if dataset_name != new_dataset_name:
                warnings.append(
                    name_already_exists_message('Data', dataset_name,
                                                new_dataset_name))

            shutil.copy(temp_file.name, dataset_path)

    warnings, errors = json.dumps(warnings), json.dumps(errors)

    return redirect(
        url_for(return_page,
                data_upload_warnings=warnings,
                data_upload_errors=errors))