(self, instance: Dict)
| 220 | @post(post_embedding) |
| 221 | @transaction.atomic |
| 222 | def edit(self, instance: Dict): |
| 223 | self.is_valid() |
| 224 | EditParagraphSerializers(data=instance).is_valid(raise_exception=True) |
| 225 | _paragraph = QuerySet(Paragraph).get(id=self.data.get("paragraph_id")) |
| 226 | update_keys = ["title", "content", "is_active"] |
| 227 | for update_key in update_keys: |
| 228 | if update_key in instance and instance.get(update_key) is not None: |
| 229 | _paragraph.__setattr__(update_key, instance.get(update_key)) |
| 230 | |
| 231 | if instance.get("content") is not None: |
| 232 | _paragraph.chunks = text_to_chunk(instance.get("content", "")) |
| 233 | |
| 234 | if "problem_list" in instance: |
| 235 | update_problem_list = list( |
| 236 | filter(lambda row: "id" in row and row.get("id") is not None, instance.get("problem_list")) |
| 237 | ) |
| 238 | |
| 239 | create_problem_list = list(filter(lambda row: row.get("id") is None, instance.get("problem_list"))) |
| 240 | |
| 241 | # 问题集合 |
| 242 | problem_list = QuerySet(Problem).filter(paragraph_id=self.data.get("paragraph_id")) |
| 243 | |
| 244 | # 校验前端 携带过来的id |
| 245 | for update_problem in update_problem_list: |
| 246 | if not set([str(row.id) for row in problem_list]).__contains__(update_problem.get("id")): |
| 247 | raise AppApiException(500, _("Problem id does not exist")) |
| 248 | # 对比需要删除的问题 |
| 249 | delete_problem_list = ( |
| 250 | list( |
| 251 | filter( |
| 252 | lambda row: ( |
| 253 | not [str(update_row.get("id")) for update_row in update_problem_list].__contains__( |
| 254 | str(row.id) |
| 255 | ) |
| 256 | ), |
| 257 | problem_list, |
| 258 | ) |
| 259 | ) |
| 260 | if len(update_problem_list) > 0 |
| 261 | else [] |
| 262 | ) |
| 263 | # 删除问题 |
| 264 | QuerySet(Problem).filter(id__in=[row.id for row in delete_problem_list]).delete() if len( |
| 265 | delete_problem_list |
| 266 | ) > 0 else None |
| 267 | # 插入新的问题 |
| 268 | QuerySet(Problem).bulk_create( |
| 269 | [ |
| 270 | Problem( |
| 271 | id=uuid.uuid7(), |
| 272 | content=p.get("content"), |
| 273 | paragraph_id=self.data.get("paragraph_id"), |
| 274 | knowledge_id=self.data.get("knowledge_id"), |
| 275 | document_id=self.data.get("document_id"), |
| 276 | ) |
| 277 | for p in create_problem_list |
| 278 | ] |
| 279 | ) if len(create_problem_list) else None |
nothing calls this directly
no test coverage detected