-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Application import and export #1836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,6 @@ | |
from common.swagger_api.common_api import CommonApi | ||
from common.util.common import query_params_to_single_dict | ||
from dataset.serializers.dataset_serializers import DataSetSerializers | ||
from setting.swagger_api.provide_api import ProvideApi | ||
|
||
chat_cache = cache.caches['chat_cache'] | ||
|
||
|
@@ -158,6 +157,34 @@ def put(self, request: Request, application_id: str): | |
data={'application_id': application_id, 'user_id': request.user.id, | ||
'image': request.FILES.get('file')}).edit(request.data)) | ||
|
||
class Import(APIView): | ||
authentication_classes = [TokenAuth] | ||
parser_classes = [MultiPartParser] | ||
|
||
@action(methods="GET", detail=False) | ||
@swagger_auto_schema(operation_summary="导入应用", operation_id="导入应用", | ||
manual_parameters=ApplicationApi.Import.get_request_params_api(), | ||
tags=["应用"] | ||
) | ||
@has_permissions(RoleConstants.ADMIN, RoleConstants.USER) | ||
def post(self, request: Request): | ||
return result.success(ApplicationSerializer.Import( | ||
data={'user_id': request.user.id, 'file': request.FILES.get('file')}).import_()) | ||
|
||
class Export(APIView): | ||
authentication_classes = [TokenAuth] | ||
|
||
@action(methods="GET", detail=False) | ||
@swagger_auto_schema(operation_summary="导出应用", operation_id="导出应用", | ||
manual_parameters=ApplicationApi.Export.get_request_params_api(), | ||
tags=["应用"] | ||
) | ||
@has_permissions(lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.MANAGE, | ||
dynamic_tag=keywords.get('application_id'))) | ||
def get(self, request: Request, application_id: str): | ||
return ApplicationSerializer.Operate( | ||
data={'application_id': application_id, 'user_id': request.user.id}).export() | ||
|
||
class Embed(APIView): | ||
@action(methods=["GET"], detail=False) | ||
@swagger_auto_schema(operation_summary="获取嵌入js", | ||
|
@@ -362,7 +389,8 @@ class AccessToken(APIView): | |
compare=CompareConstants.AND)) | ||
def put(self, request: Request, application_id: str): | ||
return result.success( | ||
ApplicationSerializer.AccessTokenSerializer(data={'application_id': application_id}).edit(request.data)) | ||
ApplicationSerializer.AccessTokenSerializer(data={'application_id': application_id}).edit( | ||
request.data)) | ||
|
||
@action(methods=['GET'], detail=False) | ||
@swagger_auto_schema(operation_summary="获取应用 AccessToken信息", | ||
|
@@ -382,9 +410,10 @@ def get(self, request: Request, application_id: str): | |
class Authentication(APIView): | ||
@action(methods=['OPTIONS'], detail=False) | ||
def options(self, request, *args, **kwargs): | ||
return HttpResponse(headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true", | ||
"Access-Control-Allow-Methods": "POST", | ||
"Access-Control-Allow-Headers": "Origin,Content-Type,Cookie,Accept,Token"}, ) | ||
return HttpResponse( | ||
headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": "true", | ||
"Access-Control-Allow-Methods": "POST", | ||
"Access-Control-Allow-Headers": "Origin,Content-Type,Cookie,Accept,Token"}, ) | ||
|
||
@action(methods=['POST'], detail=False) | ||
@swagger_auto_schema(operation_summary="应用认证", | ||
|
@@ -404,6 +433,7 @@ def post(self, request: Request): | |
) | ||
|
||
@action(methods=['POST'], detail=False) | ||
|
||
@swagger_auto_schema(operation_summary="创建应用", | ||
operation_id="创建应用", | ||
request_body=ApplicationApi.Create.get_request_body_api(), | ||
|
@@ -444,7 +474,8 @@ def get(self, request: Request, application_id: str): | |
"query_text": request.query_params.get("query_text"), | ||
"top_number": request.query_params.get("top_number"), | ||
'similarity': request.query_params.get('similarity'), | ||
'search_mode': request.query_params.get('search_mode')}).hit_test( | ||
'search_mode': request.query_params.get( | ||
'search_mode')}).hit_test( | ||
)) | ||
|
||
class Publish(APIView): | ||
|
@@ -502,7 +533,8 @@ def delete(self, request: Request, application_id: str): | |
compare=CompareConstants.AND)) | ||
def put(self, request: Request, application_id: str): | ||
return result.success( | ||
ApplicationSerializer.Operate(data={'application_id': application_id, 'user_id': request.user.id}).edit( | ||
ApplicationSerializer.Operate( | ||
data={'application_id': application_id, 'user_id': request.user.id}).edit( | ||
request.data)) | ||
|
||
@action(methods=['GET'], detail=False) | ||
|
@@ -528,11 +560,14 @@ class ListApplicationDataSet(APIView): | |
@swagger_auto_schema(operation_summary="获取当前应用可使用的知识库", | ||
operation_id="获取当前应用可使用的知识库", | ||
manual_parameters=ApplicationApi.Operate.get_request_params_api(), | ||
responses=result.get_api_array_response(DataSetSerializers.Query.get_response_body_api()), | ||
responses=result.get_api_array_response( | ||
DataSetSerializers.Query.get_response_body_api()), | ||
tags=['应用']) | ||
@has_permissions(ViewPermission([RoleConstants.ADMIN, RoleConstants.USER], | ||
[lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.USE, | ||
dynamic_tag=keywords.get('application_id'))], | ||
[lambda r, keywords: Permission(group=Group.APPLICATION, | ||
operate=Operate.USE, | ||
dynamic_tag=keywords.get( | ||
'application_id'))], | ||
compare=CompareConstants.AND)) | ||
def get(self, request: Request, application_id: str): | ||
return result.success(ApplicationSerializer.Operate( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 从代码差异来看:
未发现明显问题或潜在问题,但可以提供一些改进建议:
以下是改进建议: from rest_framework.decorators import action, swagger_auto_schema
class SwaggerAutoSchemaMixin:
@classmethod
def auto_schema(cls, method=None, **kwargs):
from drf_yasg.utils import skip_serializer_class
kwargs['_method'] = getattr(method or cls.view_func, '__func__', None)
return super().auto_schema(**kwargs)
class AppAPIView(SwaggerAutoSchemaMixin):
authentication_classes = [TokenAuth]
parser_classes = [MultiPartParser]
# 添加其他 API 视图类 这样可以简化代码,并提高重构的统一性。 |
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码中存在几个问题和建议:
缺少必要的导入语句:
缺少
rest_framework import status
和response
模块的导入。变量名重复:
在同一类中有多个使用相同名称的字段,如
user_id
。这可能会导致混淆或错误。方法命名不规范:
方法名使用了下划线(snake_case),但与 Python 的标准约定不太一致。可以考虑将它们转换为驼峰式命名法(camelCase)。
代码风格一致性:
应该保持代码风格的一致性,例如在函数参数注释前后添加空行。
异常处理:
在
export
方法中,未处理可能抛出的不同类型的所有异常,并返回统一的result.error
响应。以下是改进后的代码示例:
主要改进点:
user_id
改为userIdentifier
ImportDataRequestSerializer
和ExportDataResponseSerializer
) 来更好地组织请求和响应数据。_save_application
和_save_functions
方法来实现具体的数据保存逻辑。这些改进建议有助于提高代码的可读性和可靠性。