def obj_create(self, bundle, **kwargs): manager = TemplateManager(template_model_cls=bundle.obj.__class__) try: name = bundle.data.pop("name") creator = bundle.request.user.username pipeline_tree = json.loads(bundle.data.pop("pipeline_tree")) description = bundle.data.pop("description", "") notify_type = bundle.data.get("notify_type") or {"success": [], "fail": []} if isinstance(notify_type, str): loaded_notify_type = json.loads(notify_type) notify_type = {"success": loaded_notify_type, "fail": loaded_notify_type} bundle.data["notify_type"] = json.dumps(notify_type) except (KeyError, ValueError) as e: raise BadRequest(str(e)) with transaction.atomic(): result = manager.create_pipeline( name=name, creator=creator, pipeline_tree=pipeline_tree, description=description ) if not result["result"]: logger.error(result["verbose_message"]) raise BadRequest(result["verbose_message"]) kwargs["pipeline_template_id"] = result["data"].template_id bundle = super(TaskTemplateResource, self).obj_create(bundle, **kwargs) self._sync_template_labels(bundle) return bundle
def obj_update(self, bundle, skip_errors=False, **kwargs): template = bundle.obj manager = TemplateManager(template_model_cls=bundle.obj.__class__) try: name = bundle.data.pop("name") editor = bundle.request.user.username pipeline_tree = json.loads(bundle.data.pop("pipeline_tree")) description = bundle.data.pop("description") notify_type = bundle.data.get("notify_type") or {"success": [], "fail": []} if isinstance(notify_type, str): loaded_notify_type = json.loads(notify_type) notify_type = {"success": loaded_notify_type, "fail": loaded_notify_type} bundle.data["notify_type"] = json.dumps(notify_type) except (KeyError, ValueError) as e: raise BadRequest(str(e)) with transaction.atomic(): result = manager.update_pipeline( pipeline_template=template.pipeline_template, editor=editor, name=name, pipeline_tree=pipeline_tree, description=description, ) if not result["result"]: logger.error(result["verbose_message"]) raise BadRequest(result["verbose_message"]) bundle.data["pipeline_template"] = "/api/v3/pipeline_template/%s/" % template.pipeline_template.pk self._sync_template_labels(bundle) return super(TaskTemplateResource, self).obj_update(bundle, **kwargs)
def obj_delete(self, bundle, **kwargs): try: template = CommonTemplate.objects.get(id=kwargs["pk"], is_deleted=False) except CommonTemplate.DoesNotExist: raise NotFound("flow template does not exist") manager = TemplateManager(template_model_cls=bundle.obj.__class__) can_delete, message = manager.can_delete(template) if not can_delete: raise BadRequest(message) return super(CommonTemplateResource, self).obj_delete(bundle, **kwargs)
def batch_delete(self, request, *args, **kwargs): """批量删除流程""" data = request.data body_serializer = self.template_ids_serializer(data=data) body_serializer.is_valid(raise_exception=True) template_ids = body_serializer.validated_data.get("template_ids") manager = TemplateManager(template_model_cls=self.tmpl_model) result = manager.batch_delete(template_ids) if not result["result"]: raise APIException( f'[batch_delete] result False: {result["message"]}') return Response(result["data"])
def obj_delete(self, bundle, **kwargs): try: template = TaskTemplate.objects.get(id=kwargs["pk"]) except TaskTemplate.DoesNotExist: raise BadRequest("template does not exist") manager = TemplateManager(template_model_cls=bundle.obj.__class__) can_delete, message = manager.can_delete(template) if not can_delete: raise BadRequest(message) # 删除该流程引用的子流程节点的执行方案 pipeline_template_id = template.pipeline_template.template_id relation_queryset = TemplateRelationship.objects.filter(ancestor_template_id=pipeline_template_id) for relation in relation_queryset: relation.templatescheme_set.clear() return super(TaskTemplateResource, self).obj_delete(bundle, **kwargs)