Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion superset/charts/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _export(model: Slice) -> Iterator[Tuple[str, str]]:
# 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:
Expand Down
2 changes: 1 addition & 1 deletion superset/dashboards/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _export(model: Dashboard) -> Iterator[Tuple[str, str]]:
# 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)
Expand Down
4 changes: 2 additions & 2 deletions superset/dashboards/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion superset/databases/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _export(model: Database) -> Iterator[Tuple[str, str]]:
)
# 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:
Expand Down
18 changes: 17 additions & 1 deletion superset/datasets/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

logger = logging.getLogger(__name__)

JSON_KEYS = {"params", "template_params", "extra"}


class ExportDatasetsCommand(ExportModelsCommand):

Expand All @@ -49,6 +51,20 @@ def _export(model: SqlaTable) -> Iterator[Tuple[str, str]]:
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)
Expand All @@ -67,7 +83,7 @@ def _export(model: SqlaTable) -> Iterator[Tuple[str, str]]:
)
# 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:
Expand Down
21 changes: 21 additions & 0 deletions superset/datasets/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 []

Expand Down
8 changes: 4 additions & 4 deletions superset/datasets/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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))
Expand Down