浏览代码

support rename document (#4915)

Jyong 10 月之前
父节点
当前提交
4e3b0c5aea

+ 24 - 0
api/controllers/console/datasets/datasets_document.py

@@ -930,6 +930,28 @@ class DocumentRetryApi(DocumentResource):
         return {'result': 'success'}, 204
 
 
+class DocumentRenameApi(DocumentResource):
+    @setup_required
+    @login_required
+    @account_initialization_required
+    @marshal_with(document_fields)
+    def post(self, dataset_id, document_id):
+        # The role of the current user in the ta table must be admin or owner
+        if not current_user.is_admin_or_owner:
+            raise Forbidden()
+
+        parser = reqparse.RequestParser()
+        parser.add_argument('name', type=str, required=True, nullable=False, location='json')
+        args = parser.parse_args()
+
+        try:
+            document = DocumentService.rename_document(dataset_id, document_id, args['name'])
+        except services.errors.document.DocumentIndexingError:
+            raise DocumentIndexingError('Cannot delete document during indexing.')
+
+        return document
+
+
 api.add_resource(GetProcessRuleApi, '/datasets/process-rule')
 api.add_resource(DatasetDocumentListApi,
                  '/datasets/<uuid:dataset_id>/documents')
@@ -956,3 +978,5 @@ api.add_resource(DocumentStatusApi,
 api.add_resource(DocumentPauseApi, '/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/processing/pause')
 api.add_resource(DocumentRecoverApi, '/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/processing/resume')
 api.add_resource(DocumentRetryApi, '/datasets/<uuid:dataset_id>/retry')
+api.add_resource(DocumentRenameApi,
+                 '/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/rename')

+ 2 - 0
api/fields/document_fields.py

@@ -8,6 +8,7 @@ document_fields = {
     'position': fields.Integer,
     'data_source_type': fields.String,
     'data_source_info': fields.Raw(attribute='data_source_info_dict'),
+    'data_source_detail_dict': fields.Raw(attribute='data_source_detail_dict'),
     'dataset_process_rule_id': fields.String,
     'name': fields.String,
     'created_from': fields.String,
@@ -31,6 +32,7 @@ document_with_segments_fields = {
     'position': fields.Integer,
     'data_source_type': fields.String,
     'data_source_info': fields.Raw(attribute='data_source_info_dict'),
+    'data_source_detail_dict': fields.Raw(attribute='data_source_detail_dict'),
     'dataset_process_rule_id': fields.String,
     'name': fields.String,
     'created_from': fields.String,

+ 21 - 0
api/services/dataset_service.py

@@ -451,6 +451,27 @@ class DocumentService:
         db.session.delete(document)
         db.session.commit()
 
+    @staticmethod
+    def rename_document(dataset_id: str, document_id: str, name: str) -> Document:
+        dataset = DatasetService.get_dataset(dataset_id)
+        if not dataset:
+            raise ValueError('Dataset not found.')
+
+        document = DocumentService.get_document(dataset_id, document_id)
+
+        if not document:
+            raise ValueError('Document not found.')
+
+        if document.tenant_id != current_user.current_tenant_id:
+            raise ValueError('No permission.')
+
+        document.name = name
+
+        db.session.add(document)
+        db.session.commit()
+
+        return document
+
     @staticmethod
     def pause_document(document):
         if document.indexing_status not in ["waiting", "parsing", "cleaning", "splitting", "indexing"]: