-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: application version #3194
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: MaxKB | ||
@Author:虎虎 | ||
@file: application_version.py | ||
@date:2025/6/4 17:33 | ||
@desc: | ||
""" | ||
from drf_spectacular.types import OpenApiTypes | ||
from drf_spectacular.utils import OpenApiParameter | ||
|
||
from application.serializers.application_version import ApplicationVersionModelSerializer | ||
from common.mixins.api_mixin import APIMixin | ||
from common.result import ResultSerializer, PageDataResponse, ResultPageSerializer | ||
|
||
|
||
class ApplicationListVersionResult(ResultSerializer): | ||
def get_data(self): | ||
return ApplicationVersionModelSerializer(many=True) | ||
|
||
|
||
class ApplicationPageVersionResult(ResultPageSerializer): | ||
def get_data(self): | ||
return ApplicationVersionModelSerializer(many=True) | ||
|
||
|
||
class ApplicationWorkflowVersionResult(ResultSerializer): | ||
def get_data(self): | ||
return ApplicationVersionModelSerializer() | ||
|
||
|
||
class ApplicationVersionAPI(APIMixin): | ||
@staticmethod | ||
def get_parameters(): | ||
return [ | ||
OpenApiParameter( | ||
name="workspace_id", | ||
description="工作空间id", | ||
type=OpenApiTypes.STR, | ||
location='path', | ||
required=True, | ||
), | ||
OpenApiParameter( | ||
name="application_id", | ||
description="application ID", | ||
type=OpenApiTypes.STR, | ||
location='path', | ||
required=True, | ||
) | ||
] | ||
|
||
|
||
class ApplicationVersionOperateAPI(APIMixin): | ||
@staticmethod | ||
def get_parameters(): | ||
return [ | ||
OpenApiParameter( | ||
name="work_flow_version_id", | ||
description="工作流版本id", | ||
type=OpenApiTypes.STR, | ||
location='path', | ||
required=True, | ||
) | ||
, *ApplicationVersionAPI.get_parameters() | ||
] | ||
|
||
@staticmethod | ||
def get_response(): | ||
return ApplicationWorkflowVersionResult | ||
|
||
|
||
class ApplicationVersionListAPI(APIMixin): | ||
@staticmethod | ||
def get_parameters(): | ||
return [ | ||
OpenApiParameter( | ||
name="name", | ||
description="Version Name", | ||
type=OpenApiTypes.STR, | ||
required=False, | ||
) | ||
, *ApplicationVersionAPI.get_parameters()] | ||
|
||
@staticmethod | ||
def get_response(): | ||
return ApplicationListVersionResult | ||
|
||
|
||
class ApplicationVersionPageAPI(APIMixin): | ||
@staticmethod | ||
def get_parameters(): | ||
return ApplicationVersionListAPI.get_parameters() | ||
|
||
@staticmethod | ||
def get_response(): | ||
return ApplicationPageVersionResult | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Generated by Django 5.2 on 2025-06-04 11:57 | ||
|
||
import application.models.application_chat | ||
import common.encoder.encoder | ||
import django.contrib.postgres.fields | ||
import django.db.models.deletion | ||
import uuid | ||
import uuid_utils.compat | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('application', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Chat', | ||
fields=[ | ||
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), | ||
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')), | ||
('id', models.UUIDField(default=uuid.UUID('01973acd-fe4c-7fd1-94a8-f7cd668de562'), editable=False, primary_key=True, serialize=False, verbose_name='主键id')), | ||
('abstract', models.CharField(max_length=1024, verbose_name='摘要')), | ||
('asker', models.JSONField(default=application.models.application_chat.default_asker, encoder=common.encoder.encoder.SystemEncoder, verbose_name='访问者')), | ||
('client_id', models.UUIDField(default=None, null=True, verbose_name='客户端id')), | ||
('is_deleted', models.BooleanField(default=False, verbose_name='')), | ||
('application', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='application.application')), | ||
], | ||
options={ | ||
'db_table': 'application_chat', | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='ChatRecord', | ||
fields=[ | ||
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), | ||
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')), | ||
('id', models.UUIDField(default=uuid_utils.compat.uuid1, editable=False, primary_key=True, serialize=False, verbose_name='主键id')), | ||
('vote_status', models.CharField(choices=[('-1', '未投票'), ('0', '赞同'), ('1', '反对')], default='-1', max_length=10, verbose_name='投票')), | ||
('problem_text', models.CharField(max_length=10240, verbose_name='问题')), | ||
('answer_text', models.CharField(max_length=40960, verbose_name='答案')), | ||
('answer_text_list', django.contrib.postgres.fields.ArrayField(base_field=models.JSONField(), default=list, size=None, verbose_name='改进标注列表')), | ||
('message_tokens', models.IntegerField(default=0, verbose_name='请求token数量')), | ||
('answer_tokens', models.IntegerField(default=0, verbose_name='响应token数量')), | ||
('const', models.IntegerField(default=0, verbose_name='总费用')), | ||
('details', models.JSONField(default=dict, encoder=common.encoder.encoder.SystemEncoder, verbose_name='对话详情')), | ||
('improve_paragraph_id_list', django.contrib.postgres.fields.ArrayField(base_field=models.UUIDField(blank=True), default=list, size=None, verbose_name='改进标注列表')), | ||
('run_time', models.FloatField(default=0, verbose_name='运行时长')), | ||
('index', models.IntegerField(verbose_name='对话下标')), | ||
('chat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='application.chat')), | ||
], | ||
options={ | ||
'db_table': 'application_chat_record', | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='WorkFlowVersion', | ||
fields=[ | ||
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), | ||
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')), | ||
('id', models.UUIDField(default=uuid_utils.compat.uuid1, editable=False, primary_key=True, serialize=False, verbose_name='主键id')), | ||
('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')), | ||
('name', models.CharField(default='', max_length=128, verbose_name='版本名称')), | ||
('publish_user_id', models.UUIDField(default=None, null=True, verbose_name='发布者id')), | ||
('publish_user_name', models.CharField(default='', max_length=128, verbose_name='发布者名称')), | ||
('work_flow', models.JSONField(default=dict, verbose_name='工作流数据')), | ||
('application', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='application.application')), | ||
], | ||
options={ | ||
'db_table': 'application_work_flow_version', | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='ApplicationPublicAccessClient', | ||
fields=[ | ||
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), | ||
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')), | ||
('id', models.UUIDField(default=uuid.uuid1, editable=False, primary_key=True, serialize=False, verbose_name='主键id')), | ||
('client_id', models.UUIDField(default=uuid.uuid1, verbose_name='公共访问链接客户端id')), | ||
('client_type', models.CharField(max_length=64, verbose_name='客户端类型')), | ||
('access_num', models.IntegerField(default=0, verbose_name='访问总次数次数')), | ||
('intraday_access_num', models.IntegerField(default=0, verbose_name='当日访问次数')), | ||
('application', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='application.application', verbose_name='应用id')), | ||
], | ||
options={ | ||
'db_table': 'application_public_access_client', | ||
'indexes': [models.Index(fields=['application_id', 'client_id'], name='application_applica_8aaf45_idx')], | ||
}, | ||
), | ||
] | ||
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. The provided migration script looks generally sound for Django models, but there are a few areas where you might consider making optimizations or improvements:
Here's how you could adjust the migration slightly: from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('application', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Chat',
fields=[
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
('id', models.UUIDField(default=uuid4(), editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
('abstract', models.CharField(max_length=1024, verbose_name='摘要')),
# Remove the line below if asker is always predefined
# ('asker', models.JSONField(default=application.models.application_chat.default_asker, encoder=common.encoder.encoder.SystemEncoder, verbose_name='访问者')),
('client_id', models.UUIDField(default=None, null=True, verbose_name='客户端id')),
('is_deleted', models.BooleanField(default=False, verbose_name='')),
('application', models.ForeignKey(on_delete=models.CASCADE, to='application.application')),
],
options={
'db_table': 'application_chat',
},
),
# No changes needed for ChatRecord due to the vote status adjustments.
migrations.CreateModel(
name='WorkFlowVersion',
fields=[
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
('id', models.UUIDField(default=uuid_utils.compat.uuid1, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
('workspace_id', models.CharField(db_index=True, default='default', max_length=64, verbose_name='工作空间id')),
('name', models.CharField(default='', max_length=128, verbose_name='版本名称')),
('publish_user_id', models.UUIDField(default=None, null=True, verbose_name='发布者id')),
('publish_user_name', models.CharField(default='', max_length=128, verbose_name='发布者名称')),
('work_flow', models.JSONField(default=dict, verbose_name='工作流数据')),
('application', models.ForeignKey(on_delete=models.CASCADE, to='application.application')),
],
options={
'db_table': 'application_work_flow_version',
},
),
# Ensure appropriate indexing if required
migrations.CreateModel(
name='ApplicationPublicAccessClient',
fields=[
('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
('id', models.UUIDField(default=uuid.uuid1, editable=False, primary_key=True, serialize=False, verbose_name='主键id')),
('client_id', models.UUIDField(default=uuid.uuid1, verbose_name='公共访问链接客户端id')),
('client_type', models.CharField(max_length=64, verbose_name='客户端类型')),
('access_num', models.IntegerField(default=0, verbose_name='访问总次数次数')),
('intraday_access_num', models.IntegerField(default=0, verbose_name="当日访问次数")),
('application', models.ForeignKey(on_delete=models.CASCADE, to='application.application', verbose_name='应用id')),
],
options={
'db_table': 'application_public_access_client',
'indexes': [models.Index(fields=['application_id', 'client_id'], name='application_applica_8aaf45_idx')],
},
),
] Ensure to test these changes thoroughly post-deployment to verify no unintended consequences. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
""" | ||
from .application_api_key import * | ||
from .application import * | ||
from .application_version import * |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
from application.api.application_version import ApplicationVersionListAPI, ApplicationVersionPageAPI, \ | ||
ApplicationVersionAPI, ApplicationVersionOperateAPI | ||
from application.serializers.application_version import ApplicationVersionSerializer | ||
from common import result | ||
from common.auth import TokenAuth | ||
|
@@ -22,42 +24,39 @@ class ApplicationVersionView(APIView): | |
authentication_classes = [TokenAuth] | ||
|
||
@extend_schema( | ||
methods=['POST'], | ||
description=_("Get the application list"), | ||
summary=_("Get the application list"), | ||
operation_id=_("Get the application list"), # type: ignore | ||
# parameters=ApplicationCreateAPI.get_parameters(), | ||
# request=ApplicationCreateAPI.get_request(), | ||
# responses=ApplicationCreateAPI.get_response(), | ||
methods=['GET'], | ||
description=_("Get the application version list"), | ||
summary=_("Get the application version list"), | ||
operation_id=_("Get the application version list"), # type: ignore | ||
parameters=ApplicationVersionListAPI.get_parameters(), | ||
responses=ApplicationVersionListAPI.get_response(), | ||
tags=[_('Application/Version')] # type: ignore | ||
) | ||
@has_permissions(PermissionConstants.APPLICATION_READ) | ||
@has_permissions(PermissionConstants.APPLICATION_READ.get_workspace_application_permission()) | ||
def get(self, request: Request, workspace_id, application_id: str): | ||
return result.success( | ||
ApplicationVersionSerializer.Query( | ||
data={'name': request.query_params.get('name'), 'user_id': request.user.id, | ||
'application_id': application_id}).list(request.data)) | ||
data={'workspace_id': workspace_id}).list( | ||
{'name': request.query_params.get("name"), 'application_id': application_id})) | ||
|
||
class Page(APIView): | ||
authentication_classes = [TokenAuth] | ||
|
||
@extend_schema( | ||
methods=['GET' | ||
''], | ||
methods=['GET'], | ||
description=_("Get the list of application versions by page"), | ||
summary=_("Get the list of application versions by page"), | ||
operation_id=_("Get the list of application versions by page"), # type: ignore | ||
# parameters=ApplicationCreateAPI.get_parameters(), | ||
# request=ApplicationCreateAPI.get_request(), | ||
# responses=ApplicationCreateAPI.get_response(), | ||
parameters=ApplicationVersionPageAPI.get_parameters(), | ||
responses=ApplicationVersionPageAPI.get_response(), | ||
tags=[_('Application/Version')] # type: ignore | ||
) | ||
@has_permissions(PermissionConstants.APPLICATION_READ) | ||
def get(self, request: Request, application_id: str, current_page: int, page_size: int): | ||
@has_permissions(PermissionConstants.APPLICATION_READ.get_workspace_application_permission()) | ||
def get(self, request: Request, workspace_id: str, application_id: str, current_page: int, page_size: int): | ||
return result.success( | ||
ApplicationVersionSerializer.Query( | ||
data={'name': request.query_params.get('name'), 'user_id': request.user, | ||
'application_id': application_id}).page( | ||
data={'workspace_id': workspace_id}).page( | ||
{'name': request.query_params.get("name"), 'application_id': application_id}, | ||
current_page, page_size)) | ||
|
||
class Operate(APIView): | ||
|
@@ -68,13 +67,12 @@ class Operate(APIView): | |
description=_("Get application version details"), | ||
summary=_("Get application version details"), | ||
operation_id=_("Get application version details"), # type: ignore | ||
# parameters=ApplicationCreateAPI.get_parameters(), | ||
# request=ApplicationCreateAPI.get_request(), | ||
# responses=ApplicationCreateAPI.get_response(), | ||
parameters=ApplicationVersionOperateAPI.get_parameters(), | ||
responses=ApplicationVersionOperateAPI.get_response(), | ||
tags=[_('Application/Version')] # type: ignore | ||
) | ||
@has_permissions(PermissionConstants.APPLICATION_READ) | ||
def get(self, request: Request, application_id: str, work_flow_version_id: str): | ||
@has_permissions(PermissionConstants.APPLICATION_EDIT.get_workspace_application_permission()) | ||
def get(self, request: Request, workspace_id: str, application_id: str, work_flow_version_id: str): | ||
return result.success( | ||
ApplicationVersionSerializer.Operate( | ||
data={'user_id': request.user, | ||
|
@@ -85,12 +83,12 @@ def get(self, request: Request, application_id: str, work_flow_version_id: str): | |
description=_("Modify application version information"), | ||
summary=_("Modify application version information"), | ||
operation_id=_("Modify application version information"), # type: ignore | ||
# parameters=ApplicationCreateAPI.get_parameters(), | ||
# request=ApplicationCreateAPI.get_request(), | ||
# responses=ApplicationCreateAPI.get_response(), | ||
parameters=ApplicationVersionOperateAPI.get_parameters(), | ||
request=None, | ||
responses=ApplicationVersionOperateAPI.get_response(), | ||
tags=[_('Application/Version')] # type: ignore | ||
) | ||
def put(self, request: Request, application_id: str, work_flow_version_id: str): | ||
def put(self, request: Request, workspace_id: str, application_id: str, work_flow_version_id: str): | ||
return result.success( | ||
ApplicationVersionSerializer.Operate( | ||
data={'application_id': application_id, 'work_flow_version_id': work_flow_version_id, | ||
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. The provided Python code is part of an API for managing application versions using FastAPI with Pydantic models. The code includes several views and serializers related to application version management within a Flask REST framework (though it uses similar concepts). Here are some key observations: Code Issues
Optimizations and Suggestions
SummaryThe initial review suggests basic improvements including reducing redundancies, ensuring complete definitions, improving documentation, and streamlining response structures. For further optimizations and best practices, detailed analysis and testing would be necessary. |
||
|
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.
The provided code appears to be a set of Django REST Framework (DRF) viewsets and serializers for managing application versions in the MaxKB project. Here are some suggestions for improvement:
Suggestions:
Imports:
Comments:
get_parameters
which is repeatedly defined across multiple classes.Response Classes:
ListAPIView
,RetrieveAPIView
, etc., instead of manually implementing responses in serializer methods.View Set Inheritance:
Error Handling:
Parameters Consistency:
get_parameters
) should ideally follow similar patterns across different API groups. Use mixins or factory functions to reduce redundancy.DRF Spectacular Documentation:
drf-spectacular
. Check if annotations match your expectations based on the Swagger/OpenAPI schema.Here’s an updated version of the code with some of these improvements applied:
Key Changes Made:
ModelViewSet
,ViewSetMixin
) to reduce code duplication.This revised version provides a cleaner and potentially more maintainable implementation of the application version management system.