superset icon indicating copy to clipboard operation
superset copied to clipboard

Can't export dashboards with CLI if 'admin' user is not created

Open xavier-GitHub76 opened this issue 1 year ago • 1 comments

Bug description

Hello, with Superset 3.1.1, I can't export dashboards with CLI if 'admin' user is not created

There should be a -u option to specify the admin account to use

Actually, on all branchs, 'admin' is hard coded : https://github.com/apache/superset/blob/master/superset/cli/importexport.py image

Best regards

How to reproduce the bug

I have an admin user 'xavier’ (user 'admin' is not created) image

If I export / import from web app with user ‘xavier’, it's OK

with CLI, I have an error "AttributeError: 'NoneType' object has no attribute 'is_anonymous'" image

To succed, user 'admin' must exist image image

Screenshots/recordings

No response

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Chrome

Additional context

No response

Checklist

  • [ ] I have searched Superset docs and Slack and didn't find a solution to my problem.
  • [ ] I have searched the GitHub issue tracker and didn't find a similar bug report.
  • [ ] I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

xavier-GitHub76 avatar May 24 '24 08:05 xavier-GitHub76

To address the issue of exporting dashboards using the CLI in Superset 3.1.1 when the 'admin' user is not created, you can modify the export_dashboards function to include a -u option. This allows you to specify a different admin account. Here is the updated function:

@click.command()
@with_appcontext
@click.option(
    "--dashboard-file",
    "-f",
    help="Specify the file to export to",
)
@click.option(
    "--username",
    "-u",
    required=True,
    help="Specify the user name to use for exporting dashboards",
)
def export_dashboards(dashboard_file: Optional[str] = None, username: Optional[str] = None) -> None:
    """Export dashboards to ZIP file"""
    from superset.commands.dashboard.export import ExportDashboardsCommand
    from superset.models.dashboard import Dashboard

    g.user = security_manager.find_user(username=username)

    dashboard_ids = [id_ for (id_,) in db.session.query(Dashboard.id).all()]
    timestamp = datetime.now().strftime("%Y%m%dT%H%M%S")
    root = f"dashboard_export_{timestamp}"
    dashboard_file = dashboard_file or f"{root}.zip"

    try:
        with ZipFile(dashboard_file, "w") as bundle:
            for file_name, file_content in ExportDashboardsCommand(dashboard_ids).run():
                with bundle.open(f"{root}/{file_name}", "w") as fp:
                    fp.write(file_content().encode())
    except Exception:
        logger.exception(
            "There was an error when exporting the dashboards, please check "
            "the exception traceback in the log"
        )
        sys.exit(1)

This modification allows you to specify a different admin account using the -u option when exporting dashboards [1].

Additionally, there are existing command-line options in the Superset CLI that allow specifying a user for other commands, such as importing dashboards and datasources. These options are implemented using the --username or -u flag [1].

However, the current tests for the export_dashboards function do not cover scenarios where the 'admin' user does not exist. All tests assume the existence of the 'admin' user [2][3].

Details

About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

dosubot[bot] avatar May 24 '24 08:05 dosubot[bot]

This has gone stale (it's been almost a year), is reported on an unsupported version of Superset (we only support 4.x and 5.x as of now), and seems to be as much a feature request as it is a bug. I'll close it for those reasons, but if you're still facing this in current versions, we can reopen this and/or convert it to an "Ideas" Discussion.

rusackas avatar Mar 20 '25 22:03 rusackas