fix: remove unnecessary exception when exploring non-legacy viz plugins (#10538)

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

* lint
This commit is contained in:
Ville Brofeldt 2020-08-07 08:41:39 +03:00 committed by GitHub
parent 0bad77f0fe
commit 363abfa1a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 6 deletions

View File

@ -155,10 +155,12 @@ class Slice(
@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:
@ -170,8 +172,9 @@ class Slice(
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)

View File

@ -1380,6 +1380,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"""

View File

@ -121,7 +121,7 @@ class BaseViz:
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()

View File

@ -22,6 +22,7 @@ from decimal import Decimal
import hashlib
import json
import os
import re
from unittest.mock import Mock, patch
import numpy
@ -40,6 +41,7 @@ from superset.utils.core import (
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,
@ -1365,3 +1367,8 @@ class TestUtils(SupersetTestCase):
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