Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
timifasubaa committed Feb 2, 2018
1 parent 6c43b4c commit 5f90554
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion superset/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
from wtforms.validators import DataRequired, NumberRange, Optional

from superset import app, db
from superset.models import core as models

config = app.config


class CsvToDatabaseForm(DynamicForm):
# pylint: disable=E0211
def all_db_items():
from superset.models import core as models
return db.session.query(models.Database)

name = StringField(
Expand Down
11 changes: 0 additions & 11 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,17 +513,6 @@ def table_has_constraint(table, name, db):
return False


def upload_file(file):
"""Takes in the name of the file to be uploaded and uploads that file"""

from superset import app
config = app.config
if not file or not file.filename:
raise Exception
file.save(os.path.join(config['UPLOAD_FOLDER'], file.filename))
assert file.filename in os.listdir(config['UPLOAD_FOLDER'])


class timeout(object):
"""
To be used in a ``with`` block and timeout its content.
Expand Down
33 changes: 16 additions & 17 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
from flask_babel import gettext as __
from flask_babel import lazy_gettext as _
import pandas as pd
from six import text_type
import sqlalchemy as sqla
import sqlalchemy.exc as sqla_exceptions
from sqlalchemy import create_engine
from sqlalchemy.engine.url import make_url
from sqlalchemy.exc import OperationalError
from sqlalchemy.exc import IntegrityError, OperationalError
from unidecode import unidecode
from werkzeug.routing import BaseConverter
from werkzeug.utils import secure_filename
Expand Down Expand Up @@ -323,29 +323,28 @@ def form_post(self, form):
csv_file = form.csv_file.data
form.csv_file.data.filename = secure_filename(form.csv_file.data.filename)
csv_filename = form.csv_file.data.filename
utils.upload_file(csv_file)
table = SqlaTable(table_name=form.name.data)
table.database = form.data.get('con')
table.database_id = table.database.id
try:
csv_file.save(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
table = SqlaTable(table_name=form.name.data)
table.database = form.data.get('con')
table.database_id = table.database.id
table.database.db_engine_spec.create_table_from_csv(form, table)
except sqla_exceptions.IntegrityError:
os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
except Exception as e:
try:
os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
except OSError:
pass
message = u'Table name {} already exists. Please pick another'.format(
form.name.data) if isinstance(e, IntegrityError) else text_type(e)
flash(
'Table name {} already exists. Please pick another'.format(
form.name.data),
'warning')
message,
'danger')
return redirect('/csvtodatabaseview/form')

except Exception as e:
os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
flash(str(e), 'error')
return redirect('csvtodatabaseview/form')

os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
# Go back to welcome page / splash screen
db_name = table.database.database_name
message = _('CSV file "{0}" uploaded to table "{1}" in '
message = _(u'CSV file "{0}" uploaded to table "{1}" in '
'database "{2}"'.format(csv_filename,
form.name.data,
db_name))
Expand Down
14 changes: 8 additions & 6 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,20 +803,22 @@ def test_import_csv(self):
test_file.write('john,1\n')
test_file.write('paul,2\n')
test_file.close()
main_db_uri = db.session.query(
models.Database)\
.filter_by(database_name='main').all()
main_db_uri = (
db.session.query(models.Database)
.filter_by(database_name='main')
.all()
)

test_file = open(filename, 'rb')
form_data = {
'csv_file': test_file,
'sep': ',',
'name': table_name,
'con': main_db_uri,
'con': main_db_uri[0].id,
'if_exists': 'append',
'index_label': 'test_label',
'mangle_dupe_cols': False}

'mangle_dupe_cols': False,
}
url = '/databaseview/list/'
add_datasource_page = self.get_resp(url)
assert 'Upload a CSV' in add_datasource_page
Expand Down

0 comments on commit 5f90554

Please sign in to comment.