MCPcopy Index your code
hub / github.com/1Panel-dev/MaxKB / save

Method save

apps/knowledge/serializers/document.py:1017–1057  ·  view source on GitHub ↗
(self, instance: Dict, with_valid=True, **kwargs)

Source from the content-addressed store, hash-verified

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):

Callers 15

editMethod · 0.45
editMethod · 0.45
editMethod · 0.45
export_excelMethod · 0.45
export_zipMethod · 0.45
export_knowledgeMethod · 0.45
import_knowledgeMethod · 0.45
save_baseMethod · 0.45
save_webMethod · 0.45
handlerMethod · 0.45
transformMethod · 0.45
actionMethod · 0.45

Calls 8

is_validMethod · 0.95
to_problem_model_listMethod · 0.80
is_validMethod · 0.45
getMethod · 0.45
oneMethod · 0.45

Tested by

no test coverage detected