Repository: apache/superset
File: superset/commands/dashboard/importers/v1/utils.py (Python)

Code before:
        "global_chart_configuration", {}
    )
    scope_excluded = cross_filter_global_config.get("scope", {}).get("excluded", [])
    if scope_excluded:
        cross_filter_global_config["scope"]["excluded"] = [
            id_map[old_id] for old_id in scope_excluded if old_id in id_map
        ]

    if "chart_configuration" in (metadata := fixed.get("metadata", {})):
        # Build remapped configuration in a single pass for clarity/readability.
        new_chart_configuration: dict[str, Any] = {}
        for old_id_str, chart_config in metadata["chart_configuration"].items():
            try:
                old_id_int = int(old_id_str)
            except (TypeError, ValueError):
                continue

            new_id = id_map.get(old_id_int)
            if new_id is None:
                continue

            # Update inner id if present
            if isinstance(chart_config, dict):
                inner_id = chart_config.get("id")
                if isinstance(inner_id, int) and inner_id in id_map:
                    chart_config["id"] = id_map[inner_id]

                # Update cross filter scope excluded ids
                scope = chart_config.get("crossFilters", {}).get("scope", {})
                if isinstance(scope, dict):
                    excluded_scope = scope.get("excluded", [])
                    if excluded_scope:
                        chart_config["crossFilters"]["scope"]["excluded"] = [
                            id_map[old_id]
                            for old_id in excluded_scope
                            if old_id in id_map
                        ]

            new_chart_configuration[str(new_id)] = chart_config

        metadata["chart_configuration"] = new_chart_configuration
    return fixed


def import_dashboard(  # noqa: C901
    config: dict[str, Any],
    overwrite: bool = False,
    ignore_permissions: bool = False,
) -> Dashboard:
    can_write = ignore_permissions or security_manager.can_access(
        "can_write",

Review comment (suggestion):
Thinking out loud here, if we already found the "top/key-level ID", can't we just do `chart_config["id"] = new_id` directly (without checking if `chart_config` has an `id` property and that its value is in the map)?

Not sure if all `chart_configuration` values would have an ID, but I imagine if we have a top-level and we end up adding an inner level that wouldn't break anything right? Would just help reducing the amount of checks needed.

Code after:
        "global_chart_configuration", {}
    )
    scope_excluded = cross_filter_global_config.get("scope", {}).get("excluded", [])
    if scope_excluded:
        cross_filter_global_config["scope"]["excluded"] = [
            id_map[old_id] for old_id in scope_excluded if old_id in id_map
        ]

    if "chart_configuration" in (metadata := fixed.get("metadata", {})):
        # Build remapped configuration in a single pass for clarity/readability.
        new_chart_configuration: dict[str, Any] = {}
        for old_id_str, chart_config in metadata["chart_configuration"].items():
            try:
                old_id_int = int(old_id_str)
            except (TypeError, ValueError):
                continue

            new_id = id_map.get(old_id_int)
            if new_id is None:
                continue

            if isinstance(chart_config, dict):
                chart_config["id"] = new_id

                # Update cross filter scope excluded ids
                scope = chart_config.get("crossFilters", {}).get("scope", {})
                if isinstance(scope, dict):
                    excluded_scope = scope.get("excluded", [])
                    if excluded_scope:
                        chart_config["crossFilters"]["scope"]["excluded"] = [
                            id_map[old_id]
                            for old_id in excluded_scope
                            if old_id in id_map
                        ]

            new_chart_configuration[str(new_id)] = chart_config

        metadata["chart_configuration"] = new_chart_configuration
    return fixed


def import_dashboard(  # noqa: C901
    config: dict[str, Any],
    overwrite: bool = False,
    ignore_permissions: bool = False,
) -> Dashboard:
    can_write = ignore_permissions or security_manager.can_access(
        "can_write",
        "Dashboard",
    )
    existing = db.session.query(Dashboard).filter_by(uuid=config["uuid"]).first()