From 468e952805d9a242d2350cf8472f5a84d83722cf Mon Sep 17 00:00:00 2001 From: james Date: Mon, 19 Feb 2024 16:17:17 +0800 Subject: [PATCH 1/5] Use "admin" user as default for importing datasources --- superset/cli/importexport.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index ebf94b444ae1..e1351399c3e0 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -176,6 +176,8 @@ def import_datasources(path: str) -> None: from superset.commands.dataset.importers.dispatcher import ImportDatasetsCommand from superset.commands.importers.v1.utils import get_contents_from_bundle + g.user = security_manager.find_user(username="admin") + if is_zipfile(path): with ZipFile(path) as bundle: contents = get_contents_from_bundle(bundle) From 0cfa81c34bfe14d69c871be52a9675917cdea395 Mon Sep 17 00:00:00 2001 From: james Date: Wed, 21 Feb 2024 02:50:51 +0800 Subject: [PATCH 2/5] Try with override_user --- superset/cli/importexport.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index e1351399c3e0..eef05b348e6e 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -28,6 +28,7 @@ from superset import security_manager from superset.extensions import db +from superset.utils.core import override_user logger = logging.getLogger(__name__) @@ -176,22 +177,21 @@ def import_datasources(path: str) -> None: from superset.commands.dataset.importers.dispatcher import ImportDatasetsCommand from superset.commands.importers.v1.utils import get_contents_from_bundle - g.user = security_manager.find_user(username="admin") - - if is_zipfile(path): - with ZipFile(path) as bundle: - contents = get_contents_from_bundle(bundle) - else: - with open(path) as file: - contents = {path: file.read()} - try: - ImportDatasetsCommand(contents, overwrite=True).run() - except Exception: # pylint: disable=broad-except - logger.exception( - "There was an error when importing the dataset(s), please check the " - "exception traceback in the log" - ) - sys.exit(1) + with override_user(user=security_manager.find_user(username="admin")): + if is_zipfile(path): + with ZipFile(path) as bundle: + contents = get_contents_from_bundle(bundle) + else: + with open(path) as file: + contents = {path: file.read()} + try: + ImportDatasetsCommand(contents, overwrite=True).run() + except Exception: # pylint: disable=broad-except + logger.exception( + "There was an error when importing the dataset(s), please check the " + "exception traceback in the log" + ) + sys.exit(1) @click.command() From ce0b3bda5f22774237d36b6c557454cffe97828c Mon Sep 17 00:00:00 2001 From: james Date: Mon, 26 Feb 2024 09:06:30 -0800 Subject: [PATCH 3/5] Set username as optional input for import_datasources with default of "admin" --- superset/cli/importexport.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index eef05b348e6e..e3ab63807909 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -171,13 +171,19 @@ def import_dashboards(path: str, username: Optional[str]) -> None: "-p", help="Path to a single ZIP file", ) -def import_datasources(path: str) -> None: +@click.option( + "--username", + "-u", + required=True, + help="Specify the user name to assign dashboards to", +) +def import_datasources(path: str, username: Optional[str] = "admin") -> None: """Import datasources from ZIP file""" # pylint: disable=import-outside-toplevel from superset.commands.dataset.importers.dispatcher import ImportDatasetsCommand from superset.commands.importers.v1.utils import get_contents_from_bundle - with override_user(user=security_manager.find_user(username="admin")): + with override_user(user=security_manager.find_user(username=username)): if is_zipfile(path): with ZipFile(path) as bundle: contents = get_contents_from_bundle(bundle) From 4993006b9d58da211304284112e378e5da10273b Mon Sep 17 00:00:00 2001 From: james Date: Mon, 26 Feb 2024 09:12:50 -0800 Subject: [PATCH 4/5] Set default and required=false in click.option --- superset/cli/importexport.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index e3ab63807909..9ac5bba609e6 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -174,7 +174,8 @@ def import_dashboards(path: str, username: Optional[str]) -> None: @click.option( "--username", "-u", - required=True, + required=False, + default="admin", help="Specify the user name to assign dashboards to", ) def import_datasources(path: str, username: Optional[str] = "admin") -> None: From c38f17a0f444f1fa814b6f24909b8feb6da5dc46 Mon Sep 17 00:00:00 2001 From: james Date: Mon, 26 Feb 2024 09:32:38 -0800 Subject: [PATCH 5/5] Fix typo in option help --- superset/cli/importexport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index 9ac5bba609e6..61f3c0f2efa3 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -176,7 +176,7 @@ def import_dashboards(path: str, username: Optional[str]) -> None: "-u", required=False, default="admin", - help="Specify the user name to assign dashboards to", + help="Specify the user name to assign datasources to", ) def import_datasources(path: str, username: Optional[str] = "admin") -> None: """Import datasources from ZIP file"""