fix(import-datasources): Use "admin" user as default for importing datasources (#27154)
This commit is contained in:
parent
8b4dce71d6
commit
6447cd0e92
|
|
@ -28,6 +28,7 @@ from flask.cli import with_appcontext
|
||||||
|
|
||||||
from superset import security_manager
|
from superset import security_manager
|
||||||
from superset.extensions import db
|
from superset.extensions import db
|
||||||
|
from superset.utils.core import override_user
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -170,26 +171,34 @@ def import_dashboards(path: str, username: Optional[str]) -> None:
|
||||||
"-p",
|
"-p",
|
||||||
help="Path to a single ZIP file",
|
help="Path to a single ZIP file",
|
||||||
)
|
)
|
||||||
def import_datasources(path: str) -> None:
|
@click.option(
|
||||||
|
"--username",
|
||||||
|
"-u",
|
||||||
|
required=False,
|
||||||
|
default="admin",
|
||||||
|
help="Specify the user name to assign datasources to",
|
||||||
|
)
|
||||||
|
def import_datasources(path: str, username: Optional[str] = "admin") -> None:
|
||||||
"""Import datasources from ZIP file"""
|
"""Import datasources from ZIP file"""
|
||||||
# pylint: disable=import-outside-toplevel
|
# pylint: disable=import-outside-toplevel
|
||||||
from superset.commands.dataset.importers.dispatcher import ImportDatasetsCommand
|
from superset.commands.dataset.importers.dispatcher import ImportDatasetsCommand
|
||||||
from superset.commands.importers.v1.utils import get_contents_from_bundle
|
from superset.commands.importers.v1.utils import get_contents_from_bundle
|
||||||
|
|
||||||
if is_zipfile(path):
|
with override_user(user=security_manager.find_user(username=username)):
|
||||||
with ZipFile(path) as bundle:
|
if is_zipfile(path):
|
||||||
contents = get_contents_from_bundle(bundle)
|
with ZipFile(path) as bundle:
|
||||||
else:
|
contents = get_contents_from_bundle(bundle)
|
||||||
with open(path) as file:
|
else:
|
||||||
contents = {path: file.read()}
|
with open(path) as file:
|
||||||
try:
|
contents = {path: file.read()}
|
||||||
ImportDatasetsCommand(contents, overwrite=True).run()
|
try:
|
||||||
except Exception: # pylint: disable=broad-except
|
ImportDatasetsCommand(contents, overwrite=True).run()
|
||||||
logger.exception(
|
except Exception: # pylint: disable=broad-except
|
||||||
"There was an error when importing the dataset(s), please check the "
|
logger.exception(
|
||||||
"exception traceback in the log"
|
"There was an error when importing the dataset(s), please check the "
|
||||||
)
|
"exception traceback in the log"
|
||||||
sys.exit(1)
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue