feat: introduce hashids permalink keys (#19324)

* feat: introduce hashids permalink keys

* implement dashboard permalinks

* remove shorturl notice from UPDATING.md

* lint

* fix test

* introduce KeyValueResource

* make filterState optional

* fix test

* fix resource names

(cherry picked from commit f4b71abb22)
This commit is contained in:
Ville Brofeldt
2022-03-24 21:53:09 +02:00
committed by Ville Brofeldt
parent 18f82411c9
commit a6a2def6d3
39 changed files with 344 additions and 367 deletions

View File

@@ -17,7 +17,13 @@
from abc import ABC
from superset.commands.base import BaseCommand
from superset.key_value.shared_entries import get_permalink_salt
from superset.key_value.types import KeyValueResource, SharedKey
class BaseExplorePermalinkCommand(BaseCommand, ABC):
resource = "explore_permalink"
resource: KeyValueResource = KeyValueResource.EXPLORE_PERMALINK
@property
def salt(self) -> str:
return get_permalink_salt(SharedKey.EXPLORE_PERMALINK_SALT)

View File

@@ -24,18 +24,17 @@ from superset.explore.permalink.commands.base import BaseExplorePermalinkCommand
from superset.explore.permalink.exceptions import ExplorePermalinkCreateFailedError
from superset.explore.utils import check_access
from superset.key_value.commands.create import CreateKeyValueCommand
from superset.key_value.types import KeyType
from superset.key_value.utils import encode_permalink_key
logger = logging.getLogger(__name__)
class CreateExplorePermalinkCommand(BaseExplorePermalinkCommand):
def __init__(self, actor: User, state: Dict[str, Any], key_type: KeyType):
def __init__(self, actor: User, state: Dict[str, Any]):
self.actor = actor
self.chart_id: Optional[int] = state["formData"].get("slice_id")
self.datasource: str = state["formData"]["datasource"]
self.state = state
self.key_type = key_type
def run(self) -> str:
self.validate()
@@ -49,12 +48,10 @@ class CreateExplorePermalinkCommand(BaseExplorePermalinkCommand):
"state": self.state,
}
command = CreateKeyValueCommand(
actor=self.actor,
resource=self.resource,
value=value,
key_type=self.key_type,
actor=self.actor, resource=self.resource, value=value,
)
return command.run()
key = command.run()
return encode_permalink_key(key=key.id, salt=self.salt)
except SQLAlchemyError as ex:
logger.exception("Error running create command")
raise ExplorePermalinkCreateFailedError() from ex

View File

@@ -27,24 +27,22 @@ from superset.explore.permalink.types import ExplorePermalinkValue
from superset.explore.utils import check_access
from superset.key_value.commands.get import GetKeyValueCommand
from superset.key_value.exceptions import KeyValueGetFailedError, KeyValueParseKeyError
from superset.key_value.types import KeyType
from superset.key_value.utils import decode_permalink_id
logger = logging.getLogger(__name__)
class GetExplorePermalinkCommand(BaseExplorePermalinkCommand):
def __init__(
self, actor: User, key: str, key_type: KeyType,
):
def __init__(self, actor: User, key: str):
self.actor = actor
self.key = key
self.key_type = key_type
def run(self) -> Optional[ExplorePermalinkValue]:
self.validate()
try:
key = decode_permalink_id(self.key, salt=self.salt)
value: Optional[ExplorePermalinkValue] = GetKeyValueCommand(
resource=self.resource, key=self.key, key_type=self.key_type
resource=self.resource, key=key,
).run()
if value:
chart_id: Optional[int] = value.get("chartId")