[fix] Handling of non-existent datasource (#7755)

This commit is contained in:
John Bodley
2019-07-01 11:55:25 -07:00
committed by GitHub
parent 15426febbe
commit e0d040c377
3 changed files with 67 additions and 19 deletions

View File

@@ -16,7 +16,7 @@
# under the License.
# pylint: disable=C,R,W
from collections import defaultdict
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional, Tuple
from urllib import parse
from flask import g, request
@@ -25,6 +25,7 @@ import simplejson as json
from superset import app, db, viz
from superset.connectors.connector_registry import ConnectorRegistry
from superset.exceptions import SupersetException
from superset.legacy import update_time_range
import superset.models.core as models
from superset.utils.core import QueryStatus
@@ -144,20 +145,39 @@ def get_form_data(slice_id=None, use_slice_data=False):
return form_data, slc
def get_datasource_info(datasource_id, datasource_type, form_data):
"""Compatibility layer for handling of datasource info
def get_datasource_info(
datasource_id: Optional[int],
datasource_type: Optional[str],
form_data: Dict[str, Any],
) -> Tuple[int, Optional[str]]:
"""
Compatibility layer for handling of datasource info
datasource_id & datasource_type used to be passed in the URL
directory, now they should come as part of the form_data,
This function allows supporting both without duplicating code"""
This function allows supporting both without duplicating code
:param datasource_id: The datasource ID
:param datasource_type: The datasource type, i.e., 'druid' or 'table'
:param form_data: The URL form data
:returns: The datasource ID and type
:raises SupersetException: If the datasource no longer exists
"""
datasource = form_data.get("datasource", "")
if "__" in datasource:
datasource_id, datasource_type = datasource.split("__")
# The case where the datasource has been deleted
datasource_id = None if datasource_id == "None" else datasource_id
if datasource_id == "None":
datasource_id = None
if not datasource_id:
raise Exception("The datasource associated with this chart no longer exists")
raise SupersetException(
"The datasource associated with this chart no longer exists"
)
datasource_id = int(datasource_id)
return datasource_id, datasource_type