You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jo...@apache.org on 2018/04/18 22:01:43 UTC

[incubator-superset] branch master updated: ensure directory exists before saving csv file (#4829)

This is an automated email from the ASF dual-hosted git repository.

johnbodley pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new a14dc26  ensure directory exists before saving csv file (#4829)
a14dc26 is described below

commit a14dc26042ad533f34cefc605b9eb9b3597be1f8
Author: timifasubaa <30...@users.noreply.github.com>
AuthorDate: Wed Apr 18 15:01:40 2018 -0700

    ensure directory exists before saving csv file (#4829)
---
 superset/utils.py      | 9 +++++++++
 superset/views/core.py | 8 +++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/superset/utils.py b/superset/utils.py
index d465bb0..2d06c48 100644
--- a/superset/utils.py
+++ b/superset/utils.py
@@ -12,6 +12,7 @@ from email.mime.application import MIMEApplication
 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
@@ -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
diff --git a/superset/views/core.py b/superset/views/core.py
index 19885dc..f66b70e 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -347,15 +347,17 @@ class CsvToDatabaseView(SimpleFormView):
         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(
@@ -365,7 +367,7 @@ class CsvToDatabaseView(SimpleFormView):
                 '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 '

-- 
To stop receiving notification emails like this one, please contact
johnbodley@apache.org.