2025-04-17 10:01:33 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: MaxKB
|
|
|
|
|
|
@Author:虎虎
|
|
|
|
|
|
@file: user.py
|
|
|
|
|
|
@date:2025/4/14 19:25
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
from drf_spectacular.utils import extend_schema
|
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
from rest_framework.request import Request
|
|
|
|
|
|
|
|
|
|
|
|
from common.auth import TokenAuth
|
|
|
|
|
|
from common.auth.authentication import has_permissions
|
|
|
|
|
|
from common.constants.permission_constants import PermissionConstants
|
2025-04-28 09:36:56 +00:00
|
|
|
|
from common.result import result
|
2025-04-18 09:45:15 +00:00
|
|
|
|
from common.utils.common import query_params_to_single_dict
|
2025-04-21 02:24:13 +00:00
|
|
|
|
from models_provider.api.model import ModelCreateAPI, GetModelApi, ModelEditApi, ModelListResponse, DefaultModelResponse
|
2025-04-18 09:45:15 +00:00
|
|
|
|
from models_provider.api.provide import ProvideApi
|
|
|
|
|
|
from models_provider.serializers.model_serializer import ModelSerializer
|
2025-04-17 10:01:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Model(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['POST'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_("Create model"),
|
2025-04-17 10:01:33 +00:00
|
|
|
|
description=_("Create model"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Create model"), # type: ignore
|
|
|
|
|
|
tags=[_("Model")], # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=ModelCreateAPI.get_query_params_api(),
|
2025-04-17 10:01:33 +00:00
|
|
|
|
request=ModelCreateAPI.get_request(),
|
|
|
|
|
|
responses=ModelCreateAPI.get_response())
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_CREATE.get_workspace_permission())
|
2025-04-17 10:01:33 +00:00
|
|
|
|
def post(self, request: Request, workspace_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Create(data={**request.data, 'user_id': request.user.id}).insert(workspace_id,
|
|
|
|
|
|
with_valid=True))
|
2025-04-18 09:45:15 +00:00
|
|
|
|
|
|
|
|
|
|
# @extend_schema(methods=['PUT'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
# summary=_('Update model'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
# operation_id=_('Update model'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
# request=ModelEditApi.get_request(),
|
|
|
|
|
|
# responses=ModelCreateApi.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
# tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
# @has_permissions(PermissionConstants.MODEL_CREATE)
|
|
|
|
|
|
# def put(self, request: Request):
|
|
|
|
|
|
# return result.success(
|
|
|
|
|
|
# ModelSerializer.Create(data={**request.data, 'user_id': str(request.user.id)}).insert(request.user.id,
|
|
|
|
|
|
# with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Query model list'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Query model list'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Query model list'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=ModelCreateAPI.get_query_params_api(),
|
|
|
|
|
|
responses=ModelListResponse.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_READ.get_workspace_permission())
|
2025-04-21 01:10:06 +00:00
|
|
|
|
def get(self, request: Request, workspace_id: str):
|
2025-04-18 09:45:15 +00:00
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Query(
|
2025-04-21 01:10:06 +00:00
|
|
|
|
data={**query_params_to_single_dict(request.query_params)}).list(workspace_id=workspace_id,
|
|
|
|
|
|
with_valid=True))
|
2025-04-18 09:45:15 +00:00
|
|
|
|
|
|
|
|
|
|
class Operate(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['PUT'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Update model'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Update model'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Update model'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
request=ModelEditApi.get_request(),
|
|
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
|
|
|
|
|
responses=ModelEditApi.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_EDIT.get_workspace_permission())
|
|
|
|
|
|
def put(self, request: Request, workspace_id, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Operate(data={'id': model_id, 'user_id': request.user.id}).edit(request.data,
|
|
|
|
|
|
str(request.user.id)))
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['DELETE'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Delete model'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Delete model'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Delete model'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
2025-04-21 02:24:13 +00:00
|
|
|
|
responses=DefaultModelResponse.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_DELETE.get_workspace_permission())
|
|
|
|
|
|
def delete(self, request: Request, workspace_id: str, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Operate(data={'id': model_id, 'user_id': request.user.id}).delete())
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Query model details'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Query model details'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Query model details'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
|
|
|
|
|
responses=GetModelApi.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_READ.get_workspace_permission())
|
|
|
|
|
|
def get(self, request: Request, workspace_id: str, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Operate(data={'id': model_id, 'user_id': request.user.id}).one(with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
class ModelParamsForm(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Get model parameter form'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Get model parameter form'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Get model parameter form'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
|
|
|
|
|
responses=ProvideApi.ModelParamsForm.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_READ.get_workspace_permission())
|
|
|
|
|
|
def get(self, request: Request, workspace_id: str, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.ModelParams(data={'id': model_id}).get_model_params())
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['PUT'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Save model parameter form'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Save model parameter form'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Save model parameter form'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
2025-04-24 05:42:58 +00:00
|
|
|
|
request=GetModelApi.get_request(),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
responses=ProvideApi.ModelParamsForm.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_READ.get_workspace_permission())
|
|
|
|
|
|
def put(self, request: Request, workspace_id: str, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.ModelParams(data={'id': model_id}).save_model_params_form(request.data))
|
|
|
|
|
|
|
|
|
|
|
|
class ModelMeta(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_(
|
|
|
|
|
|
'Query model meta information, this interface does not carry authentication information'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_(
|
|
|
|
|
|
'Query model meta information, this interface does not carry authentication information'),
|
|
|
|
|
|
operation_id=_(
|
|
|
|
|
|
'Query model meta information, this interface does not carry authentication information'),
|
|
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
|
|
|
|
|
responses=GetModelApi.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_READ.get_workspace_permission())
|
|
|
|
|
|
def get(self, request: Request, workspace_id: str, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Operate(data={'id': model_id}).one_meta(with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
class PauseDownload(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['PUT'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_('Pause model download'),
|
2025-04-18 09:45:15 +00:00
|
|
|
|
description=_('Pause model download'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Pause model download'), # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
parameters=GetModelApi.get_query_params_api(),
|
2025-04-24 05:42:58 +00:00
|
|
|
|
request=GetModelApi.get_request(),
|
2025-04-21 02:24:13 +00:00
|
|
|
|
responses=DefaultModelResponse.get_response(),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
tags=[_('Model')]) # type: ignore
|
2025-04-18 09:45:15 +00:00
|
|
|
|
@has_permissions(PermissionConstants.MODEL_CREATE.get_workspace_permission())
|
|
|
|
|
|
def put(self, request: Request, workspace_id: str, model_id: str):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
ModelSerializer.Operate(data={'id': model_id}).pause_download())
|