Files
superset2/superset/temporary_cache/api.py
Ville Brofeldt b7a0559aaf 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
2022-03-17 01:15:52 +02:00

172 lines
6.2 KiB
Python

# 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:
...