From afbdec53845d9a2fc0396fa614795c6530341fa2 Mon Sep 17 00:00:00 2001 From: guohelu <19503896967@163.com> Date: Fri, 3 Jan 2025 16:31:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E6=B5=8B=E8=AF=95bug=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20--story=3D121224121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gcloud/contrib/template_market/viewsets.py | 8 +++++++- .../core/apis/drf/serilaziers/periodic_task.py | 12 +++++++++++- .../apigw/copy_template_across_project.py | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/gcloud/contrib/template_market/viewsets.py b/gcloud/contrib/template_market/viewsets.py index d05336957..14e6d5a66 100644 --- a/gcloud/contrib/template_market/viewsets.py +++ b/gcloud/contrib/template_market/viewsets.py @@ -68,11 +68,17 @@ class TemplateSceneViewSet(viewsets.ViewSet): market_client = MarketAPIClient def _build_template_data(self, serializer, **kwargs): + template_ids = serializer.validated_data["templates"] + templates = TaskTemplate.objects.filter( - id__in=serializer.validated_data["templates"], + id__in=template_ids, project_id=serializer.validated_data["project_code"], is_deleted=False, ) + if templates.count() != len(template_ids): + missing_ids = set(template_ids) - {template.id for template in templates} + raise ValueError(f"Templates with IDs {missing_ids} are missing or deleted.") + template_info = [{"id": template.id, "name": template.name} for template in templates] serializer.validated_data["templates"] = template_info data = {"source_system": settings.APP_CODE, **serializer.validated_data} diff --git a/gcloud/core/apis/drf/serilaziers/periodic_task.py b/gcloud/core/apis/drf/serilaziers/periodic_task.py index 670a8ba59..4f290e90a 100644 --- a/gcloud/core/apis/drf/serilaziers/periodic_task.py +++ b/gcloud/core/apis/drf/serilaziers/periodic_task.py @@ -174,6 +174,7 @@ def inspect_cron(self, cron): class CreatePeriodicTaskSerializer(CronFieldSerializer, serializers.ModelSerializer): + id = serializers.IntegerField(required=False) project = serializers.IntegerField(write_only=True) template_source = serializers.CharField(required=False, default=PROJECT) pipeline_tree = ReadWriteSerializerMethodField() @@ -219,7 +220,16 @@ def validate(self, attrs): class Meta: model = PeriodicTask - fields = ["project", "cron", "name", "template_id", "pipeline_tree", "template_source", "template_scheme_ids"] + fields = [ + "id", + "project", + "cron", + "name", + "template_id", + "pipeline_tree", + "template_source", + "template_scheme_ids", + ] class PatchUpdatePeriodicTaskSerializer(CronFieldSerializer, serializers.Serializer): 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 5e4456418..703e466a4 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 @@ -21,6 +21,7 @@ from gcloud.iam_auth import res_factory from gcloud.iam_auth.intercept import ViewInterceptor from gcloud.contrib.template_market.models import TemplateSharedRecord +from gcloud.tasktmpl3.models import TaskTemplate iam = get_iam_client() @@ -32,13 +33,22 @@ def process(self, request, *args, **kwargs): template_ids = data.get("template_ids") subject = Subject("user", request.user.username) + existing_templates = TaskTemplate.objects.filter( + project_id=request.project.id, id__in=template_ids + ).values_list("id", flat=True) + missing_template_ids = set(template_ids) - set(existing_templates) + if missing_template_ids: + error_message = f"The following templates already not exist {missing_template_ids}" + logging.error(error_message) + raise ValueError(error_message) + 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}" + missing_template_records_ids = set(template_ids) - set(existing_records) + if missing_template_records_ids: + error_message = f"The following templates are not shared {missing_template_records_ids}" logging.error(error_message) raise ValueError(error_message) From 4f3038edaed271286477a206eb0c3c3d028c4756 Mon Sep 17 00:00:00 2001 From: guohelu <19503896967@163.com> Date: Fri, 3 Jan 2025 17:16:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91=20--story=3D121224121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/apis/drf/serilaziers/periodic_task.py | 2 -- .../apigw/copy_template_across_project.py | 18 +++++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/gcloud/core/apis/drf/serilaziers/periodic_task.py b/gcloud/core/apis/drf/serilaziers/periodic_task.py index 4f290e90a..7b3a76e53 100644 --- a/gcloud/core/apis/drf/serilaziers/periodic_task.py +++ b/gcloud/core/apis/drf/serilaziers/periodic_task.py @@ -174,7 +174,6 @@ def inspect_cron(self, cron): class CreatePeriodicTaskSerializer(CronFieldSerializer, serializers.ModelSerializer): - id = serializers.IntegerField(required=False) project = serializers.IntegerField(write_only=True) template_source = serializers.CharField(required=False, default=PROJECT) pipeline_tree = ReadWriteSerializerMethodField() @@ -221,7 +220,6 @@ def validate(self, attrs): class Meta: model = PeriodicTask fields = [ - "id", "project", "cron", "name", 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 703e466a4..438ab64f3 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 @@ -33,15 +33,6 @@ def process(self, request, *args, **kwargs): template_ids = data.get("template_ids") subject = Subject("user", request.user.username) - existing_templates = TaskTemplate.objects.filter( - project_id=request.project.id, id__in=template_ids - ).values_list("id", flat=True) - missing_template_ids = set(template_ids) - set(existing_templates) - if missing_template_ids: - error_message = f"The following templates already not exist {missing_template_ids}" - logging.error(error_message) - raise ValueError(error_message) - existing_records = TemplateSharedRecord.objects.filter( project_id=request.project.id, template_id__in=template_ids ).values_list("template_id", flat=True) @@ -52,6 +43,15 @@ def process(self, request, *args, **kwargs): logging.error(error_message) raise ValueError(error_message) + existing_templates = TaskTemplate.objects.filter( + project_id=request.project.id, id__in=template_ids, is_deleted=False + ).values_list("id", flat=True) + missing_template_ids = set(template_ids) - set(existing_templates) + if missing_template_ids: + error_message = f"The following templates already not exist {missing_template_ids}" + logging.error(error_message) + raise ValueError(error_message) + action = Action(IAMMeta.FLOW_CREATE_ACTION) resources = res_factory.resources_for_project(new_project_id) allow_or_raise_auth_failed(iam, IAMMeta.SYSTEM_ID, subject, action, resources, cache=True)