Skip to content

Commit

Permalink
fix: remove unnecessary exception when exploring non-legacy viz plugi…
Browse files Browse the repository at this point in the history
…ns (apache#10538)

* fix: remove unnecessary exception when exploring non-legacy viz plugins

* lint
  • Loading branch information
villebro committed Sep 2, 2020
1 parent 759b4f6 commit 6e75ea6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
13 changes: 8 additions & 5 deletions superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ def datasource_edit_url(self) -> Optional[str]:

@property # type: ignore
@utils.memoized
def viz(self) -> BaseViz:
def viz(self) -> Optional[BaseViz]:
form_data = json.loads(self.params)
viz_class = viz_types[self.viz_type]
return viz_class(datasource=self.datasource, form_data=form_data)
viz_class = viz_types.get(self.viz_type)
if viz_class:
return viz_class(datasource=self.datasource, form_data=form_data)
return None

@property
def description_markeddown(self) -> str:
Expand All @@ -170,8 +172,9 @@ def data(self) -> Dict[str, Any]:
data: Dict[str, Any] = {}
self.token = ""
try:
data = self.viz.data
self.token = data.get("token") # type: ignore
viz = self.viz
data = viz.data if viz else self.form_data
self.token = utils.get_form_data_token(data)
except Exception as ex: # pylint: disable=broad-except
logger.exception(ex)
data["error"] = str(ex)
Expand Down
10 changes: 10 additions & 0 deletions superset/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,16 @@ def get_iterable(x: Any) -> List[Any]:
return x if isinstance(x, list) else [x]


def get_form_data_token(form_data: Dict[str, Any]) -> str:
"""
Return the token contained within form data or generate a new one.
:param form_data: chart form data
:return: original token if predefined, otherwise new uuid4 based token
"""
return form_data.get("token") or "token_" + uuid.uuid4().hex[:8]


class LenientEnum(Enum):
"""Enums that do not raise ValueError when value is invalid"""

Expand Down
2 changes: 1 addition & 1 deletion superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(
self.form_data = form_data

self.query = ""
self.token = self.form_data.get("token", "token_" + uuid.uuid4().hex[:8])
self.token = utils.get_form_data_token(form_data)

self.groupby: List[str] = self.form_data.get("groupby") or []
self.time_shift = timedelta()
Expand Down
7 changes: 7 additions & 0 deletions tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import hashlib
import json
import os
import re
from unittest.mock import Mock, patch

import numpy
Expand All @@ -40,6 +41,7 @@
convert_legacy_filters_into_adhoc,
create_ssl_cert_file,
format_timedelta,
get_form_data_token,
get_iterable,
get_email_address_list,
get_or_create_db,
Expand Down Expand Up @@ -1365,3 +1367,8 @@ def test_schema_one_of_case_insensitive(self):
self.assertEqual("BaZ", validator("BaZ"))
self.assertRaises(marshmallow.ValidationError, validator, "qwerty")
self.assertRaises(marshmallow.ValidationError, validator, 4)

def test_get_form_data_token(self):
assert get_form_data_token({"token": "token_abcdefg1"}) == "token_abcdefg1"
generated_token = get_form_data_token({})
assert re.match(r"^token_[a-z0-9]{8}$", generated_token) is not None

0 comments on commit 6e75ea6

Please sign in to comment.