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)