Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[import csv] ensure directory exists before saving csv file #4829

Merged
merged 1 commit into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
import errno
import functools
import json
import logging
Expand Down Expand Up @@ -827,3 +828,11 @@ def is_adhoc_metric(metric):

def get_metric_names(metrics):
return [metric['label'] if is_adhoc_metric(metric) else metric for metric in metrics]


def ensure_path_exists(path):
try:
os.makedirs(path)
except OSError as exc:
if not (os.path.isdir(path) and exc.errno == errno.EEXIST):
raise
8 changes: 5 additions & 3 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,17 @@ 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
path = os.path.join(config['UPLOAD_FOLDER'], csv_filename)
try:
csv_file.save(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
utils.ensure_path_exists(config['UPLOAD_FOLDER'])
csv_file.save(path)
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 Exception as e:
try:
os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
os.remove(path)
except OSError:
pass
message = 'Table name {} already exists. Please pick another'.format(
Expand All @@ -365,7 +367,7 @@ def form_post(self, form):
'danger')
return redirect('/csvtodatabaseview/form')

os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
os.remove(path)
# Go back to welcome page / splash screen
db_name = table.database.database_name
message = _('CSV file "{0}" uploaded to table "{1}" in '
Expand Down