aboutsummaryrefslogtreecommitdiff
path: root/ustayml/views/load_data.py
blob: 20833c2e2f3d222a8bb1edb245a59f654f7fbd65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
from flask import (
    Blueprint, flash, g, redirect, render_template, request, url_for, current_app
)
from werkzeug.exceptions import abort
from werkzeug.utils import secure_filename

from ustayml.views.auth import login_required
from ustayml.db import get_db, get_paginated_rows, get_row

bp = Blueprint('load_data', __name__, url_prefix='/load_data')

ALLOWED_EXTENSIONS = ['txt', 'csv']


@bp.route('/', methods=('GET', 'POST'))
@login_required
def index():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'diccionario' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['diccionario']
        # If the user does not select a file, the browser submits an
        # empty file without a filename.
        if file.filename == '':
            flash('Por favor, suba el diccionario de variables')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(current_app.config['DATASET_PATH'], filename))
            return redirect(url_for('load_data.success', name=filename))
    return render_template(
        'load_data/index.html'
    )

@bp.route('/success', methods=('GET', 'POST'))
@login_required
def success():
    return render_template(
        'load_data/success.html'
    )


# Helper functions

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS