def import_dashboard(dashboard_to_import: Dashboard, dataset_id_mapping: Optional[Dict[(int, int)]]=None, import_time: Optional[int]=None, database_id: Optional[int]=None) -> int:
    "Imports the dashboard from the object to the database.\n\n    Once dashboard is imported, json_metadata field is extended and stores\n    remote_id and import_time. It helps to decide if the dashboard has to\n    be overridden or just copies over. Slices that belong to this\n    dashboard will be wired to existing tables. This function can be used\n    to import/export dashboards between multiple superset instances.\n    Audit metadata isn't copied over.\n    "

    def alter_positions(dashboard: Dashboard, old_to_new_slc_id_dict: Dict[(int, int)]) -> None:
        'Updates slice_ids in the position json.\n\n        Sample position_json data:\n        {\n            "DASHBOARD_VERSION_KEY": "v2",\n            "DASHBOARD_ROOT_ID": {\n                "type": "DASHBOARD_ROOT_TYPE",\n                "id": "DASHBOARD_ROOT_ID",\n                "children": ["DASHBOARD_GRID_ID"]\n            },\n            "DASHBOARD_GRID_ID": {\n                "type": "DASHBOARD_GRID_TYPE",\n                "id": "DASHBOARD_GRID_ID",\n                "children": ["DASHBOARD_CHART_TYPE-2"]\n            },\n            "DASHBOARD_CHART_TYPE-2": {\n                "type": "CHART",\n                "id": "DASHBOARD_CHART_TYPE-2",\n                "children": [],\n                "meta": {\n                    "width": 4,\n                    "height": 50,\n                    "chartId": 118\n                }\n            },\n        }\n        '
        position_data = json.loads(dashboard.position_json)
        position_json = position_data.values()
        for value in position_json:
            if (isinstance(value, dict) and value.get('meta') and value.get('meta', {}).get('chartId')):
                old_slice_id = value['meta']['chartId']
                if (old_slice_id in old_to_new_slc_id_dict):
                    value['meta']['chartId'] = old_to_new_slc_id_dict[old_slice_id]
        dashboard.position_json = json.dumps(position_data)

    def alter_native_filters(dashboard: Dashboard) -> None:
        json_metadata = json.loads(dashboard.json_metadata)
        native_filter_configuration = json_metadata.get('native_filter_configuration')
        if (not native_filter_configuration):
            return
        for native_filter in native_filter_configuration:
            for target in native_filter.get('targets', []):
                old_dataset_id = target.get('datasetId')
                if (dataset_id_mapping and (old_dataset_id is not None)):
                    target['datasetId'] = dataset_id_mapping.get(old_dataset_id, old_dataset_id)
        dashboard.json_metadata = json.dumps(json_metadata)
    logger.info('Started import of the dashboard: %s', dashboard_to_import.to_json())
    session = db.session
    logger.info('Dashboard has %d slices', len(dashboard_to_import.slices))
    slices = copy(dashboard_to_import.slices)
    dashboard_to_import.slug = None
    old_json_metadata = json.loads((dashboard_to_import.json_metadata or '{}'))
    old_to_new_slc_id_dict: Dict[(int, int)] = {}
    new_timed_refresh_immune_slices = []
    new_expanded_slices = {}
    new_filter_scopes = {}
    i_params_dict = dashboard_to_import.params_dict
    remote_id_slice_map = {slc.params_dict['remote_id']: slc for slc in session.query(Slice).filter(Slice.datasource_id.in_(list(dataset_id_mapping.values()))).all() if ('remote_id' in slc.params_dict)}
    for slc in slices:
        logger.info('Importing slice %s from the dashboard: %s', slc.to_json(), dashboard_to_import.dashboard_title)
        if database_id:
            database_name = session.query(Database).filter((Database.id == database_id)).first().name
            slc.alter_params(database_name=database_name)
        remote_slc = remote_id_slice_map.get(slc.id)
        new_slc_id = import_chart(slc, remote_slc, import_time=import_time)
        old_to_new_slc_id_dict[slc.id] = new_slc_id
        new_slc_id_str = str(new_slc_id)
        old_slc_id_str = str(slc.id)
        if (('timed_refresh_immune_slices' in i_params_dict) and (old_slc_id_str in i_params_dict['timed_refresh_immune_slices'])):
            new_timed_refresh_immune_slices.append(new_slc_id_str)
        if (('expanded_slices' in i_params_dict) and (old_slc_id_str in i_params_dict['expanded_slices'])):
            new_expanded_slices[new_slc_id_str] = i_params_dict['expanded_slices'][old_slc_id_str]
    filter_scopes = {}
    if (('filter_immune_slices' in i_params_dict) or ('filter_immune_slice_fields' in i_params_dict)):
        filter_scopes = convert_filter_scopes(old_json_metadata, slices)
    if ('filter_scopes' in i_params_dict):
        filter_scopes = old_json_metadata.get('filter_scopes')
    if filter_scopes:
        new_filter_scopes = copy_filter_scopes(old_to_new_slc_id_dict=old_to_new_slc_id_dict, old_filter_scopes=filter_scopes)
    existing_dashboard = None
    for dash in session.query(Dashboard).all():
        if (('remote_id' in dash.params_dict) and (dash.params_dict['remote_id'] == dashboard_to_import.id)):
            existing_dashboard = dash
    dashboard_to_import = dashboard_to_import.copy()
    dashboard_to_import.id = None
    dashboard_to_import.reset_ownership()
    if dashboard_to_import.position_json:
        alter_positions(dashboard_to_import, old_to_new_slc_id_dict)
    dashboard_to_import.alter_params(import_time=import_time)
    dashboard_to_import.remove_params(param_to_remove='filter_immune_slices')
    dashboard_to_import.remove_params(param_to_remove='filter_immune_slice_fields')
    if new_filter_scopes:
        dashboard_to_import.alter_params(filter_scopes=new_filter_scopes)
    if new_expanded_slices:
        dashboard_to_import.alter_params(expanded_slices=new_expanded_slices)
    if new_timed_refresh_immune_slices:
        dashboard_to_import.alter_params(timed_refresh_immune_slices=new_timed_refresh_immune_slices)
    alter_native_filters(dashboard_to_import)
    new_slices = session.query(Slice).filter(Slice.id.in_(old_to_new_slc_id_dict.values())).all()
    if existing_dashboard:
        existing_dashboard.override(dashboard_to_import)
        existing_dashboard.slices = new_slices
        session.flush()
        return existing_dashboard.id
    dashboard_to_import.slices = new_slices
    session.add(dashboard_to_import)
    session.flush()
    return dashboard_to_import.id
