From abdd7865ed82a58a1538addd5fe15047601c2046 Mon Sep 17 00:00:00 2001 From: hanshuaikang <1758504262@qq.com> Date: Sat, 16 Dec 2023 19:09:04 +0800 Subject: [PATCH] =?UTF-8?q?minor:=20=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=8D=89=E7=A8=BF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/apis/drf/viewsets/draft_template.py | 2 +- .../view_interceptors/base_template.py | 6 +- gcloud/template_base/domains/importer.py | 27 +++++--- .../template_base/domains/template_manager.py | 66 +++++++++---------- gcloud/template_base/models.py | 10 ++- 5 files changed, 62 insertions(+), 49 deletions(-) diff --git a/gcloud/core/apis/drf/viewsets/draft_template.py b/gcloud/core/apis/drf/viewsets/draft_template.py index 1771d63840..d45059a853 100644 --- a/gcloud/core/apis/drf/viewsets/draft_template.py +++ b/gcloud/core/apis/drf/viewsets/draft_template.py @@ -24,7 +24,7 @@ def update_template_draft(self, manager, template, request): serializer.validated_data["labels"] = serializer.validated_data.pop("template_labels", []) result = manager.update_draft_pipeline( - template.draft_template, request.user.username, serializer.validated_data + template.draft_template, request.user.username, serializer.validated_data["pipeline_tree"] ) return result diff --git a/gcloud/iam_auth/view_interceptors/base_template.py b/gcloud/iam_auth/view_interceptors/base_template.py index d1ca1fccec..5754704047 100644 --- a/gcloud/iam_auth/view_interceptors/base_template.py +++ b/gcloud/iam_auth/view_interceptors/base_template.py @@ -12,7 +12,7 @@ """ from gcloud.iam_auth import IAMMeta from gcloud.iam_auth.intercept import ViewInterceptor -from gcloud.iam_auth.utils import iam_resource_auth_or_raise, iam_multi_resource_auth_or_raise +from gcloud.iam_auth.utils import iam_multi_resource_auth_or_raise, iam_resource_auth_or_raise class YamlImportInterceptor(ViewInterceptor): @@ -27,13 +27,13 @@ def process(self, request, *args, **kwargs): project_resource_id = data["project_id"] project_get_resource_func = "resources_for_project" project_action = IAMMeta.FLOW_CREATE_ACTION - template_action = IAMMeta.FLOW_EDIT_ACTION + template_action = IAMMeta.FLOW_PUBLISH_DRAFT_ACTION template_get_resource_func = "resources_list_for_flows" else: project_resource_id = None project_get_resource_func = None project_action = IAMMeta.COMMON_FLOW_CREATE_ACTION - template_action = IAMMeta.COMMON_FLOW_EDIT_ACTION + template_action = IAMMeta.COMMON_FLOW_PUBLISH_DRAFT_ACTION template_get_resource_func = "resources_list_for_common_flows" iam_resource_auth_or_raise(username, project_action, project_resource_id, project_get_resource_func) diff --git a/gcloud/template_base/domains/importer.py b/gcloud/template_base/domains/importer.py index 54c03c8ab2..f1bcecd7e5 100644 --- a/gcloud/template_base/domains/importer.py +++ b/gcloud/template_base/domains/importer.py @@ -10,14 +10,15 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific lan """ +import logging + from django.apps import apps from django.db import transaction +from django.utils.translation import ugettext_lazy as _ -from .template_manager import TemplateManager -from ..utils import replace_biz_id_value from ...common_template.models import CommonTemplate -from django.utils.translation import ugettext_lazy as _ -import logging +from ..utils import replace_biz_id_value +from .template_manager import TemplateManager logger = logging.getLogger("root") @@ -81,9 +82,9 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N template_id = override_template_id or refer_template_config["template_id"] try: if ( - refer_template_config - and self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate") - and refer_template_config["template_type"] == "common" + refer_template_config + and self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate") + and refer_template_config["template_type"] == "common" ): template = CommonTemplate.objects.get(id=template_id) else: @@ -105,6 +106,7 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N continue if override_template_id: + # 如果选择了替换已有流程,则选择更新模板和草稿 operate_result = manager.update( template=template, editor=operator, @@ -112,6 +114,11 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N pipeline_tree=pipeline_tree, description=description, ) + # 如果需要覆盖的流程已经有草稿了,则更新草稿,没有草稿则不创建 + if template.draft_template_id: + # 更新草稿的时候不再替换 id + manager.update_draft_pipeline(template.draft_template, operator, pipeline_tree) + if operate_result["result"]: pipeline_id_map[td["id"]] = operate_result["data"].id pipeline_version_map[td["id"]] = operate_result["data"].version @@ -120,8 +127,8 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N for key, constant in pipeline_tree["constants"].items(): source_info_map.setdefault(td["id"], {}).update({key: constant.get("source_info", {})}) if ( - self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate") - and refer_template_config["template_type"] == "common" + self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate") + and refer_template_config["template_type"] == "common" ): common_child_templates[td["id"]] = {"constants": pipeline_tree["constants"]} pipeline_id_map[td["id"]] = refer_template_config["template_id"] @@ -144,7 +151,7 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N @staticmethod def _replace_subprocess_template_info( - pipeline_tree: dict, pipeline_id_map: dict, source_map_info: dict, pipeline_version_map: dict + pipeline_tree: dict, pipeline_id_map: dict, source_map_info: dict, pipeline_version_map: dict ) -> dict: """ 将模板数据中临时的模板信息 替换成数据库中模型的对应信息 diff --git a/gcloud/template_base/domains/template_manager.py b/gcloud/template_base/domains/template_manager.py index 6a33b974c7..d20df47226 100644 --- a/gcloud/template_base/domains/template_manager.py +++ b/gcloud/template_base/domains/template_manager.py @@ -36,11 +36,11 @@ def __init__(self, template_model_cls): self.template_model_cls = template_model_cls def create_pipeline( - self, - name: str, - creator: str, - pipeline_tree: dict, - description: str = "", + self, + name: str, + creator: str, + pipeline_tree: dict, + description: str = "", ) -> dict: """ 创建 pipeline 层模板 @@ -68,7 +68,8 @@ def create_pipeline( "result": False, "data": None, "message": message, - "verbose_message": _(f"保存流程失败: 流程树合法性校验失败, 请检查流程. 失败原因: {traceback.format_exc()} | create_pipeline"), + "verbose_message": _( + f"保存流程失败: 流程树合法性校验失败, 请检查流程. 失败原因: {traceback.format_exc()} | create_pipeline"), } create_template_kwargs = { @@ -80,7 +81,8 @@ def create_pipeline( try: pipeline_template = self.template_model_cls.objects.create_pipeline_template(**create_template_kwargs) except Exception as e: - message = _(f"保存流程失败: 创建Pipeline流程失败, 请检查流程. 创建参数[{create_template_kwargs}], 失败原因: [{e}] | create_pipeline") + message = _( + f"保存流程失败: 创建Pipeline流程失败, 请检查流程. 创建参数[{create_template_kwargs}], 失败原因: [{e}] | create_pipeline") logger.error(message) return { "result": False, @@ -95,12 +97,12 @@ def create_pipeline( return {"result": True, "data": pipeline_template, "message": "success", "verbose_message": "success"} def create( - self, - name: str, - creator: str, - pipeline_tree: dict, - template_kwargs: dict, - description: str = "", + self, + name: str, + creator: str, + pipeline_tree: dict, + template_kwargs: dict, + description: str = "", ) -> dict: """ 创建 template 层模板 @@ -128,7 +130,8 @@ def create( try: template = self.template_model_cls.objects.create(**template_kwargs) except Exception as e: - message = _(f"保存流程失败: 创建模板失败, 请检查流程. 创建参数[{template_kwargs}], 失败原因: [{e}] | create") + message = _( + f"保存流程失败: 创建模板失败, 请检查流程. 创建参数[{template_kwargs}], 失败原因: [{e}] | create") logger.error(message) return { "result": False, @@ -142,12 +145,12 @@ def create( return {"result": True, "data": template, "message": "success", "verbose_message": "success"} def update_pipeline( - self, - pipeline_template: PipelineTemplate, - editor: str, - name: str = "", - pipeline_tree: str = None, - description: str = "", + self, + pipeline_template: PipelineTemplate, + editor: str, + name: str = "", + pipeline_tree: str = None, + description: str = "", ) -> dict: """ 更新 pipeline 层模板 @@ -197,7 +200,8 @@ def update_pipeline( try: pipeline_template.update_template(**update_kwargs) except Exception as e: - message = _(f"更新流程失败: 更新Pipeline失败, 请检查流程. 更新参数: [{update_kwargs}], 失败原因: [{e}] | update_pipeline") + message = _( + f"更新流程失败: 更新Pipeline失败, 请检查流程. 更新参数: [{update_kwargs}], 失败原因: [{e}] | update_pipeline") logger.error(message) return { "result": False, @@ -237,14 +241,8 @@ def create_draft(self, template, editor): template.save(update_fields=["draft_template_id"]) @transaction.atomic() - def update_draft_pipeline(self, draft_template, editor, data): - - pipeline_tree = data.pop("pipeline_tree") + def update_draft_pipeline(self, draft_template, editor, pipeline_tree): # 草稿更新不会触发流程合法性校验 - standardize_pipeline_node_name(pipeline_tree) - replace_template_id(self.template_model_cls, pipeline_tree) - pipeline_web_tree = PipelineWebTreeCleaner(pipeline_tree) - pipeline_web_tree.clean() draft_template.editor = editor draft_template.save() @@ -297,12 +295,12 @@ def discard_draft(self, template, editor): return {"result": True, "data": None, "message": "success", "verbose_message": "success"} def update( - self, - template: object, - editor: str, - name: str = "", - pipeline_tree: str = None, - description: str = "", + self, + template: object, + editor: str, + name: str = "", + pipeline_tree: str = None, + description: str = "", ) -> dict: """ 更新 template 层模板 diff --git a/gcloud/template_base/models.py b/gcloud/template_base/models.py index 16a3872ec3..f33c122553 100644 --- a/gcloud/template_base/models.py +++ b/gcloud/template_base/models.py @@ -213,6 +213,15 @@ def _perform_import(self, template_data, check_info, override, defaults_getter, obj, created = self.update_or_create(id=tid, defaults=defaults) if created: new_objects_template_ids.add(template_dict["pipeline_template_id"]) + else: + # 如果不是新建,则需要更新模板信息 + from gcloud.template_base.domains.template_manager import TemplateManager + template_instance = self.get(id=tid) + if template_instance.draft_template_id: + manger = TemplateManager(template_model_cls=None) + manger.update_draft_pipeline(draft_template=template_instance.draft_template, + editor=operator, + pipeline_tree=template_instance.pipeline_template.data) else: new_objects.append(self.model(**defaults)) new_objects_template_ids.add(template_dict["pipeline_template_id"]) @@ -381,7 +390,6 @@ def draft_pipeline_tree(self): draft_template = DraftTemplate.objects.get(id=self.draft_template_id) draft_snapshot_id = draft_template.snapshot_id tree = Snapshot.objects.get(id=draft_snapshot_id).data - replace_template_id(self.__class__, tree, reverse=True) return tree @property