fix: small fixes to the new import/export (#12064)

This commit is contained in:
Beto Dealmeida 2020-12-15 12:24:24 -08:00 committed by GitHub
parent 52e970a520
commit e0079bb5ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 10 deletions

View File

@ -57,7 +57,7 @@ class ExportChartsCommand(ExportModelsCommand):
# becomes the default export endpoint
for key in REMOVE_KEYS:
del payload[key]
if "params" in payload:
if payload.get("params"):
try:
payload["params"] = json.loads(payload["params"])
except json.decoder.JSONDecodeError:

View File

@ -108,7 +108,7 @@ class ExportDashboardsCommand(ExportModelsCommand):
# TODO (betodealmeida): move this logic to export_to_dict once this
# becomes the default export endpoint
for key, new_name in JSON_KEYS.items():
if key in payload:
if payload.get(key):
value = payload.pop(key)
try:
payload[new_name] = json.loads(value)

View File

@ -109,8 +109,8 @@ def import_dashboard(
value = config.pop(key)
try:
config[new_name] = json.dumps(value)
except json.decoder.JSONDecodeError:
logger.info("Unable to decode `%s` field: %s", key, value)
except TypeError:
logger.info("Unable to encode `%s` field: %s", key, value)
dashboard = Dashboard.import_from_dict(session, config, recursive=False)
if dashboard.id is None:

View File

@ -50,7 +50,7 @@ class ExportDatabasesCommand(ExportModelsCommand):
)
# TODO (betodealmeida): move this logic to export_to_dict once this
# becomes the default export endpoint
if "extra" in payload:
if payload.get("extra"):
try:
payload["extra"] = json.loads(payload["extra"])
except json.decoder.JSONDecodeError:

View File

@ -31,6 +31,8 @@ from superset.utils.dict_import_export import EXPORT_VERSION
logger = logging.getLogger(__name__)
JSON_KEYS = {"params", "template_params", "extra"}
class ExportDatasetsCommand(ExportModelsCommand):
@ -49,6 +51,20 @@ class ExportDatasetsCommand(ExportModelsCommand):
include_defaults=True,
export_uuids=True,
)
# TODO (betodealmeida): move this logic to export_to_dict once this
# becomes the default export endpoint
for key in JSON_KEYS:
if payload.get(key):
try:
payload[key] = json.loads(payload[key])
except json.decoder.JSONDecodeError:
logger.info("Unable to decode `%s` field: %s", key, payload[key])
for metric in payload.get("metrics", []):
if metric.get("extra"):
try:
metric["extra"] = json.loads(metric["extra"])
except json.decoder.JSONDecodeError:
logger.info("Unable to decode `extra` field: %s", metric["extra"])
payload["version"] = EXPORT_VERSION
payload["database_uuid"] = str(model.database.uuid)
@ -67,7 +83,7 @@ class ExportDatasetsCommand(ExportModelsCommand):
)
# TODO (betodealmeida): move this logic to export_to_dict once this
# becomes the default export endpoint
if "extra" in payload:
if payload.get("extra"):
try:
payload["extra"] = json.loads(payload["extra"])
except json.decoder.JSONDecodeError:

View File

@ -15,12 +15,18 @@
# specific language governing permissions and limitations
# under the License.
import json
import logging
from typing import Any, Dict
from sqlalchemy.orm import Session
from superset.connectors.sqla.models import SqlaTable
logger = logging.getLogger(__name__)
JSON_KEYS = {"params", "template_params", "extra"}
def import_dataset(
session: Session, config: Dict[str, Any], overwrite: bool = False
@ -31,6 +37,21 @@ def import_dataset(
return existing
config["id"] = existing.id
# TODO (betodealmeida): move this logic to import_from_dict
config = config.copy()
for key in JSON_KEYS:
if config.get(key):
try:
config[key] = json.dumps(config[key])
except TypeError:
logger.info("Unable to encode `%s` field: %s", key, config[key])
for metric in config.get("metrics", []):
if metric.get("extra"):
try:
metric["extra"] = json.dumps(metric["extra"])
except TypeError:
logger.info("Unable to encode `extra` field: %s", metric["extra"])
# should we delete columns and metrics not present in the current import?
sync = ["columns", "metrics"] if overwrite else []

View File

@ -145,7 +145,7 @@ class ImportV1MetricSchema(Schema):
expression = fields.String(required=True)
description = fields.String(allow_none=True)
d3format = fields.String(allow_none=True)
extra = fields.String(allow_none=True)
extra = fields.Dict(allow_none=True)
warning_text = fields.String(allow_none=True)
@ -158,11 +158,11 @@ class ImportV1DatasetSchema(Schema):
cache_timeout = fields.Integer(allow_none=True)
schema = fields.String(allow_none=True)
sql = fields.String(allow_none=True)
params = fields.String(allow_none=True)
template_params = fields.String(allow_none=True)
params = fields.Dict(allow_none=True)
template_params = fields.Dict(allow_none=True)
filter_select_enabled = fields.Boolean()
fetch_values_predicate = fields.String(allow_none=True)
extra = fields.String(allow_none=True)
extra = fields.Dict(allow_none=True)
uuid = fields.UUID(required=True)
columns = fields.List(fields.Nested(ImportV1ColumnSchema))
metrics = fields.List(fields.Nested(ImportV1MetricSchema))