|
25 | 25 | from xlwt import Utils |
26 | 26 |
|
27 | 27 | from common.db.search import native_search, get_dynamics_model, native_page_search |
28 | | -from common.event.listener_manage import ListenerManagement |
29 | 28 | from common.event.common import work_thread_pool |
| 29 | +from common.event.listener_manage import ListenerManagement |
30 | 30 | from common.exception.app_exception import AppApiException |
31 | 31 | from common.field.common import UploadedFileField |
32 | 32 | from common.handle.impl.qa.csv_parse_qa_handle import CsvParseQAHandle |
@@ -1332,6 +1332,69 @@ def batch_add_tag(self, instance: Dict, with_valid=True): |
1332 | 1332 | if new_relations: |
1333 | 1333 | QuerySet(DocumentTag).bulk_create(new_relations) |
1334 | 1334 |
|
| 1335 | + |
| 1336 | + def batch_export(self, instance: Dict, with_valid=True): |
| 1337 | + if with_valid: |
| 1338 | + BatchSerializer(data=instance).is_valid(model=Document, raise_exception=True) |
| 1339 | + self.is_valid(raise_exception=True) |
| 1340 | + document_ids = instance.get("id_list") |
| 1341 | + document_list = QuerySet(Document).filter(id__in=document_ids) |
| 1342 | + paragraph_list = native_search( |
| 1343 | + QuerySet(Paragraph).filter(document_id__in=document_ids), |
| 1344 | + get_file_content( |
| 1345 | + os.path.join(PROJECT_DIR, "apps", "knowledge", 'sql', 'list_paragraph_document_name.sql') |
| 1346 | + ) |
| 1347 | + ) |
| 1348 | + problem_mapping_list = native_search( |
| 1349 | + QuerySet(ProblemParagraphMapping).filter(document_id__in=document_ids), |
| 1350 | + get_file_content(os.path.join(PROJECT_DIR, "apps", "knowledge", 'sql', 'list_problem_mapping.sql')), |
| 1351 | + with_table_name=True |
| 1352 | + ) |
| 1353 | + data_dict, document_dict = DocumentSerializers.Operate.merge_problem( |
| 1354 | + paragraph_list, problem_mapping_list, document_list |
| 1355 | + ) |
| 1356 | + workbook = DocumentSerializers.Operate.get_workbook(data_dict, document_dict) |
| 1357 | + response = HttpResponse(content_type='application/vnd.ms-excel') |
| 1358 | + response['Content-Disposition'] = 'attachment; filename="knowledge.xlsx"' |
| 1359 | + workbook.save(response) |
| 1360 | + return response |
| 1361 | + |
| 1362 | + def batch_export_zip(self, instance: Dict, with_valid=True): |
| 1363 | + if with_valid: |
| 1364 | + BatchSerializer(data=instance).is_valid(model=Document, raise_exception=True) |
| 1365 | + self.is_valid(raise_exception=True) |
| 1366 | + document_ids = instance.get("id_list") |
| 1367 | + document_list = QuerySet(Document).filter(id__in=document_ids) |
| 1368 | + paragraph_list = native_search( |
| 1369 | + QuerySet(Paragraph).filter(document_id__in=document_ids), |
| 1370 | + get_file_content( |
| 1371 | + os.path.join(PROJECT_DIR, "apps", "knowledge", 'sql', 'list_paragraph_document_name.sql') |
| 1372 | + ) |
| 1373 | + ) |
| 1374 | + problem_mapping_list = native_search( |
| 1375 | + QuerySet(ProblemParagraphMapping).filter(document_id__in=document_ids), |
| 1376 | + get_file_content(os.path.join(PROJECT_DIR, "apps", "knowledge", 'sql', 'list_problem_mapping.sql')), |
| 1377 | + with_table_name=True |
| 1378 | + ) |
| 1379 | + data_dict, document_dict = DocumentSerializers.Operate.merge_problem( |
| 1380 | + paragraph_list, problem_mapping_list, document_list |
| 1381 | + ) |
| 1382 | + res = [parse_image(paragraph.get('content')) for paragraph in paragraph_list] |
| 1383 | + |
| 1384 | + workbook = DocumentSerializers.Operate.get_workbook(data_dict, document_dict) |
| 1385 | + response = HttpResponse(content_type='application/zip') |
| 1386 | + response['Content-Disposition'] = f'attachment; filename="knowledge.zip"' |
| 1387 | + zip_buffer = io.BytesIO() |
| 1388 | + with TemporaryDirectory() as tempdir: |
| 1389 | + knowledge_file = os.path.join(tempdir, 'knowledge.xlsx') |
| 1390 | + workbook.save(knowledge_file) |
| 1391 | + for r in res: |
| 1392 | + write_image(tempdir, r) |
| 1393 | + zip_dir(tempdir, zip_buffer) |
| 1394 | + response.write(zip_buffer.getvalue()) |
| 1395 | + return response |
| 1396 | + |
| 1397 | + |
1335 | 1398 | class BatchGenerateRelated(serializers.Serializer): |
1336 | 1399 | workspace_id = serializers.CharField(required=True, label=_('workspace id')) |
1337 | 1400 | knowledge_id = serializers.UUIDField(required=True, label=_('knowledge id')) |
|
0 commit comments