mirror of
https://github.com/apache/superset.git
synced 2026-05-12 19:35:17 +00:00
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:
committed by
Ville Brofeldt
parent
18f82411c9
commit
a6a2def6d3
@@ -24,8 +24,9 @@ from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.state import TemporaryExploreState
|
||||
from superset.explore.utils import check_access
|
||||
from superset.extensions import cache_manager
|
||||
from superset.key_value.utils import random_key
|
||||
from superset.temporary_cache.commands.exceptions import TemporaryCacheCreateFailedError
|
||||
from superset.temporary_cache.utils import cache_key, random_key
|
||||
from superset.temporary_cache.utils import cache_key
|
||||
from superset.utils.schema import validate_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -26,11 +26,12 @@ from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.state import TemporaryExploreState
|
||||
from superset.explore.utils import check_access
|
||||
from superset.extensions import cache_manager
|
||||
from superset.key_value.utils import random_key
|
||||
from superset.temporary_cache.commands.exceptions import (
|
||||
TemporaryCacheAccessDeniedError,
|
||||
TemporaryCacheUpdateFailedError,
|
||||
)
|
||||
from superset.temporary_cache.utils import cache_key, random_key
|
||||
from superset.temporary_cache.utils import cache_key
|
||||
from superset.utils.schema import validate_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
# under the License.
|
||||
import logging
|
||||
|
||||
from flask import current_app, g, request, Response
|
||||
from flask import g, request, Response
|
||||
from flask_appbuilder.api import BaseApi, expose, protect, safe
|
||||
from marshmallow import ValidationError
|
||||
|
||||
@@ -98,12 +98,9 @@ class ExplorePermalinkRestApi(BaseApi):
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
key_type = current_app.config["PERMALINK_KEY_TYPE"]
|
||||
try:
|
||||
state = self.add_model_schema.load(request.json)
|
||||
key = CreateExplorePermalinkCommand(
|
||||
actor=g.user, state=state, key_type=key_type,
|
||||
).run()
|
||||
key = CreateExplorePermalinkCommand(actor=g.user, state=state).run()
|
||||
http_origin = request.headers.environ.get("HTTP_ORIGIN")
|
||||
url = f"{http_origin}/superset/explore/p/{key}/"
|
||||
return self.response(201, key=key, url=url)
|
||||
@@ -159,10 +156,7 @@ class ExplorePermalinkRestApi(BaseApi):
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
try:
|
||||
key_type = current_app.config["PERMALINK_KEY_TYPE"]
|
||||
value = GetExplorePermalinkCommand(
|
||||
actor=g.user, key=key, key_type=key_type
|
||||
).run()
|
||||
value = GetExplorePermalinkCommand(actor=g.user, key=key).run()
|
||||
if not value:
|
||||
return self.response_404()
|
||||
return self.response(200, **value)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user