From f1a2405b8f67528f8780dfb477daac947d107477 Mon Sep 17 00:00:00 2001 From: guohelu <141622458+guohelu@users.noreply.github.com> Date: Mon, 23 Dec 2024 21:31:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20sre=E5=B8=82=E5=9C=BA=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=20--story=3D121334648=20(#?= =?UTF-8?q?7648)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: sre视图优化 --story=121334648 * fix: 添加报错返回信息 --story=121334648 * fix: 去除多余权限 --story=121334648 --- gcloud/contrib/template_market/permission.py | 2 +- gcloud/contrib/template_market/serializers.py | 3 +- gcloud/contrib/template_market/viewsets.py | 42 +++++++++++-------- .../core/apis/drf/viewsets/common_template.py | 6 ++- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/gcloud/contrib/template_market/permission.py b/gcloud/contrib/template_market/permission.py index d2f684701..d2821814f 100644 --- a/gcloud/contrib/template_market/permission.py +++ b/gcloud/contrib/template_market/permission.py @@ -47,7 +47,7 @@ def has_permission(self, request, view): serializer = view.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) - template_id_list = serializer.validated_data["template_ids"] + template_id_list = serializer.validated_data["templates"] try: iam_multi_resource_auth_or_raise( username, IAMMeta.FLOW_EDIT_ACTION, template_id_list, "resources_list_for_flows" diff --git a/gcloud/contrib/template_market/serializers.py b/gcloud/contrib/template_market/serializers.py index c809b0da9..40c18d6dc 100644 --- a/gcloud/contrib/template_market/serializers.py +++ b/gcloud/contrib/template_market/serializers.py @@ -47,9 +47,8 @@ class FileUploadAddrSerializer(serializers.Serializer): class TemplateSharedRecordSerializer(serializers.Serializer): project_code = serializers.CharField(required=True, max_length=32, help_text="项目id") - template_ids = serializers.ListField(required=True, help_text="关联的模板列表") + templates = serializers.ListField(required=True, help_text="关联的模板列表") name = serializers.CharField(required=True, help_text="共享名称") - code = serializers.CharField(required=True, help_text="共享标识") category = serializers.CharField(required=True, help_text="共享分类") risk_level = serializers.IntegerField(required=True, help_text="风险级别") usage_id = serializers.IntegerField(required=False, help_text="使用说明id") diff --git a/gcloud/contrib/template_market/viewsets.py b/gcloud/contrib/template_market/viewsets.py index 38436c668..b7e24f27b 100644 --- a/gcloud/contrib/template_market/viewsets.py +++ b/gcloud/contrib/template_market/viewsets.py @@ -62,9 +62,14 @@ class TemplateSceneViewSet(viewsets.ViewSet): market_client = MarketAPIClient def _build_template_data(self, serializer, **kwargs): - templates = TaskTemplate.objects.filter(id__in=serializer.validated_data["template_ids"], is_deleted=False) + templates = TaskTemplate.objects.filter( + id__in=serializer.validated_data["templates"], + project_id=serializer.validated_data["project_code"], + is_deleted=False, + ) template_info = [{"id": template.id, "name": template.name} for template in templates] - data = {"source_system": settings.APP_CODE, "templates": template_info, **serializer.validated_data} + serializer.validated_data["templates"] = template_info + data = {"source_system": settings.APP_CODE, **serializer.validated_data} market_record_id = kwargs.get("market_record_id") if market_record_id: data["id"] = market_record_id @@ -72,15 +77,17 @@ def _build_template_data(self, serializer, **kwargs): def _handle_response(self, response_data, error_message): if not response_data.get("result"): - message = "market template {}, error message: {}".format(error_message, response_data["message"]) - logger.error(message) - return Response( - { - "result": False, - "message": message, - "code": err_code.OPERATION_FAIL.code, - } - ) + response_structure = { + "result": False, + "message": "market template {}, error message: {}".format(error_message, response_data.get("message")), + "code": err_code.OPERATION_FAIL.code, + } + logger.error(response_structure["message"]) + + if response_data.get("code") == 9007: + response_structure["data"] = response_data.get("data") + + return Response(response_structure) return None @action(detail=False, methods=["get"]) @@ -119,7 +126,7 @@ def get_scene_label(self, request, *args, **kwargs): @action(detail=False, methods=["post"]) @swagger_auto_schema(request_body=SceneLabelSerializer) - def create_scene_label(self, request, *args, **kwargs): + def add_scene_label(self, request, *args, **kwargs): serializer = SceneLabelSerializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -146,7 +153,8 @@ def get_risk_level(self, request, *args, **kwargs): return error_response return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code}) - def list(self, request, *args, **kwargs): + @action(detail=False, methods=["get"]) + def get_scene_template_list(self, request, *args, **kwargs): client = self.market_client(username=request.user.username) response_data = client.get_template_scene_list() error_response = self._handle_response(response_data, "Failed to obtain the list") @@ -162,13 +170,13 @@ def create(self, request, *args, **kwargs): data = self._build_template_data(serializer) client = self.market_client(username=request.user.username) response_data = client.create_template_scene(data) - create_response = self._handle_response(response_data, "Failed to create market template record") + create_response = self._handle_response(response_data, "Failed to create record") if create_response: return create_response TemplateSharedRecord.objects.update_shared_record( project_id=int(serializer.validated_data["project_code"]), - new_template_ids=serializer.validated_data["template_ids"], + new_template_ids=request.data["templates"], market_record_id=response_data["data"]["id"], creator=request.user.username, ) @@ -189,13 +197,13 @@ def partial_update(self, request, *args, **kwargs): data = self._build_template_data(serializer, market_record_id=market_record_id) response_data = client.patch_template_scene(data, market_record_id) - update_response = self._handle_response(response_data, "Failed to update market template record") + update_response = self._handle_response(response_data, "Failed to update record") if update_response: return update_response TemplateSharedRecord.objects.update_shared_record( project_id=int(serializer.validated_data["project_code"]), - new_template_ids=serializer.validated_data["template_ids"], + new_template_ids=request.data["templates"], market_record_id=market_record_id, creator=request.user.username, existing_market_template_ids=existing_market_template_ids, diff --git a/gcloud/core/apis/drf/viewsets/common_template.py b/gcloud/core/apis/drf/viewsets/common_template.py index eb5ef89bd..ebdda0daf 100644 --- a/gcloud/core/apis/drf/viewsets/common_template.py +++ b/gcloud/core/apis/drf/viewsets/common_template.py @@ -91,7 +91,11 @@ class CommonTemplateViewSet(GcloudModelViewSet): pagination_class = LimitOffsetPagination iam_resource_helper = ViewSetResourceHelper( resource_func=res_factory.resources_for_common_flow_obj, - actions=[IAMMeta.COMMON_FLOW_VIEW_ACTION, IAMMeta.COMMON_FLOW_EDIT_ACTION, IAMMeta.COMMON_FLOW_DELETE_ACTION], + actions=[ + IAMMeta.COMMON_FLOW_VIEW_ACTION, + IAMMeta.COMMON_FLOW_EDIT_ACTION, + IAMMeta.COMMON_FLOW_DELETE_ACTION, + ], ) filterset_class = CommonTemplateFilter permission_classes = [permissions.IsAuthenticated, CommonTemplatePermission]