mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
chore(command): Organize Commands according to SIP-92 (#25850)
This commit is contained in:
@@ -20,18 +20,18 @@ from flask import request, Response
|
||||
from flask_appbuilder.api import expose, protect, safe
|
||||
from marshmallow import ValidationError
|
||||
|
||||
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP
|
||||
from superset.explore.form_data.commands.create import CreateFormDataCommand
|
||||
from superset.explore.form_data.commands.delete import DeleteFormDataCommand
|
||||
from superset.explore.form_data.commands.get import GetFormDataCommand
|
||||
from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.update import UpdateFormDataCommand
|
||||
from superset.explore.form_data.schemas import FormDataPostSchema, FormDataPutSchema
|
||||
from superset.extensions import event_logger
|
||||
from superset.temporary_cache.commands.exceptions import (
|
||||
from superset.commands.explore.form_data.create import CreateFormDataCommand
|
||||
from superset.commands.explore.form_data.delete import DeleteFormDataCommand
|
||||
from superset.commands.explore.form_data.get import GetFormDataCommand
|
||||
from superset.commands.explore.form_data.parameters import CommandParameters
|
||||
from superset.commands.explore.form_data.update import UpdateFormDataCommand
|
||||
from superset.commands.temporary_cache.exceptions import (
|
||||
TemporaryCacheAccessDeniedError,
|
||||
TemporaryCacheResourceNotFoundError,
|
||||
)
|
||||
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP
|
||||
from superset.explore.form_data.schemas import FormDataPostSchema, FormDataPutSchema
|
||||
from superset.extensions import event_logger
|
||||
from superset.views.base_api import BaseSupersetApi, requires_json, statsd_metrics
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
# 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.
|
||||
@@ -1,72 +0,0 @@
|
||||
# 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 flask import session
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from superset.commands.base import BaseCommand
|
||||
from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.state import TemporaryExploreState
|
||||
from superset.explore.form_data.commands.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
|
||||
from superset.utils.core import DatasourceType, get_user_id
|
||||
from superset.utils.schema import validate_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CreateFormDataCommand(BaseCommand):
|
||||
def __init__(self, cmd_params: CommandParameters):
|
||||
self._cmd_params = cmd_params
|
||||
|
||||
def run(self) -> str:
|
||||
self.validate()
|
||||
try:
|
||||
datasource_id = self._cmd_params.datasource_id
|
||||
datasource_type = self._cmd_params.datasource_type
|
||||
chart_id = self._cmd_params.chart_id
|
||||
tab_id = self._cmd_params.tab_id
|
||||
form_data = self._cmd_params.form_data
|
||||
check_access(datasource_id, chart_id, datasource_type)
|
||||
contextual_key = cache_key(
|
||||
session.get("_id"), tab_id, datasource_id, chart_id, datasource_type
|
||||
)
|
||||
key = cache_manager.explore_form_data_cache.get(contextual_key)
|
||||
if not key or not tab_id:
|
||||
key = random_key()
|
||||
if form_data:
|
||||
state: TemporaryExploreState = {
|
||||
"owner": get_user_id(),
|
||||
"datasource_id": datasource_id,
|
||||
"datasource_type": DatasourceType(datasource_type),
|
||||
"chart_id": chart_id,
|
||||
"form_data": form_data,
|
||||
}
|
||||
cache_manager.explore_form_data_cache.set(key, state)
|
||||
cache_manager.explore_form_data_cache.set(contextual_key, key)
|
||||
return key
|
||||
except SQLAlchemyError as ex:
|
||||
logger.exception("Error running create command")
|
||||
raise TemporaryCacheCreateFailedError() from ex
|
||||
|
||||
def validate(self) -> None:
|
||||
if self._cmd_params.form_data:
|
||||
validate_json(self._cmd_params.form_data)
|
||||
@@ -1,68 +0,0 @@
|
||||
# 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
|
||||
from typing import Optional
|
||||
|
||||
from flask import session
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from superset.commands.base import BaseCommand
|
||||
from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.state import TemporaryExploreState
|
||||
from superset.explore.form_data.commands.utils import check_access
|
||||
from superset.extensions import cache_manager
|
||||
from superset.temporary_cache.commands.exceptions import (
|
||||
TemporaryCacheAccessDeniedError,
|
||||
TemporaryCacheDeleteFailedError,
|
||||
)
|
||||
from superset.temporary_cache.utils import cache_key
|
||||
from superset.utils.core import DatasourceType, get_user_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DeleteFormDataCommand(BaseCommand, ABC):
|
||||
def __init__(self, cmd_params: CommandParameters):
|
||||
self._cmd_params = cmd_params
|
||||
|
||||
def run(self) -> bool:
|
||||
try:
|
||||
key = self._cmd_params.key
|
||||
state: TemporaryExploreState = cache_manager.explore_form_data_cache.get(
|
||||
key
|
||||
)
|
||||
if state:
|
||||
datasource_id: int = state["datasource_id"]
|
||||
chart_id: Optional[int] = state["chart_id"]
|
||||
datasource_type = DatasourceType(state["datasource_type"])
|
||||
check_access(datasource_id, chart_id, datasource_type)
|
||||
if state["owner"] != get_user_id():
|
||||
raise TemporaryCacheAccessDeniedError()
|
||||
tab_id = self._cmd_params.tab_id
|
||||
contextual_key = cache_key(
|
||||
session.get("_id"), tab_id, datasource_id, chart_id, datasource_type
|
||||
)
|
||||
cache_manager.explore_form_data_cache.delete(contextual_key)
|
||||
return cache_manager.explore_form_data_cache.delete(key)
|
||||
return False
|
||||
except SQLAlchemyError as ex:
|
||||
logger.exception("Error running delete command")
|
||||
raise TemporaryCacheDeleteFailedError() from ex
|
||||
|
||||
def validate(self) -> None:
|
||||
pass
|
||||
@@ -1,62 +0,0 @@
|
||||
# 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
|
||||
from typing import Optional
|
||||
|
||||
from flask import current_app as app
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from superset.commands.base import BaseCommand
|
||||
from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.state import TemporaryExploreState
|
||||
from superset.explore.form_data.commands.utils import check_access
|
||||
from superset.extensions import cache_manager
|
||||
from superset.temporary_cache.commands.exceptions import TemporaryCacheGetFailedError
|
||||
from superset.utils.core import DatasourceType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GetFormDataCommand(BaseCommand, ABC):
|
||||
def __init__(self, cmd_params: CommandParameters) -> None:
|
||||
self._cmd_params = cmd_params
|
||||
config = app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"]
|
||||
self._refresh_timeout = config.get("REFRESH_TIMEOUT_ON_RETRIEVAL")
|
||||
|
||||
def run(self) -> Optional[str]:
|
||||
try:
|
||||
key = self._cmd_params.key
|
||||
state: TemporaryExploreState = cache_manager.explore_form_data_cache.get(
|
||||
key
|
||||
)
|
||||
if state:
|
||||
check_access(
|
||||
state["datasource_id"],
|
||||
state["chart_id"],
|
||||
DatasourceType(state["datasource_type"]),
|
||||
)
|
||||
if self._refresh_timeout:
|
||||
cache_manager.explore_form_data_cache.set(key, state)
|
||||
return state["form_data"]
|
||||
return None
|
||||
except SQLAlchemyError as ex:
|
||||
logger.exception("Error running get command")
|
||||
raise TemporaryCacheGetFailedError() from ex
|
||||
|
||||
def validate(self) -> None:
|
||||
pass
|
||||
@@ -1,30 +0,0 @@
|
||||
# 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 superset.utils.core import DatasourceType
|
||||
|
||||
|
||||
@dataclass
|
||||
class CommandParameters:
|
||||
datasource_type: DatasourceType = DatasourceType.TABLE
|
||||
datasource_id: int = 0
|
||||
chart_id: int = 0
|
||||
tab_id: Optional[int] = None
|
||||
key: Optional[str] = None
|
||||
form_data: Optional[str] = None
|
||||
@@ -1,27 +0,0 @@
|
||||
# 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 import Optional, TypedDict
|
||||
|
||||
from superset.utils.core import DatasourceType
|
||||
|
||||
|
||||
class TemporaryExploreState(TypedDict):
|
||||
owner: Optional[int]
|
||||
datasource_id: int
|
||||
datasource_type: DatasourceType
|
||||
chart_id: Optional[int]
|
||||
form_data: str
|
||||
@@ -1,90 +0,0 @@
|
||||
# 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
|
||||
from typing import Optional
|
||||
|
||||
from flask import session
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from superset.commands.base import BaseCommand
|
||||
from superset.explore.form_data.commands.parameters import CommandParameters
|
||||
from superset.explore.form_data.commands.state import TemporaryExploreState
|
||||
from superset.explore.form_data.commands.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
|
||||
from superset.utils.core import DatasourceType, get_user_id
|
||||
from superset.utils.schema import validate_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class UpdateFormDataCommand(BaseCommand, ABC):
|
||||
def __init__(
|
||||
self,
|
||||
cmd_params: CommandParameters,
|
||||
):
|
||||
self._cmd_params = cmd_params
|
||||
|
||||
def run(self) -> Optional[str]:
|
||||
self.validate()
|
||||
try:
|
||||
datasource_id = self._cmd_params.datasource_id
|
||||
chart_id = self._cmd_params.chart_id
|
||||
datasource_type = self._cmd_params.datasource_type
|
||||
key = self._cmd_params.key
|
||||
form_data = self._cmd_params.form_data
|
||||
check_access(datasource_id, chart_id, datasource_type)
|
||||
state: TemporaryExploreState = cache_manager.explore_form_data_cache.get(
|
||||
key
|
||||
)
|
||||
owner = get_user_id()
|
||||
if state and form_data:
|
||||
if state["owner"] != owner:
|
||||
raise TemporaryCacheAccessDeniedError()
|
||||
|
||||
# Generate a new key if tab_id changes or equals 0
|
||||
tab_id = self._cmd_params.tab_id
|
||||
contextual_key = cache_key(
|
||||
session.get("_id"), tab_id, datasource_id, chart_id, datasource_type
|
||||
)
|
||||
key = cache_manager.explore_form_data_cache.get(contextual_key)
|
||||
if not key or not tab_id:
|
||||
key = random_key()
|
||||
cache_manager.explore_form_data_cache.set(contextual_key, key)
|
||||
|
||||
new_state: TemporaryExploreState = {
|
||||
"owner": owner,
|
||||
"datasource_id": datasource_id,
|
||||
"datasource_type": DatasourceType(datasource_type),
|
||||
"chart_id": chart_id,
|
||||
"form_data": form_data,
|
||||
}
|
||||
cache_manager.explore_form_data_cache.set(key, new_state)
|
||||
return key
|
||||
except SQLAlchemyError as ex:
|
||||
logger.exception("Error running update command")
|
||||
raise TemporaryCacheUpdateFailedError() from ex
|
||||
|
||||
def validate(self) -> None:
|
||||
if self._cmd_params.form_data:
|
||||
validate_json(self._cmd_params.form_data)
|
||||
@@ -1,45 +0,0 @@
|
||||
# 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 import Optional
|
||||
|
||||
from superset.charts.commands.exceptions import (
|
||||
ChartAccessDeniedError,
|
||||
ChartNotFoundError,
|
||||
)
|
||||
from superset.datasets.commands.exceptions import (
|
||||
DatasetAccessDeniedError,
|
||||
DatasetNotFoundError,
|
||||
)
|
||||
from superset.explore.utils import check_access as explore_check_access
|
||||
from superset.temporary_cache.commands.exceptions import (
|
||||
TemporaryCacheAccessDeniedError,
|
||||
TemporaryCacheResourceNotFoundError,
|
||||
)
|
||||
from superset.utils.core import DatasourceType
|
||||
|
||||
|
||||
def check_access(
|
||||
datasource_id: int,
|
||||
chart_id: Optional[int],
|
||||
datasource_type: DatasourceType,
|
||||
) -> None:
|
||||
try:
|
||||
explore_check_access(datasource_id, chart_id, datasource_type)
|
||||
except (ChartNotFoundError, DatasetNotFoundError) as ex:
|
||||
raise TemporaryCacheResourceNotFoundError from ex
|
||||
except (ChartAccessDeniedError, DatasetAccessDeniedError) as ex:
|
||||
raise TemporaryCacheAccessDeniedError from ex
|
||||
Reference in New Issue
Block a user