Skip to content

Commit

Permalink
fix: 模板市场导入业务优化 --story=121432122 (#7662)
Browse files Browse the repository at this point in the history
* fix: 导入业务优化 --story=121224121

* fix: 添加复制模板最大数量限制 --story=121224121
  • Loading branch information
guohelu authored Jan 2, 2025
1 parent 724cb0a commit 9cfd9b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
7 changes: 5 additions & 2 deletions gcloud/apigw/validators/copy_template_across_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""
12 changes: 10 additions & 2 deletions gcloud/apigw/views/copy_template_across_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9cfd9b6

Please sign in to comment.