dpgaspar commented on a change in pull request #11751:
URL: 
https://github.com/apache/incubator-superset/pull/11751#discussion_r527644883



##########
File path: tests/dashboards/api_tests.py
##########
@@ -1077,3 +1086,86 @@ def test_export_bundle_not_allowed(self):
 
         db.session.delete(dashboard)
         db.session.commit()
+
+    def test_import_dashboard(self):
+        """
+        Dashboard API: Test import dashboard
+        """
+        self.login(username="admin")
+        uri = "api/v1/dashboard/import/"
+
+        buf = BytesIO()
+        with ZipFile(buf, "w") as bundle:

Review comment:
       This can be a `pytest. yield_fixture` and then reuse on 
`test_import_dashboard_invalid`

##########
File path: superset/dashboards/api.py
##########
@@ -641,3 +644,56 @@ def favorite_status(self, **kwargs: Any) -> Response:
             for request_id in requested_ids
         ]
         return self.response(200, result=res)
+
+    @expose("/import/", methods=["POST"])
+    @protect()
+    @safe
+    @statsd_metrics
+    def import_(self) -> Response:
+        """Import dashboard(s) with associated charts/datasets/databases
+        ---
+        post:
+          requestBody:
+            content:
+              application/zip:
+                schema:
+                  type: string
+                  format: binary
+          responses:
+            200:
+              description: Dashboard import result
+              content:
+                application/json:
+                  schema:
+                    type: object
+                    properties:
+                      message:
+                        type: string
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        upload = request.files.get("file")
+        if not upload:
+            return self.response_400()
+        with ZipFile(upload) as bundle:
+            contents = {
+                file_name: bundle.read(file_name).decode()
+                for file_name in bundle.namelist()
+            }
+
+        command = ImportDashboardsCommand(contents)
+        try:
+            command.run()
+            return self.response(200, message="OK")
+        except CommandInvalidError as exc:
+            logger.warning("Import dashboard failed")
+            return self.response_422(message=exc.normalized_messages())
+        except Exception as exc:  # pylint: disable=broad-except

Review comment:
       nit: We could catch any broad exceptions (if it's dificult to pin) on 
`ImportDashboardsCommand` and reraise a `CommandError` new child like 
`ImportDashboardsError`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to