You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/01/07 17:33:13 UTC

[GitHub] [superset] dpgaspar commented on a change in pull request #17882: feat: Adds a key-value endpoint to store charts form data

dpgaspar commented on a change in pull request #17882:
URL: https://github.com/apache/superset/pull/17882#discussion_r780399249



##########
File path: superset/charts/form_data/api.py
##########
@@ -0,0 +1,246 @@
+# 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 typing import Type
+
+from flask import Response
+from flask_appbuilder.api import expose, protect, safe
+
+from superset.charts.form_data.commands.create import CreateFormDataCommand
+from superset.charts.form_data.commands.delete import DeleteFormDataCommand
+from superset.charts.form_data.commands.get import GetFormDataCommand
+from superset.charts.form_data.commands.update import UpdateFormDataCommand
+from superset.extensions import event_logger
+from superset.key_value.api import KeyValueRestApi
+
+logger = logging.getLogger(__name__)
+
+
+class ChartFormDataRestApi(KeyValueRestApi):
+    class_permission_name = "ChartFormDataRestApi"
+    resource_name = "chart"
+    openapi_spec_tag = "Chart Form Data"
+
+    def get_create_command(self) -> Type[CreateFormDataCommand]:
+        return CreateFormDataCommand
+
+    def get_update_command(self) -> Type[UpdateFormDataCommand]:
+        return UpdateFormDataCommand
+
+    def get_get_command(self) -> Type[GetFormDataCommand]:
+        return GetFormDataCommand
+
+    def get_delete_command(self) -> Type[DeleteFormDataCommand]:
+        return DeleteFormDataCommand
+
+    @expose("/<int:pk>/form_data", methods=["POST"])
+    @protect()
+    @safe
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
+        log_to_statsd=False,
+    )
+    def post(self, pk: int) -> Response:
+        """Stores a new value.
+        ---
+        post:
+          description: >-
+            Stores a new value.
+          parameters:
+          - in: path
+            schema:
+              type: integer
+            name: pk
+          - in: query
+            schema:
+              type: integer
+            name: dataset
+            required: false
+          requestBody:
+            required: true
+            content:
+              application/json:
+                schema:
+                    type: object

Review comment:
       remove the `type: object` and fix indentation

##########
File path: superset/charts/form_data/commands/delete.py
##########
@@ -0,0 +1,41 @@
+# 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 superset.charts.form_data.utils import check_access
+from superset.extensions import cache_manager
+from superset.key_value.commands.delete import DeleteKeyValueCommand
+from superset.key_value.commands.entry import Entry
+from superset.key_value.commands.exceptions import KeyValueAccessDeniedError
+from superset.key_value.commands.parameters import CommandParameters
+from superset.key_value.utils import cache_key
+
+
+class DeleteFormDataCommand(DeleteKeyValueCommand):
+    def delete(self, cmd_params: CommandParameters) -> bool:
+        resource_id = cmd_params["resource_id"]
+        actor = cmd_params["actor"]
+        key = cmd_params["key"]
+        check_access(cmd_params)
+        entry: Entry = cache_manager.chart_form_data_cache.get(
+            cache_key(resource_id, key)
+        )
+        if entry:
+            if entry["owner"] != actor.get_user_id():
+                raise KeyValueAccessDeniedError()

Review comment:
       would be great to have a test case to cover this path

##########
File path: superset/key_value/api.py
##########
@@ -48,65 +58,113 @@ class KeyValueRestApi(BaseApi, ABC):
     allow_browser_login = True
 
     def add_apispec_components(self, api_spec: APISpec) -> None:
-        api_spec.components.schema(
-            KeyValuePostSchema.__name__, schema=KeyValuePostSchema,
-        )
-        api_spec.components.schema(
-            KeyValuePutSchema.__name__, schema=KeyValuePutSchema,
-        )
+        try:
+            api_spec.components.schema(

Review comment:
       can we instead use `openapi_spec_component_schemas` for this?

##########
File path: superset/charts/form_data/api.py
##########
@@ -0,0 +1,246 @@
+# 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 typing import Type
+
+from flask import Response
+from flask_appbuilder.api import expose, protect, safe
+
+from superset.charts.form_data.commands.create import CreateFormDataCommand
+from superset.charts.form_data.commands.delete import DeleteFormDataCommand
+from superset.charts.form_data.commands.get import GetFormDataCommand
+from superset.charts.form_data.commands.update import UpdateFormDataCommand
+from superset.extensions import event_logger
+from superset.key_value.api import KeyValueRestApi
+
+logger = logging.getLogger(__name__)
+
+
+class ChartFormDataRestApi(KeyValueRestApi):
+    class_permission_name = "ChartFormDataRestApi"
+    resource_name = "chart"
+    openapi_spec_tag = "Chart Form Data"
+
+    def get_create_command(self) -> Type[CreateFormDataCommand]:
+        return CreateFormDataCommand
+
+    def get_update_command(self) -> Type[UpdateFormDataCommand]:
+        return UpdateFormDataCommand
+
+    def get_get_command(self) -> Type[GetFormDataCommand]:
+        return GetFormDataCommand
+
+    def get_delete_command(self) -> Type[DeleteFormDataCommand]:
+        return DeleteFormDataCommand
+
+    @expose("/<int:pk>/form_data", methods=["POST"])
+    @protect()
+    @safe
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
+        log_to_statsd=False,
+    )
+    def post(self, pk: int) -> Response:
+        """Stores a new value.
+        ---
+        post:
+          description: >-
+            Stores a new value.
+          parameters:
+          - in: path
+            schema:
+              type: integer
+            name: pk
+          - in: query
+            schema:
+              type: integer
+            name: dataset
+            required: false
+          requestBody:
+            required: true
+            content:
+              application/json:
+                schema:
+                    type: object
+                    $ref: '#/components/schemas/KeyValuePostSchema'
+          responses:
+            201:
+              description: The value was stored successfully.
+              content:
+                application/json:
+                  schema:
+                    type: object
+                    properties:
+                      key:
+                        type: string
+                        description: The key to retrieve the value.
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        return super().post(pk)
+
+    @expose("/<int:pk>/form_data/<string:key>/", methods=["PUT"])
+    @protect()
+    @safe
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
+        log_to_statsd=False,
+    )
+    def put(self, pk: int, key: str) -> Response:
+        """Updates an existing value.
+        ---
+        put:
+          description: >-
+            Updates an existing value.
+          parameters:
+          - in: path
+            schema:
+              type: integer
+            name: pk
+          - in: path
+            schema:
+              type: string
+            name: key
+          requestBody:
+            required: true
+            content:
+              application/json:
+                schema:
+                    type: object

Review comment:
       same here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org