aminghadersohi commented on code in PR #40350: URL: https://github.com/apache/superset/pull/40350#discussion_r3328088364
########## superset/mcp_service/css_template/schemas.py: ########## @@ -0,0 +1,108 @@ +# 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. + +"""Pydantic schemas for CSS template MCP tools.""" + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateCssTemplateRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + template_name: str = Field( + ..., + min_length=1, + max_length=250, + description="Name for the CSS template.", + ) + css: str = Field( + ..., + description="CSS content for the template.", + ) + + @field_validator("template_name") + @classmethod + def template_name_must_not_be_empty(cls, v: str) -> str: + if not v.strip(): + raise ValueError("template_name must not be empty") + return v.strip() + + +class CreateCssTemplateResponse(BaseModel): + """Response schema for create_css_template.""" + + id: int | None = Field( + None, + description="ID of the created CSS template. None if creation failed.", + ) + template_name: str | None = Field( + None, + description="Name of the created CSS template.", + ) + css: str | None = Field( + None, + description="CSS content of the created template.", + ) + error: str | None = Field( + None, + description="Error message if creation failed, otherwise null.", + ) Review Comment: Fixed — added a `@field_validator("error")` `sanitize_error_for_llm_context` method to `CreateCssTemplateResponse` that calls `sanitize_for_llm_context(value, field_path=("error",))`, consistent with the pattern used in `DashboardError`, `CssTemplateError`, and other MCP response schemas. ########## superset/mcp_service/css_template/schemas.py: ########## @@ -0,0 +1,108 @@ +# 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. + +"""Pydantic schemas for CSS template MCP tools.""" + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateCssTemplateRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + template_name: str = Field( + ..., + min_length=1, + max_length=250, + description="Name for the CSS template.", + ) + css: str = Field( + ..., + description="CSS content for the template.", + ) + + @field_validator("template_name") + @classmethod + def template_name_must_not_be_empty(cls, v: str) -> str: + if not v.strip(): + raise ValueError("template_name must not be empty") + return v.strip() + + +class CreateCssTemplateResponse(BaseModel): + """Response schema for create_css_template.""" + + id: int | None = Field( + None, + description="ID of the created CSS template. None if creation failed.", + ) + template_name: str | None = Field( + None, + description="Name of the created CSS template.", + ) + css: str | None = Field( + None, + description="CSS content of the created template.", + ) + error: str | None = Field( + None, + description="Error message if creation failed, otherwise null.", + ) + + +class UpdateCssTemplateRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + id: int = Field(..., description="ID of the CSS template to update.") + template_name: str | None = Field( + None, + max_length=250, + description="New name for the CSS template.", + ) + css: str | None = Field( + None, + description="New CSS content for the template.", + ) + + @field_validator("template_name") + @classmethod + def template_name_must_not_be_empty(cls, v: str | None) -> str | None: + if v is not None: + if not v.strip(): + raise ValueError("template_name must not be empty") + return v.strip() + return v + + +class UpdateCssTemplateResponse(BaseModel): + """Response schema for update_css_template.""" + + id: int | None = Field( + None, + description="ID of the updated CSS template. None if update failed.", + ) + template_name: str | None = Field( + None, + description="Name of the updated CSS template.", + ) + css: str | None = Field( + None, + description="CSS content of the updated template.", + ) + error: str | None = Field( + None, + description="Error message if update failed, otherwise null.", + ) Review Comment: Fixed — same `sanitize_error_for_llm_context` validator added to `UpdateCssTemplateResponse.error`. ########## superset/commands/css/update.py: ########## @@ -0,0 +1,53 @@ +# 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 functools import partial +from typing import Any + +from superset.commands.base import BaseCommand +from superset.commands.css.exceptions import ( + CssTemplateInvalidError, + CssTemplateNotFoundError, + CssTemplateUpdateFailedError, +) +from superset.daos.css import CssTemplateDAO +from superset.models.core import CssTemplate +from superset.utils.decorators import on_error, transaction + +logger = logging.getLogger(__name__) + + +class UpdateCssTemplateCommand(BaseCommand): + def __init__(self, model_id: int, properties: dict[str, Any]): + self._model_id = model_id + self._properties = properties + self._model: CssTemplate | None = None + + @transaction(on_error=partial(on_error, reraise=CssTemplateUpdateFailedError)) + def run(self) -> CssTemplate: + self.validate() + assert self._model + return CssTemplateDAO.update(self._model, attributes=self._properties) + + def validate(self) -> None: + self._model = CssTemplateDAO.find_by_id(self._model_id) + if not self._model: + raise CssTemplateNotFoundError() + template_name = self._properties.get("template_name") + if template_name is not None and not template_name.strip(): + raise CssTemplateInvalidError() Review Comment: Not implementing this suggestion — `UpdateCssTemplateCommand` intentionally allows partial updates: a user may rename a template without changing its CSS, or update the CSS without renaming it. The MCP tool already enforces that at least one field must be provided (`if not properties: return UpdateCssTemplateResponse(error="At least one of template_name or css must be provided.")`) before the command is even invoked. Requiring CSS on every update would break valid rename-only calls. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
