diff --git a/superset/charts/api.py b/superset/charts/api.py index 7e027d66a..90c353249 100644 --- a/superset/charts/api.py +++ b/superset/charts/api.py @@ -63,7 +63,7 @@ from superset.charts.schemas import ( thumbnail_query_schema, ) from superset.commands.exceptions import CommandInvalidError -from superset.commands.importers.v1.utils import is_valid_config, remove_root +from superset.commands.importers.v1.utils import get_contents_from_bundle from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod from superset.exceptions import SupersetSecurityException from superset.extensions import event_logger @@ -1013,11 +1013,7 @@ class ChartRestApi(BaseSupersetModelRestApi): if not upload: return self.response_400() with ZipFile(upload) as bundle: - contents = { - remove_root(file_name): bundle.read(file_name).decode() - for file_name in bundle.namelist() - if is_valid_config(file_name) - } + contents = get_contents_from_bundle(bundle) passwords = ( json.loads(request.form["passwords"]) diff --git a/superset/commands/importers/v1/utils.py b/superset/commands/importers/v1/utils.py index a5dd09cd0..b071678a0 100644 --- a/superset/commands/importers/v1/utils.py +++ b/superset/commands/importers/v1/utils.py @@ -16,6 +16,7 @@ import logging from pathlib import Path from typing import Any, Dict +from zipfile import ZipFile import yaml from marshmallow import fields, Schema, validate @@ -87,3 +88,11 @@ def is_valid_config(file_name: str) -> bool: return False return True + + +def get_contents_from_bundle(bundle: ZipFile) -> Dict[str, str]: + return { + remove_root(file_name): bundle.read(file_name).decode() + for file_name in bundle.namelist() + if is_valid_config(file_name) + } diff --git a/superset/dashboards/api.py b/superset/dashboards/api.py index 3262bfe40..61b56b1de 100644 --- a/superset/dashboards/api.py +++ b/superset/dashboards/api.py @@ -31,7 +31,7 @@ from werkzeug.wsgi import FileWrapper from superset import is_feature_enabled, thumbnail_cache from superset.commands.exceptions import CommandInvalidError -from superset.commands.importers.v1.utils import remove_root +from superset.commands.importers.v1.utils import get_contents_from_bundle from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod from superset.dashboards.commands.bulk_delete import BulkDeleteDashboardCommand from superset.dashboards.commands.create import CreateDashboardCommand @@ -729,10 +729,7 @@ class DashboardRestApi(BaseSupersetModelRestApi): if not upload: return self.response_400() with ZipFile(upload) as bundle: - contents = { - remove_root(file_name): bundle.read(file_name).decode() - for file_name in bundle.namelist() - } + contents = get_contents_from_bundle(bundle) passwords = ( json.loads(request.form["passwords"]) diff --git a/superset/databases/api.py b/superset/databases/api.py index e9f3ffc86..e8273eb1e 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -29,7 +29,7 @@ from sqlalchemy.exc import NoSuchTableError, OperationalError, SQLAlchemyError from superset import event_logger from superset.commands.exceptions import CommandInvalidError -from superset.commands.importers.v1.utils import remove_root +from superset.commands.importers.v1.utils import get_contents_from_bundle from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod from superset.databases.commands.create import CreateDatabaseCommand from superset.databases.commands.delete import DeleteDatabaseCommand @@ -778,10 +778,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi): if not upload: return self.response_400() with ZipFile(upload) as bundle: - contents = { - remove_root(file_name): bundle.read(file_name).decode() - for file_name in bundle.namelist() - } + contents = get_contents_from_bundle(bundle) passwords = ( json.loads(request.form["passwords"]) diff --git a/superset/datasets/api.py b/superset/datasets/api.py index 84ab33606..af235ced1 100644 --- a/superset/datasets/api.py +++ b/superset/datasets/api.py @@ -31,7 +31,7 @@ from marshmallow import ValidationError from superset import event_logger, is_feature_enabled from superset.commands.exceptions import CommandInvalidError -from superset.commands.importers.v1.utils import remove_root +from superset.commands.importers.v1.utils import get_contents_from_bundle from superset.connectors.sqla.models import SqlaTable from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod from superset.databases.filters import DatabaseFilter @@ -687,10 +687,7 @@ class DatasetRestApi(BaseSupersetModelRestApi): if not upload: return self.response_400() with ZipFile(upload) as bundle: - contents = { - remove_root(file_name): bundle.read(file_name).decode() - for file_name in bundle.namelist() - } + contents = get_contents_from_bundle(bundle) passwords = ( json.loads(request.form["passwords"])