refactor: split changes for api/controllers/console/explore/installed… (#29891)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
Asuka Minato 2025-12-22 10:40:32 +09:00 committed by GitHub
parent f8ccc75cde
commit 4cf65f0137
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 13 deletions

View File

@ -2,7 +2,8 @@ import logging
from typing import Any from typing import Any
from flask import request from flask import request
from flask_restx import Resource, inputs, marshal_with, reqparse from flask_restx import Resource, marshal_with
from pydantic import BaseModel
from sqlalchemy import and_, select from sqlalchemy import and_, select
from werkzeug.exceptions import BadRequest, Forbidden, NotFound from werkzeug.exceptions import BadRequest, Forbidden, NotFound
@ -18,6 +19,15 @@ from services.account_service import TenantService
from services.enterprise.enterprise_service import EnterpriseService from services.enterprise.enterprise_service import EnterpriseService
from services.feature_service import FeatureService from services.feature_service import FeatureService
class InstalledAppCreatePayload(BaseModel):
app_id: str
class InstalledAppUpdatePayload(BaseModel):
is_pinned: bool | None = None
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -105,26 +115,25 @@ class InstalledAppsListApi(Resource):
@account_initialization_required @account_initialization_required
@cloud_edition_billing_resource_check("apps") @cloud_edition_billing_resource_check("apps")
def post(self): def post(self):
parser = reqparse.RequestParser().add_argument("app_id", type=str, required=True, help="Invalid app_id") payload = InstalledAppCreatePayload.model_validate(console_ns.payload or {})
args = parser.parse_args()
recommended_app = db.session.query(RecommendedApp).where(RecommendedApp.app_id == args["app_id"]).first() recommended_app = db.session.query(RecommendedApp).where(RecommendedApp.app_id == payload.app_id).first()
if recommended_app is None: if recommended_app is None:
raise NotFound("App not found") raise NotFound("Recommended app not found")
_, current_tenant_id = current_account_with_tenant() _, current_tenant_id = current_account_with_tenant()
app = db.session.query(App).where(App.id == args["app_id"]).first() app = db.session.query(App).where(App.id == payload.app_id).first()
if app is None: if app is None:
raise NotFound("App not found") raise NotFound("App entity not found")
if not app.is_public: if not app.is_public:
raise Forbidden("You can't install a non-public app") raise Forbidden("You can't install a non-public app")
installed_app = ( installed_app = (
db.session.query(InstalledApp) db.session.query(InstalledApp)
.where(and_(InstalledApp.app_id == args["app_id"], InstalledApp.tenant_id == current_tenant_id)) .where(and_(InstalledApp.app_id == payload.app_id, InstalledApp.tenant_id == current_tenant_id))
.first() .first()
) )
@ -133,7 +142,7 @@ class InstalledAppsListApi(Resource):
recommended_app.install_count += 1 recommended_app.install_count += 1
new_installed_app = InstalledApp( new_installed_app = InstalledApp(
app_id=args["app_id"], app_id=payload.app_id,
tenant_id=current_tenant_id, tenant_id=current_tenant_id,
app_owner_tenant_id=app.tenant_id, app_owner_tenant_id=app.tenant_id,
is_pinned=False, is_pinned=False,
@ -163,12 +172,11 @@ class InstalledAppApi(InstalledAppResource):
return {"result": "success", "message": "App uninstalled successfully"}, 204 return {"result": "success", "message": "App uninstalled successfully"}, 204
def patch(self, installed_app): def patch(self, installed_app):
parser = reqparse.RequestParser().add_argument("is_pinned", type=inputs.boolean) payload = InstalledAppUpdatePayload.model_validate(console_ns.payload or {})
args = parser.parse_args()
commit_args = False commit_args = False
if "is_pinned" in args: if payload.is_pinned is not None:
installed_app.is_pinned = args["is_pinned"] installed_app.is_pinned = payload.is_pinned
commit_args = True commit_args = True
if commit_args: if commit_args: