Add admin settings UI and provider deletes
This commit is contained in:
@@ -38,6 +38,15 @@ def update_provider_account(
|
||||
return success_response(ProvidersService(db).update_account(account_id, payload))
|
||||
|
||||
|
||||
@router.delete("/provider-accounts/{account_id}")
|
||||
def delete_provider_account(
|
||||
account_id: int,
|
||||
_=Depends(require_admin_permission()),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return success_response(ProvidersService(db).delete_account(account_id))
|
||||
|
||||
|
||||
@router.get("/provider-models")
|
||||
def list_provider_models(
|
||||
_=Depends(require_admin_permission()),
|
||||
@@ -64,3 +73,11 @@ def update_provider_model(
|
||||
):
|
||||
return success_response(ProvidersService(db).update_model(model_id, payload))
|
||||
|
||||
|
||||
@router.delete("/provider-models/{model_id}")
|
||||
def delete_provider_model(
|
||||
model_id: int,
|
||||
_=Depends(require_admin_permission()),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return success_response(ProvidersService(db).delete_model(model_id))
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
from sqlalchemy import delete, func, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.common.errors.app_error import NotFoundAppError
|
||||
from app.models.entities import ProviderAccount, ProviderModel
|
||||
from app.common.errors.app_error import BusinessAppError, NotFoundAppError
|
||||
from app.models.entities import (
|
||||
ProviderAccount,
|
||||
ProviderModel,
|
||||
VideoGenerationTask,
|
||||
VideoModelSupplierBinding,
|
||||
)
|
||||
from app.modules.providers.repository import ProvidersRepository
|
||||
|
||||
|
||||
@@ -50,6 +56,43 @@ class ProvidersService:
|
||||
self.db.commit()
|
||||
return self.serialize_account(item)
|
||||
|
||||
def delete_account(self, account_id: int) -> dict:
|
||||
item = self.repository.get_account(account_id)
|
||||
if not item:
|
||||
raise NotFoundAppError("provider account not found", code=60001)
|
||||
|
||||
model_ids = list(
|
||||
self.db.scalars(
|
||||
select(ProviderModel.id).where(ProviderModel.provider_account_id == account_id)
|
||||
)
|
||||
)
|
||||
task_conditions = [VideoGenerationTask.provider_account_id == account_id]
|
||||
if model_ids:
|
||||
task_conditions.append(VideoGenerationTask.provider_model_id.in_(model_ids))
|
||||
task_count = self.db.scalar(
|
||||
select(func.count())
|
||||
.select_from(VideoGenerationTask)
|
||||
.where(or_(*task_conditions))
|
||||
)
|
||||
if task_count:
|
||||
raise BusinessAppError(
|
||||
"该供应商已有任务记录,不能直接删除,请先停用。",
|
||||
code=60011,
|
||||
)
|
||||
|
||||
if model_ids:
|
||||
self.db.execute(
|
||||
delete(VideoModelSupplierBinding).where(
|
||||
VideoModelSupplierBinding.provider_model_id.in_(model_ids)
|
||||
)
|
||||
)
|
||||
self.db.execute(
|
||||
delete(ProviderModel).where(ProviderModel.provider_account_id == account_id)
|
||||
)
|
||||
self.db.delete(item)
|
||||
self.db.commit()
|
||||
return {"id": account_id, "deleted": True}
|
||||
|
||||
def list_models(self) -> list[dict]:
|
||||
accounts = {item.id: item for item in self.repository.list_accounts().all()}
|
||||
return [self.serialize_model(item, accounts) for item in self.repository.list_models().all()]
|
||||
@@ -72,6 +115,31 @@ class ProvidersService:
|
||||
account = self.repository.get_account(item.provider_account_id)
|
||||
return self.serialize_model(item, {account.id: account} if account else {})
|
||||
|
||||
def delete_model(self, model_id: int) -> dict:
|
||||
item = self.repository.get_model(model_id)
|
||||
if not item:
|
||||
raise NotFoundAppError("provider model not found", code=60002)
|
||||
|
||||
task_count = self.db.scalar(
|
||||
select(func.count())
|
||||
.select_from(VideoGenerationTask)
|
||||
.where(VideoGenerationTask.provider_model_id == model_id)
|
||||
)
|
||||
if task_count:
|
||||
raise BusinessAppError(
|
||||
"该模型已有任务记录,不能直接删除,请先停用。",
|
||||
code=60012,
|
||||
)
|
||||
|
||||
self.db.execute(
|
||||
delete(VideoModelSupplierBinding).where(
|
||||
VideoModelSupplierBinding.provider_model_id == model_id
|
||||
)
|
||||
)
|
||||
self.db.delete(item)
|
||||
self.db.commit()
|
||||
return {"id": model_id, "deleted": True}
|
||||
|
||||
@staticmethod
|
||||
def serialize_account(item: ProviderAccount) -> dict:
|
||||
return {
|
||||
@@ -109,4 +177,3 @@ class ProvidersService:
|
||||
"defaultResolution": item.default_resolution,
|
||||
"status": item.status,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user