Esempio n. 1
0
def upload_file():
    form = UploadFileForm()
    if form.validate_on_submit():
        card = form.card.data
        year = form.year.data
        month = form.month.data
        owner = form.owner.data
        file = form.file.data
        if not file or file.filename == '':
            return make_response("no file, error! ")
        if file and allowed_file(file.filename):
            filename = "_".join([card, year, month, owner, 'Transactions.csv'])
            path = os.path.join(app.config['BASE_FOLDER'], 'app', 'uploads',
                                filename)
            try:
                file.save(path)
                parseAndSaveTransctions(card, owner, path)
            except ValueError as ve:
                return make_response(
                    "File parsing error, please check your uploading file type or card type: "
                    + str(ve))
            except Exception as e:
                return make_response("Uploading filed failed, as: " + str(e))
            return redirect(url_for('list_records'))
    return render_template('home/uploadfile.html', form=form)
Esempio n. 2
0
def upload(user):
    """Upload a file from a client machine."""
    file = UploadFileForm()
    if file.validate_on_submit():
        f = file.file_selector.data
        filename = secure_filename(f.filename)

        mrn = file.mrn.data

        if filename[-4:] != '.wav':
            flash('File type must be .wav')
        elif len(mrn) != 7 or not mrn.isnumeric():
            flash('MRN must be a 7 digit number')
        else:
            file_dir_path = os.path.join(application.instance_path, 'files')
            file_path = os.path.join(file_dir_path, filename)
            f.save(file_path)

            # Add this to the queue table
            current_id = User.query.filter_by(username=user).first().id
            transcription_id = str(uuid.uuid4())
            now_utc = pytz.utc.localize(datetime.utcnow())
            now_pst = now_utc - timedelta(hours=7)
            upload_row = Queue(id=current_id,
                               mrn=mrn,
                               transcription_id=transcription_id,
                               timestamp=now_pst,
                               filename=filename)
            db.session.add(upload_row)
            db.session.commit()

            return redirect(url_for('queue', user=user))
    return render_template('upload.html', form=file)
Esempio n. 3
0
def upload_file():
    form = UploadFileForm()
    if form.validate_on_submit():
        app.logger.info('import file')
        data = fJson.load(form.selected_file.data)
        #app.logger.info(data[0]['tags'][0])
        flash('Import success.')
    return render_template('upload.html', form=form)
Esempio n. 4
0
def upload():
    """Upload a file from a client machine."""
    file = UploadFileForm()
    if file.validate_on_submit():
        f = file.file_selector.data
        filename = secure_filename(f.filename)
        file_dir_path = os.path.join(application.instance_path, 'files')
        file_path = os.path.join(file_dir_path, filename)
        # Save file to file_path (instance/ + 'files' + filename)
        f.save(file_path)

        file_dir_path = os.path.join(application.instance_path, 'files')
        file_path = os.path.join(file_dir_path, filename)

        # Convert audio file to text (String)
        r = sr.Recognizer()
        harvard = sr.AudioFile(file_path)
        with harvard as source:
            audio = r.record(source)
        talk_to_text = r.recognize_google(audio)

        # pipe results from talk to text to nlp model
        example_result = prepare_note(spacy_model, talk_to_text)
        """Display the model results."""
        proper_title_keys = [k.title() for k in list(example_result.keys())]

        session['example_result'] = example_result
        session['proper_title_keys'] = proper_title_keys

        # delete the file
        if os.path.exists(file_path):
            os.remove(file_path)
        else:
            print("The file does not exist.")

        return redirect(url_for('results', filename=filename))
    return render_template('upload.html', form=file)
Esempio n. 5
0
def index():
    global selected_dataset, last_dataset
    # ^ it's bad decision...

    if selected_dataset is None:
        selected_dataset = request.args.get("dataset")
        if selected_dataset is not None:
            last_dataset = selected_dataset
            return redirect(url_for(".index"))

    header = None
    if session.get("current_data") is not None:
        header = get_header(session["current_data"])

    simplegraph_form = SimpleGraphForm(header)
    anomalies_form = AnomaliesForm(algorithms)
    dataoverview_form = DataOverviewForm()
    upload_form = UploadFileForm()

    kwargs = dict(
        upload_form=upload_form,
        simplegraph_form=simplegraph_form,
        anomalies_form=anomalies_form,
        dataoverview_form=dataoverview_form,
        header=header,
    )

    def _get_axes(axes_form):
        """
        Return selected columns to draw plots with.
        :return: (axis_x, axis_y)

        """
        return (
            dict(axes_form.axis_x.choices)[axes_form.axis_x.data],
            dict(axes_form.axis_y.choices)[axes_form.axis_y.data],
        )

    def _get_algorithm():
        return dict(anomalies_form.selected_algorithm.choices)[
            anomalies_form.selected_algorithm.data]

    def _filesize(path):
        fs = os.path.getsize(path)
        m = "B"
        if fs > 1025:
            fs /= 1024
            m = "KB"
        if fs > 1025:
            fs /= 1024
            m = "MB"
        if fs > 1025:
            fs /= 1024
            m = "GB"
        fs = round(fs)
        res = "{} {}".format(str(fs), m)
        return res

    # Display content of dataset.
    if upload_form.validate_on_submit() or selected_dataset is not None:
        if selected_dataset is not None:
            uploaded_file = os.path.join("uploads", selected_dataset)
        else:
            uploaded_file = upload_form.uploaded_file.data

        session["current_data"] = get_json_data(uploaded_file)

        header = get_header(session["current_data"])
        values = get_values(session["current_data"])

        simplegraph_form.change_choices(header)

        if selected_dataset is not None:
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)
            kwargs.update(dataset=selected_dataset)
            selected_dataset = None
        else:
            session["current_file_id"] = save_uploaded_file(uploaded_file)
            kwargs.update(dataset=upload_form.uploaded_file.data.filename)
        last_dataset = kwargs["dataset"]

        mat = db_queries.get_dataframe(session["current_file_id"])
        y = mat["outlier"].values
        X = mat.drop("outlier", axis=1).values

        n_dim = X.shape[1] - 1
        n_obj = len(y)
        n_out = np.count_nonzero(y)
        percentage = round(100 * n_out / n_obj, 2)

        kwargs.update(
            simplegraph_form=simplegraph_form,
            anomalies_form=anomalies_form,
            header=header,
            data=values,
            n_dim=n_dim,
            n_obj=n_obj,
            n_out=n_out,
            percentage=percentage,
        )

        return render_template("showcsv.html", **kwargs)

    # Simple plots.
    if simplegraph_form.submit_graph.data:
        try:
            axis_x, axis_y = _get_axes(simplegraph_form)
        except:
            axis_x, axis_y = None, None

        if "current_file_id" not in session:
            uploaded_file = os.path.join("uploads", last_dataset)
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)

            session["current_data"] = get_json_data(uploaded_file)
            header = get_header(session["current_data"])
            simplegraph_form.change_choices(header)
            kwargs.update(simplegraph_form=simplegraph_form)

        filename = plot_df.naive_plot_df(session["current_file_id"], axis_x,
                                         axis_y)
        kwargs.update(filename=filename,
                      axis_x=axis_x,
                      axis_y=axis_y,
                      dataset=last_dataset)

        return render_template("showgraph.html", **kwargs)

    # Anomaly Detection.
    if anomalies_form.submit_anomalies.data:
        # axis_x, axis_y = _get_axes(simplegraph_form)
        selected_algortihm = _get_algorithm()

        if "current_file_id" not in session:
            uploaded_file = os.path.join("uploads", last_dataset)
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)

            session["current_data"] = get_json_data(uploaded_file)
            header = get_header(session["current_data"])
            simplegraph_form.change_choices(header)
            kwargs.update(simplegraph_form=simplegraph_form)

        filename_analyze_selected_algorithm = plot_anomalies.analyze_selected_algorithm(
            session["current_file_id"], last_dataset, selected_algortihm)
        # filename_simple_plot = plot_anomalies.simple_plot(
        #     session["current_file_id"], axis_x, axis_y
        # )
        # filename_simple_anomalies = plot_anomalies.simple_anomalies(
        #     session["current_file_id"], axis_x, axis_y
        # )
        # filename_data_overview = plot_anomalies.data_overview(
        #     session["current_file_id"], dataset_title=last_dataset
        # )

        kwargs.update(
            algorithms=algorithms,
            # axis_x=axis_x,
            # axis_y=axis_y,
            # filename_simple_plot=filename_simple_plot,
            # filename_simple_anomalies=filename_simple_anomalies,
            # filename_data_overview=filename_data_overview,
            filename_analyze_selected_algorithm=
            filename_analyze_selected_algorithm,
            dataset=last_dataset,
        )

        return render_template("showanomalies.html", **kwargs)

    # Data Overview.
    if dataoverview_form.validate_on_submit():
        if "current_file_id" not in session:
            uploaded_file = os.path.join("uploads", last_dataset)
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)

            session["current_data"] = get_json_data(uploaded_file)
            header = get_header(session["current_data"])
            simplegraph_form.change_choices(header)
            kwargs.update(simplegraph_form=simplegraph_form)

        filename_data_overview = plot_anomalies.data_overview(
            session["current_file_id"], dataset_title=last_dataset)

        kwargs.update(filename_data_overview=filename_data_overview,
                      dataset=last_dataset)

        return render_template("showdataoverview.html", **kwargs)

    datasets = sorted(os.listdir("uploads/"), key=str.lower)
    datasets = list(filter(lambda x: x.startswith(".") is False, datasets))

    datasets = list(
        map(lambda x: (x, _filesize(os.path.join("uploads", x))), datasets))
    kwargs.update(datasets=datasets)

    return render_template("showdatasets.html", **kwargs)
Esempio n. 6
0
def admin_problem_file(problem_id):
    #if it is not an admin user or the user is not logged does not display this page
    if not current_user.is_authenticated or not current_user.admin:
        return render_template('404.html')
    if not ProblemInformation.query.filter_by(code=problem_id).first():
        return redirect(url_for('admin'))

    form = UploadFileForm(prefix='form')
    form1 = DeleteFileForm(prefix='form1')
    form11 = SetDescriptionForm(prefix='form11')
    form2 = SetTestCaseForm(prefix='form2')
    form3 = DeleteTestCaseForm(prefix='form3')

    if form.submit.data and form.validate_on_submit():
        filename = os.path.join(app.config['PROBLEMS_DIR'], problem_id,
                                form.file_name.data)
        form.file_sent.data.save(filename)
        f = ProblemFile.query.filter_by(problem_code=problem_id,
                                        file_name=form.file_name.data).first()
        if f:
            f.visible = form.visible.data
            db.session.commit()
            flash("File overwritten.")
        else:
            file = ProblemFile(problem_id, form.file_name.data,
                               form.visible.data)
            db.session.add(file)
            db.session.commit()
            flash("File uploaded.")
    elif form1.submit.data and form1.validate_on_submit():
        file = ProblemFile.query.filter_by(id=form1.file_id.data,
                                           problem_code=problem_id).first()
        if file:
            path = os.path.join(app.config['PROBLEMS_DIR'], problem_id,
                                file.file_name)
            db.session.delete(file)
            db.session.commit()
            os.unlink(path)
            flash('File deleted.')
        else:
            flash('File not found.')
    elif form11.submit.data and form11.validate_on_submit():
        if form11.remove.data:
            p = ProblemInformation.query.filter_by(code=problem_id).first()
            p.description_file = None
            db.session.commit()
            flash('Description file removed')
        else:
            exists = ProblemFile.query.filter_by(
                problem_code=problem_id,
                id=form11.description_file.data).first()
            if not exists:
                flash('Description file not in problem!')
            else:
                p = ProblemInformation.query.filter_by(code=problem_id).first()
                p.description_file = form11.description_file.data
                db.session.commit()
                flash('Description file set.')
    elif form2.submit.data and form2.validate_on_submit():
        in_file = ProblemFile.query.filter_by(
            problem_code=problem_id, id=form2.input_file.data).first()
        res_file = ProblemFile.query.filter_by(problem_code=problem_id,
                                               id=form2.res_file.data).first()
        exists = ProblemTestCaseInformation.query.filter_by(
            problem_code=problem_id, test_case=form2.test_case.data).first()
        if not in_file:
            flash('The input file is not in this problem.')
        if not res_file:
            flash('The response file is not in this problem.')
        elif not exists:
            n = ProblemTestCaseInformation(problem_id, form2.test_case.data,
                                           form2.input_file.data,
                                           form2.res_file.data,
                                           form2.is_open.data)
            db.session.add(n)
            db.session.commit()
            flash('Test case added.')
        else:
            exists.input_file = form2.input_file.data
            exists.res_file = form2.res_file.data
            exists.is_open_case = form2.is_open.data
            db.session.commit()
            flash('Test case was overwritten.')
    elif form3.submit.data and form3.validate_on_submit():
        exists = ProblemTestCaseInformation.query.filter_by(
            problem_code=problem_id, test_case=form3.test_case.data).first()
        if exists:
            db.session.delete(exists)
            db.session.commit()
            flash('Test case removed.')
        else:
            flash('Test case was not removed because it does not exist.')
    return render_template(
        'admin_problem_file.html',
        title='Manage problem - files',
        form=form,
        form1=form1,
        form11=form11,
        form2=form2,
        form3=form3,
        problem=ProblemInformation.query.filter_by(code=problem_id).first(),
        files=ProblemFile.query.filter_by(problem_code=problem_id).all(),
        testcases=ProblemTestCaseInformation.query.filter_by(
            problem_code=problem_id).all())