feat: implement pagination support for Problem API with new Page endpoint

This commit is contained in:
CaptainB 2025-05-07 16:53:02 +08:00
parent 4995721399
commit a6c74024b0
4 changed files with 78 additions and 12 deletions

View File

@ -48,3 +48,42 @@ class BatchDeleteAPI(ProblemReadAPI):
@staticmethod @staticmethod
def get_request(): def get_request():
return ProblemBatchDeleteSerializer return ProblemBatchDeleteSerializer
class ProblemPageAPI(APIMixin):
@staticmethod
def get_parameters():
return [
OpenApiParameter(
name="workspace_id",
description="工作空间id",
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="knowledge_id",
description="知识库id",
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="current_page",
description="当前页码",
type=OpenApiTypes.INT,
location='path',
required=True,
),
OpenApiParameter(
name="page_size",
description="每页条数",
type=OpenApiTypes.INT,
location='path',
required=True,
),
]
@staticmethod
def get_response():
return DefaultResultSerializer

View File

@ -58,16 +58,18 @@ def is_exits(exits_problem_paragraph_mapping_list, new_paragraph_mapping):
exits_problem_paragraph_mapping_list if exits_problem_paragraph_mapping_list if
str(exits_problem_paragraph_mapping.paragraph_id) == new_paragraph_mapping.paragraph_id str(exits_problem_paragraph_mapping.paragraph_id) == new_paragraph_mapping.paragraph_id
and str(exits_problem_paragraph_mapping.problem_id) == new_paragraph_mapping.problem_id and str(exits_problem_paragraph_mapping.problem_id) == new_paragraph_mapping.problem_id
and str(exits_problem_paragraph_mapping.dataset_id) == new_paragraph_mapping.dataset_id] and str(exits_problem_paragraph_mapping.knowledge_id) == new_paragraph_mapping.knowledge_id]
return len(filter_list) > 0 return len(filter_list) > 0
def to_problem_paragraph_mapping(problem, document_id: str, paragraph_id: str, dataset_id: str): def to_problem_paragraph_mapping(problem, document_id: str, paragraph_id: str, knowledge_id: str):
return ProblemParagraphMapping(id=uuid.uuid1(), return ProblemParagraphMapping(
document_id=document_id, id=uuid.uuid7(),
paragraph_id=paragraph_id, document_id=document_id,
dataset_id=dataset_id, paragraph_id=paragraph_id,
problem_id=str(problem.id)), problem knowledge_id=knowledge_id,
problem_id=str(problem.id)
), problem
class ProblemSerializers(serializers.Serializer): class ProblemSerializers(serializers.Serializer):
@ -117,9 +119,9 @@ class ProblemSerializers(serializers.Serializer):
) if not is_exits(exits_problem_paragraph_mapping, problem_paragraph_mapping) ) if not is_exits(exits_problem_paragraph_mapping, problem_paragraph_mapping)
] ]
QuerySet(ProblemParagraphMapping).bulk_create([ QuerySet(ProblemParagraphMapping).bulk_create(
problem_paragraph_mapping for problem_paragraph_mapping, problem in problem_paragraph_mapping_list [problem_paragraph_mapping for problem_paragraph_mapping, problem in problem_paragraph_mapping_list]
]) )
data_list = [ data_list = [
{ {

View File

@ -33,6 +33,7 @@ urlpatterns = [
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem', views.ProblemView.as_view()), path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem', views.ProblemView.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem/batch_delete', views.ProblemView.BatchDelete.as_view()), path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem/batch_delete', views.ProblemView.BatchDelete.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem/batch_association', views.ProblemView.BatchAssociation.as_view()), path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem/batch_association', views.ProblemView.BatchAssociation.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/document/<int:current_page>/<int:page_sige>', views.DocumentView.Page.as_view()), path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/problem/<int:current_page>/<int:page_size>', views.ProblemView.Page.as_view()),
path('workspace/<str:workspace_id>/knowledge/<str:knowledge_id>/document/<int:current_page>/<int:page_size>', views.DocumentView.Page.as_view()),
path('workspace/<str:workspace_id>/knowledge/<int:current_page>/<int:page_size>', views.KnowledgeView.Page.as_view()), path('workspace/<str:workspace_id>/knowledge/<int:current_page>/<int:page_size>', views.KnowledgeView.Page.as_view()),
] ]

View File

@ -8,7 +8,8 @@ from common.auth.authentication import has_permissions
from common.constants.permission_constants import PermissionConstants from common.constants.permission_constants import PermissionConstants
from common.result import result from common.result import result
from common.utils.common import query_params_to_single_dict from common.utils.common import query_params_to_single_dict
from knowledge.api.problem import ProblemReadAPI, ProblemBatchCreateAPI, BatchAssociationAPI, BatchDeleteAPI from knowledge.api.problem import ProblemReadAPI, ProblemBatchCreateAPI, BatchAssociationAPI, BatchDeleteAPI, \
ProblemPageAPI
from knowledge.serializers.problem import ProblemSerializers from knowledge.serializers.problem import ProblemSerializers
@ -88,3 +89,26 @@ class ProblemView(APIView):
return result.success(ProblemSerializers.BatchOperate( return result.success(ProblemSerializers.BatchOperate(
data={'knowledge_id': knowledge_id, 'workspace_id': workspace_id} data={'knowledge_id': knowledge_id, 'workspace_id': workspace_id}
).delete(request.data)) ).delete(request.data))
class Page(APIView):
authentication_classes = [TokenAuth]
@extend_schema(
summary=_('Get the list of questions by page'),
description=_('Get the list of questions by page'),
operation_id=_('Get the list of questions by page'),
parameters=ProblemPageAPI.get_parameters(),
responses=ProblemPageAPI.get_response(),
tags=[_('Knowledge Base/Documentation/Paragraph/Question')]
)
@has_permissions(PermissionConstants.DOCUMENT_EDIT.get_workspace_permission())
def get(self, request: Request, workspace_id: str, knowledge_id: str, current_page, page_size):
d = ProblemSerializers.Query(
data={
**query_params_to_single_dict(request.query_params),
'knowledge_id': knowledge_id,
'workspace_id': workspace_id
}
)
d.is_valid(raise_exception=True)
return result.success(d.page(current_page, page_size))