Imports the dashboard from the object to the database.

Once dashboard is imported, json_metadata field is extended and stores
remote_id and import_time. It helps to decide if the dashboard has to
be overridden or just copies over. Slices that belong to this
dashboard will be wired to existing tables. This function can be used
to import/export dashboards between multiple superset instances.
Audit metadata isn't copied over.
def import_dashboard(dashboard_to_import: Dashboard, dataset_id_mapping: Optional[Dict[(int, int)]]=None, import_time: Optional[int]=None, database_id: Optional[int]=None) -> int:
    "Imports the dashboard from the object to the database.\n\n    Once dashboard is imported, json_metadata field is extended and stores\n    remote_id and import_time. It helps to decide if the dashboard has to\n    be overridden or just copies over. Slices that belong to this\n    dashboard will be wired to existing tables. This function can be used\n    to import/export dashboards between multiple superset instances.\n    Audit metadata isn't copied over.\n    "

    def alter_positions(dashboard: Dashboard, old_to_new_slc_id_dict: Dict[(int, int)]) -> None:
        'Updates slice_ids in the position json.\n\n        Sample position_json data:\n        {\n            "DASHBOARD_VERSION_KEY": "v2",\n            "DASHBOARD_ROOT_ID": {\n                "type": "DASHBOARD_ROOT_TYPE",\n                "id": "DASHBOARD_ROOT_ID",\n                "children": ["DASHBOARD_GRID_ID"]\n            },\n            "DASHBOARD_GRID_ID": {\n                "type": "DASHBOARD_GRID_TYPE",\n                "id": "DASHBOARD_GRID_ID",\n                "children": ["DASHBOARD_CHART_TYPE-2"]\n            },\n            "DASHBOARD_CHART_TYPE-2": {\n                "type": "CHART",\n                "id": "DASHBOARD_CHART_TYPE-2",\n                "children": [],\n                "meta": {\n                    "width": 4,\n                    "height": 50,\n                    "chartId": 118\n                }\n            },\n        }\n        '
        position_data = json.loads(dashboard.position_json)
        position_json = position_data.values()
        for value in position_json:
            if (isinstance(value, dict) and value.get('meta') and value.get('meta', {}).get('chartId')):
                old_slice_id = value['meta']['chartId']
                if (old_slice_id in old_to_new_slc_id_dict):
                    value['meta']['chartId'] = old_to_new_slc_id_dict[old_slice_id]
        dashboard.position_json = json.dumps(position_data)

    def alter_native_filters(dashboard: Dashboard) -> None:
        json_metadata = json.loads(dashboard.json_metadata)
        native_filter_configuration = json_metadata.get('native_filter_configuration')
        if (not native_filter_configuration):
            return
        for native_filter in native_filter_configuration:
            for target in native_filter.get('targets', []):
                old_dataset_id = target.get('datasetId')
                if (dataset_id_mapping and (old_dataset_id is not None)):
                    target['datasetId'] = dataset_id_mapping.get(old_dataset_id, old_dataset_id)
        dashboard.json_metadata = json.dumps(json_metadata)
    logger.info('Started import of the dashboard: %s', dashboard_to_import.to_json())
    session = db.session
    logger.info('Dashboard has %d slices', len(dashboard_to_import.slices))
    slices = copy(dashboard_to_import.slices)
    dashboard_to_import.slug = None
    old_json_metadata = json.loads((dashboard_to_import.json_metadata or '{}'))
    old_to_new_slc_id_dict: Dict[(int, int)] = {}
    new_timed_refresh_immune_slices = []
    new_expanded_slices = {}
    new_filter_scopes = {}
    i_params_dict = dashboard_to_import.params_dict
    remote_id_slice_map = {slc.params_dict['remote_id']: slc for slc in session.query(Slice).filter(Slice.datasource_id.in_(list(dataset_id_mapping.values()))).all() if ('remote_id' in slc.params_dict)}
    for slc in slices:
        logger.info('Importing slice %s from the dashboard: %s', slc.to_json(), dashboard_to_import.dashboard_title)
        if database_id:
            database_name = session.query(Database).filter((Database.id == database_id)).first().name
            slc.alter_params(database_name=database_name)
        remote_slc = remote_id_slice_map.get(slc.id)
        new_slc_id = import_chart(slc, remote_slc, import_time=import_time)
        old_to_new_slc_id_dict[slc.id] = new_slc_id
        new_slc_id_str = str(new_slc_id)
        old_slc_id_str = str(slc.id)
        if (('timed_refresh_immune_slices' in i_params_dict) and (old_slc_id_str in i_params_dict['timed_refresh_immune_slices'])):
            new_timed_refresh_immune_slices.append(new_slc_id_str)
        if (('expanded_slices' in i_params_dict) and (old_slc_id_str in i_params_dict['expanded_slices'])):
            new_expanded_slices[new_slc_id_str] = i_params_dict['expanded_slices'][old_slc_id_str]
    filter_scopes = {}
    if (('filter_immune_slices' in i_params_dict) or ('filter_immune_slice_fields' in i_params_dict)):
        filter_scopes = convert_filter_scopes(old_json_metadata, slices)
    if ('filter_scopes' in i_params_dict):
        filter_scopes = old_json_metadata.get('filter_scopes')
    if filter_scopes:
        new_filter_scopes = copy_filter_scopes(old_to_new_slc_id_dict=old_to_new_slc_id_dict, old_filter_scopes=filter_scopes)
    existing_dashboard = None
    for dash in session.query(Dashboard).all():
        if (('remote_id' in dash.params_dict) and (dash.params_dict['remote_id'] == dashboard_to_import.id)):
            existing_dashboard = dash
    dashboard_to_import = dashboard_to_import.copy()
    dashboard_to_import.id = None
    dashboard_to_import.reset_ownership()
    if dashboard_to_import.position_json:
        alter_positions(dashboard_to_import, old_to_new_slc_id_dict)
    dashboard_to_import.alter_params(import_time=import_time)
    dashboard_to_import.remove_params(param_to_remove='filter_immune_slices')
    dashboard_to_import.remove_params(param_to_remove='filter_immune_slice_fields')
    if new_filter_scopes:
        dashboard_to_import.alter_params(filter_scopes=new_filter_scopes)
    if new_expanded_slices:
        dashboard_to_import.alter_params(expanded_slices=new_expanded_slices)
    if new_timed_refresh_immune_slices:
        dashboard_to_import.alter_params(timed_refresh_immune_slices=new_timed_refresh_immune_slices)
    alter_native_filters(dashboard_to_import)
    new_slices = session.query(Slice).filter(Slice.id.in_(old_to_new_slc_id_dict.values())).all()
    if existing_dashboard:
        existing_dashboard.override(dashboard_to_import)
        existing_dashboard.slices = new_slices
        session.flush()
        return existing_dashboard.id
    dashboard_to_import.slices = new_slices
    session.add(dashboard_to_import)
    session.flush()
    return dashboard_to_import.id