xrmx commented on a change in pull request #4298: Refactor import csv
URL: 
https://github.com/apache/incubator-superset/pull/4298#discussion_r164987668
 
 

 ##########
 File path: superset/views/core.py
 ##########
 @@ -319,49 +318,38 @@ def form_get(self, form):
         form.infer_datetime_format.data = True
         form.decimal.data = '.'
         form.if_exists.data = 'append'
-        all_datasources = (
-            db.session.query(
-                models.Database.sqlalchemy_uri,
-                models.Database.database_name)
-            .all()
-        )
-        form.con.choices += all_datasources
 
     def form_post(self, form):
-        def _upload_file(csv_file):
-            if csv_file and csv_file.filename:
-                filename = secure_filename(csv_file.filename)
-                csv_file.save(os.path.join(config['UPLOAD_FOLDER'], filename))
-                return filename
-
         csv_file = form.csv_file.data
-        _upload_file(csv_file)
-        table = SqlaTable(table_name=form.name.data)
-        database = (
-            db.session.query(models.Database)
-            .filter_by(sqlalchemy_uri=form.data.get('con'))
-            .one()
-        )
-        table.database = database
-        table.database_id = database.id
+        form.csv_file.data.filename = 
secure_filename(form.csv_file.data.filename)
+        csv_filename = form.csv_file.data.filename
         try:
-            database.db_engine_spec.create_table_from_csv(form, table)
+            utils.upload_file(csv_file)
+            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 IntegrityError:
+            os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
+            flash(
+                u'Table name {} already exists. Please pick another'.format(
+                    form.name.data),
+                'danger')
+            return redirect('/csvtodatabaseview/form')
+
         except Exception as e:
-            os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_file.filename))
-            flash(e, 'error')
-            return redirect('/tablemodelview/list/')
+            if csv_filename in os.listdir(config['UPLOAD_FOLDER']):
 
 Review comment:
   try:
       os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename))
   except OSError:
        pass

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to