feat: add permalink to dashboard and explore (#19078)

* rename key_value to temporary_cache

* add migration

* create new key_value package

* add commands

* lots of new stuff

* fix schema reference

* remove redundant filter state from bootstrap data

* add missing license headers

* fix pylint

* fix dashboard permalink access

* use valid json mocks for filter state tests

* fix temporary cache tests

* add anchors to dashboard state

* lint

* fix util test

* fix url shortlink button tests

* remove legacy shortner

* remove unused imports

* fix js tests

* fix test

* add native filter state to anchor link

* add UPDATING.md section

* address comments

* address comments

* lint

* fix test

* add utils tests + other test stubs

* add key_value integration tests

* add filter box state to permalink state

* fully support persisting url parameters

* lint, add redirects and a few integration tests

* fix test + clean up trailing comma

* fix anchor bug

* change value to LargeBinary to support persisting binary values

* fix urlParams type and simplify urlencode

* lint

* add optional entry expiration

* fix incorrect chart id + add test
This commit is contained in:
Ville Brofeldt
2022-03-17 01:15:52 +02:00
committed by GitHub
parent d01fdad1d8
commit b7a0559aaf
94 changed files with 2943 additions and 439 deletions

View File

@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

View File

@@ -0,0 +1,171 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from abc import ABC, abstractmethod
from typing import Any
from apispec import APISpec
from apispec.exceptions import DuplicateComponentNameError
from flask import g, request, Response
from flask_appbuilder.api import BaseApi
from marshmallow import ValidationError
from superset.charts.commands.exceptions import (
ChartAccessDeniedError,
ChartNotFoundError,
)
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
from superset.dashboards.commands.exceptions import (
DashboardAccessDeniedError,
DashboardNotFoundError,
)
from superset.datasets.commands.exceptions import (
DatasetAccessDeniedError,
DatasetNotFoundError,
)
from superset.temporary_cache.commands.exceptions import TemporaryCacheAccessDeniedError
from superset.temporary_cache.commands.parameters import CommandParameters
from superset.temporary_cache.schemas import (
TemporaryCachePostSchema,
TemporaryCachePutSchema,
)
from superset.views.base_api import requires_json
logger = logging.getLogger(__name__)
class TemporaryCacheRestApi(BaseApi, ABC):
add_model_schema = TemporaryCachePostSchema()
edit_model_schema = TemporaryCachePutSchema()
method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
include_route_methods = {
RouteMethod.POST,
RouteMethod.PUT,
RouteMethod.GET,
RouteMethod.DELETE,
}
allow_browser_login = True
def add_apispec_components(self, api_spec: APISpec) -> None:
try:
api_spec.components.schema(
TemporaryCachePostSchema.__name__, schema=TemporaryCachePostSchema,
)
api_spec.components.schema(
TemporaryCachePutSchema.__name__, schema=TemporaryCachePutSchema,
)
except DuplicateComponentNameError:
pass
super().add_apispec_components(api_spec)
@requires_json
def post(self, pk: int) -> Response:
try:
item = self.add_model_schema.load(request.json)
tab_id = request.args.get("tab_id")
args = CommandParameters(
actor=g.user, resource_id=pk, value=item["value"], tab_id=tab_id
)
key = self.get_create_command()(args).run()
return self.response(201, key=key)
except ValidationError as ex:
return self.response(400, message=ex.messages)
except (
ChartAccessDeniedError,
DashboardAccessDeniedError,
DatasetAccessDeniedError,
TemporaryCacheAccessDeniedError,
) as ex:
return self.response(403, message=str(ex))
except (ChartNotFoundError, DashboardNotFoundError, DatasetNotFoundError) as ex:
return self.response(404, message=str(ex))
@requires_json
def put(self, pk: int, key: str) -> Response:
try:
item = self.edit_model_schema.load(request.json)
tab_id = request.args.get("tab_id")
args = CommandParameters(
actor=g.user,
resource_id=pk,
key=key,
value=item["value"],
tab_id=tab_id,
)
key = self.get_update_command()(args).run()
return self.response(200, key=key)
except ValidationError as ex:
return self.response(400, message=ex.messages)
except (
ChartAccessDeniedError,
DashboardAccessDeniedError,
DatasetAccessDeniedError,
TemporaryCacheAccessDeniedError,
) as ex:
return self.response(403, message=str(ex))
except (ChartNotFoundError, DashboardNotFoundError, DatasetNotFoundError) as ex:
return self.response(404, message=str(ex))
def get(self, pk: int, key: str) -> Response:
try:
args = CommandParameters(actor=g.user, resource_id=pk, key=key)
value = self.get_get_command()(args).run()
if not value:
return self.response_404()
return self.response(200, value=value)
except (
ChartAccessDeniedError,
DashboardAccessDeniedError,
DatasetAccessDeniedError,
TemporaryCacheAccessDeniedError,
) as ex:
return self.response(403, message=str(ex))
except (ChartNotFoundError, DashboardNotFoundError, DatasetNotFoundError) as ex:
return self.response(404, message=str(ex))
def delete(self, pk: int, key: str) -> Response:
try:
args = CommandParameters(actor=g.user, resource_id=pk, key=key)
result = self.get_delete_command()(args).run()
if not result:
return self.response_404()
return self.response(200, message="Deleted successfully")
except (
ChartAccessDeniedError,
DashboardAccessDeniedError,
DatasetAccessDeniedError,
TemporaryCacheAccessDeniedError,
) as ex:
return self.response(403, message=str(ex))
except (ChartNotFoundError, DashboardNotFoundError, DatasetNotFoundError) as ex:
return self.response(404, message=str(ex))
@abstractmethod
def get_create_command(self) -> Any:
...
@abstractmethod
def get_update_command(self) -> Any:
...
@abstractmethod
def get_get_command(self) -> Any:
...
@abstractmethod
def get_delete_command(self) -> Any:
...

View File

@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

View File

@@ -0,0 +1,45 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from abc import ABC, abstractmethod
from sqlalchemy.exc import SQLAlchemyError
from superset.commands.base import BaseCommand
from superset.temporary_cache.commands.exceptions import TemporaryCacheCreateFailedError
from superset.temporary_cache.commands.parameters import CommandParameters
logger = logging.getLogger(__name__)
class CreateTemporaryCacheCommand(BaseCommand, ABC):
def __init__(self, cmd_params: CommandParameters):
self._cmd_params = cmd_params
def run(self) -> str:
try:
return self.create(self._cmd_params)
except SQLAlchemyError as ex:
logger.exception("Error running create command")
raise TemporaryCacheCreateFailedError() from ex
def validate(self) -> None:
pass
@abstractmethod
def create(self, cmd_params: CommandParameters) -> str:
...

View File

@@ -0,0 +1,45 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from abc import ABC, abstractmethod
from sqlalchemy.exc import SQLAlchemyError
from superset.commands.base import BaseCommand
from superset.temporary_cache.commands.exceptions import TemporaryCacheDeleteFailedError
from superset.temporary_cache.commands.parameters import CommandParameters
logger = logging.getLogger(__name__)
class DeleteTemporaryCacheCommand(BaseCommand, ABC):
def __init__(self, cmd_params: CommandParameters):
self._cmd_params = cmd_params
def run(self) -> bool:
try:
return self.delete(self._cmd_params)
except SQLAlchemyError as ex:
logger.exception("Error running delete command")
raise TemporaryCacheDeleteFailedError() from ex
def validate(self) -> None:
pass
@abstractmethod
def delete(self, cmd_params: CommandParameters) -> bool:
...

View File

@@ -0,0 +1,22 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing_extensions import TypedDict
class Entry(TypedDict):
owner: int
value: str

View File

@@ -0,0 +1,45 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from flask_babel import lazy_gettext as _
from superset.commands.exceptions import (
CommandException,
CreateFailedError,
DeleteFailedError,
ForbiddenError,
UpdateFailedError,
)
class TemporaryCacheCreateFailedError(CreateFailedError):
message = _("An error occurred while creating the value.")
class TemporaryCacheGetFailedError(CommandException):
message = _("An error occurred while accessing the value.")
class TemporaryCacheDeleteFailedError(DeleteFailedError):
message = _("An error occurred while deleting the value.")
class TemporaryCacheUpdateFailedError(UpdateFailedError):
message = _("An error occurred while updating the value.")
class TemporaryCacheAccessDeniedError(ForbiddenError):
message = _("You don't have permission to modify the value.")

View File

@@ -0,0 +1,46 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from abc import ABC, abstractmethod
from typing import Optional
from sqlalchemy.exc import SQLAlchemyError
from superset.commands.base import BaseCommand
from superset.temporary_cache.commands.exceptions import TemporaryCacheGetFailedError
from superset.temporary_cache.commands.parameters import CommandParameters
logger = logging.getLogger(__name__)
class GetTemporaryCacheCommand(BaseCommand, ABC):
def __init__(self, cmd_params: CommandParameters):
self._cmd_params = cmd_params
def run(self) -> Optional[str]:
try:
return self.get(self._cmd_params)
except SQLAlchemyError as ex:
logger.exception("Error running get command")
raise TemporaryCacheGetFailedError() from ex
def validate(self) -> None:
pass
@abstractmethod
def get(self, cmd_params: CommandParameters) -> Optional[str]:
...

View File

@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from dataclasses import dataclass
from typing import Optional
from flask_appbuilder.security.sqla.models import User
@dataclass
class CommandParameters:
actor: User
resource_id: int
tab_id: Optional[int] = None
key: Optional[str] = None
value: Optional[str] = None

View File

@@ -0,0 +1,48 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from abc import ABC, abstractmethod
from typing import Optional
from sqlalchemy.exc import SQLAlchemyError
from superset.commands.base import BaseCommand
from superset.temporary_cache.commands.exceptions import TemporaryCacheUpdateFailedError
from superset.temporary_cache.commands.parameters import CommandParameters
logger = logging.getLogger(__name__)
class UpdateTemporaryCacheCommand(BaseCommand, ABC):
def __init__(
self, cmd_params: CommandParameters,
):
self._parameters = cmd_params
def run(self) -> Optional[str]:
try:
return self.update(self._parameters)
except SQLAlchemyError as ex:
logger.exception("Error running update command")
raise TemporaryCacheUpdateFailedError() from ex
def validate(self) -> None:
pass
@abstractmethod
def update(self, cmd_params: CommandParameters) -> Optional[str]:
...

View File

@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from marshmallow import fields, Schema
from superset.utils.schema import validate_json
class TemporaryCachePostSchema(Schema):
value = fields.String(
required=True,
allow_none=False,
description="Any type of JSON supported text.",
validate=validate_json,
)
class TemporaryCachePutSchema(Schema):
value = fields.String(
required=True,
allow_none=False,
description="Any type of JSON supported text.",
validate=validate_json,
)

View File

@@ -0,0 +1,28 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from secrets import token_urlsafe
from typing import Any
SEPARATOR = ";"
def cache_key(*args: Any) -> str:
return SEPARATOR.join(str(arg) for arg in args)
def random_key() -> str:
return token_urlsafe(48)