(self, instance: Dict, with_valid=True, **kwargs)
| 1015 | @post(post_function=post_embedding) |
| 1016 | @transaction.atomic |
| 1017 | def save(self, instance: Dict, with_valid=True, **kwargs): |
| 1018 | if with_valid: |
| 1019 | DocumentInstanceSerializer(data=instance).is_valid(raise_exception=True) |
| 1020 | self.is_valid(raise_exception=True) |
| 1021 | knowledge_id = self.data.get("knowledge_id") |
| 1022 | user_id = self.data.get("user_id") |
| 1023 | document_paragraph_model = self.get_document_paragraph_model(knowledge_id, user_id, instance) |
| 1024 | |
| 1025 | document_model = document_paragraph_model.get("document") |
| 1026 | paragraph_model_list = document_paragraph_model.get("paragraph_model_list") |
| 1027 | problem_paragraph_object_list = document_paragraph_model.get("problem_paragraph_object_list") |
| 1028 | problem_model_list, problem_paragraph_mapping_list = ProblemParagraphManage( |
| 1029 | problem_paragraph_object_list, knowledge_id |
| 1030 | ).to_problem_model_list() |
| 1031 | # 插入文档 |
| 1032 | document_model.save() |
| 1033 | # 批量插入段落 |
| 1034 | if len(paragraph_model_list) > 0: |
| 1035 | max_position = ( |
| 1036 | Paragraph.objects.filter(document_id=document_model.id).aggregate(max_position=Max("position"))[ |
| 1037 | "max_position" |
| 1038 | ] |
| 1039 | or 0 |
| 1040 | ) |
| 1041 | for i, paragraph in enumerate(paragraph_model_list): |
| 1042 | paragraph.position = max_position + i + 1 |
| 1043 | QuerySet(Paragraph).bulk_create(paragraph_model_list) |
| 1044 | # 批量插入问题 |
| 1045 | QuerySet(Problem).bulk_create(problem_model_list) if len(problem_model_list) > 0 else None |
| 1046 | # 批量插入关联问题 |
| 1047 | QuerySet(ProblemParagraphMapping).bulk_create(problem_paragraph_mapping_list) if len( |
| 1048 | problem_paragraph_mapping_list |
| 1049 | ) > 0 else None |
| 1050 | document_id = str(document_model.id) |
| 1051 | return ( |
| 1052 | DocumentSerializers.Operate(data={"knowledge_id": knowledge_id, "document_id": document_id}).one( |
| 1053 | with_valid=True |
| 1054 | ), |
| 1055 | document_id, |
| 1056 | knowledge_id, |
| 1057 | ) |
| 1058 | |
| 1059 | @staticmethod |
| 1060 | def get_paragraph_model(document_model, paragraph_list: List): |
no test coverage detected