From 9cfd9b6b2a507796846415c50d87e3af17d1113a Mon Sep 17 00:00:00 2001 From: guohelu <141622458+guohelu@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:58:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A8=A1=E6=9D=BF=E5=B8=82=E5=9C=BA?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E4=B8=9A=E5=8A=A1=E4=BC=98=E5=8C=96=20--stor?= =?UTF-8?q?y=3D121432122=20(#7662)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 导入业务优化 --story=121224121 * fix: 添加复制模板最大数量限制 --story=121224121 --- .../apigw/validators/copy_template_across_project.py | 7 +++++-- gcloud/apigw/views/copy_template_across_project.py | 12 ++++++++++-- .../apigw/copy_template_across_project.py | 12 ++++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/gcloud/apigw/validators/copy_template_across_project.py b/gcloud/apigw/validators/copy_template_across_project.py index 53d609477..7d3107b17 100644 --- a/gcloud/apigw/validators/copy_template_across_project.py +++ b/gcloud/apigw/validators/copy_template_across_project.py @@ -23,7 +23,10 @@ def validate(self, request, *args, **kwargs): return valid, err data = json.loads(request.body) - if not data.get("new_project_id") or not data.get("template_id"): - return False, "new_project_id and template_id are required" + if not data.get("new_project_id") or not data.get("template_ids"): + return False, "new_project_id and template_ids are required" + + if data.get("new_project_id") == request.project.id: + return False, "无法导入流程到到同一个项目" return True, "" diff --git a/gcloud/apigw/views/copy_template_across_project.py b/gcloud/apigw/views/copy_template_across_project.py index 6c8681f04..730358da5 100644 --- a/gcloud/apigw/views/copy_template_across_project.py +++ b/gcloud/apigw/views/copy_template_across_project.py @@ -31,6 +31,8 @@ from gcloud.iam_auth.view_interceptors.apigw import CopyTemplateInterceptor from gcloud.apigw.validators.copy_template_across_project import CopyTemplateAcrossProjectValidator +TEMPLATE_COPY_MAX_NUMBER = 10 + @login_exempt @csrf_exempt @@ -51,10 +53,16 @@ def copy_template_across_project(request, project_id): params_data = json.loads(request.body) new_project_id = params_data["new_project_id"] - template_id = params_data["template_id"] + template_ids = params_data["template_ids"] try: - export_data = TaskTemplate.objects.export_templates([template_id], is_full=False, project_id=request.project.id) + export_data = TaskTemplate.objects.export_templates(template_ids, is_full=False, project_id=request.project.id) + if len(export_data["template"]) > TEMPLATE_COPY_MAX_NUMBER: + return { + "result": False, + "message": "only {} templates can be copied once.".format(TEMPLATE_COPY_MAX_NUMBER), + "code": err_code.INVALID_OPERATION.code, + } import_result = TaskTemplate.objects.import_templates( template_data=export_data, override=False, diff --git a/gcloud/iam_auth/view_interceptors/apigw/copy_template_across_project.py b/gcloud/iam_auth/view_interceptors/apigw/copy_template_across_project.py index 9b6b30058..5e4456418 100644 --- a/gcloud/iam_auth/view_interceptors/apigw/copy_template_across_project.py +++ b/gcloud/iam_auth/view_interceptors/apigw/copy_template_across_project.py @@ -29,12 +29,16 @@ class CopyTemplateInterceptor(ViewInterceptor): def process(self, request, *args, **kwargs): data = json.loads(request.body) new_project_id = data.get("new_project_id") - template_id = data.get("template_id") + template_ids = data.get("template_ids") subject = Subject("user", request.user.username) - record = TemplateSharedRecord.objects.filter(project_id=request.project.id, template_id=template_id).first() - if record is None: - error_message = f"Unable to find template {template_id} in project {request.project.id}." + existing_records = TemplateSharedRecord.objects.filter( + project_id=request.project.id, template_id__in=template_ids + ).values_list("template_id", flat=True) + + missing_template_ids = set(template_ids) - set(existing_records) + if missing_template_ids: + error_message = f"The following templates are not shared {missing_template_ids}" logging.error(error_message) raise ValueError(error_message)