Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sre市场功能代码优化 --story=121334648 #7648

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gcloud/contrib/template_market/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions gcloud/contrib/template_market/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
42 changes: 25 additions & 17 deletions gcloud/contrib/template_market/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,32 @@ 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
return data

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"])
Expand Down Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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,
)
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion gcloud/core/apis/drf/viewsets/common_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading