Skip to content

Commit

Permalink
fix: 补充接口 #7626
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Dec 19, 2024
1 parent ac035af commit dd8766e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
10 changes: 9 additions & 1 deletion gcloud/contrib/template_market/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def _get_url(self, endpoint):

def _make_request(self, method, endpoint, **kwargs):
url = self._get_url(endpoint)
response = requests.request(method, url, headers=self.headers, json=kwargs.get("data"))
response = requests.request(
method, url, headers=self.headers, json=kwargs.get("data"), params=kwargs.get("params")
)
return response.json()

def get_service_category(self):
Expand All @@ -40,6 +42,12 @@ def get_service_category(self):
def get_scene_label(self):
return self._make_request("GET", "/sre_property/scene_label/")

def get_file_upload_addr(self, params):
return self._make_request("GET", "/sre_scene/get_file_upload_addr/", params=params)

def create_scene_label(self, data):
return self._make_request("POST", "/sre_property/scene_label/", data=data)

def get_risk_level(self):
return self._make_request("GET", "/sre_scene/risk_level/")

Expand Down
11 changes: 11 additions & 0 deletions gcloud/contrib/template_market/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class TemplateProjectBaseSerializer(serializers.Serializer):
project_id = serializers.CharField(required=True, help_text="项目id")


class SceneLabelSerializer(serializers.Serializer):
name = serializers.CharField(required=True, help_text="标签名称")
code = serializers.CharField(required=True, help_text="场景标签英文标识")


class FileUploadAddrSerializer(serializers.Serializer):
scene_type = serializers.CharField(required=True, help_text="场景类型")
scene_code = serializers.CharField(required=True, help_text="场景标识")
file_name = serializers.CharField(required=True, help_text="文件名称")


class TemplateSharedRecordSerializer(serializers.Serializer):
project_code = serializers.CharField(required=True, max_length=32, help_text="项目id")
template_ids = serializers.ListField(required=True, help_text="关联的模板列表")
Expand Down
57 changes: 49 additions & 8 deletions gcloud/contrib/template_market/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
TemplateSharedRecordSerializer,
TemplatePreviewSerializer,
TemplateProjectBaseSerializer,
SceneLabelSerializer,
FileUploadAddrSerializer,
)
from gcloud.contrib.template_market.models import TemplateSharedRecord
from gcloud.taskflow3.models import TaskTemplate
Expand Down Expand Up @@ -91,6 +93,22 @@ def get_service_category(self, request, *args, **kwargs):
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

@action(detail=False, methods=["get"])
def get_file_upload_addr(self, request, *args, **kwargs):
serializer = FileUploadAddrSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
params = {
"scene_type": serializer.validated_data["scene_type"],
"scene_code": serializer.validated_data["scene_code"],
"file_name": serializer.validated_data["file_name"],
}
client = self.market_client(username=request.user.username)
response_data = client.get_file_upload_addr(params)
error_response = self._handle_response(response_data, "Failed to obtain file upload address")
if error_response:
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

@action(detail=False, methods=["get"])
def get_scene_label(self, request, *args, **kwargs):
client = self.market_client(username=request.user.username)
Expand All @@ -100,19 +118,39 @@ def get_scene_label(self, request, *args, **kwargs):
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

@action(detail=False, methods=["post"])
@swagger_auto_schema(request_body=SceneLabelSerializer)
def create_scene_label(self, request, *args, **kwargs):
serializer = SceneLabelSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

data = {
"name": serializer.validated_data["name"],
"code": serializer.validated_data["code"],
}

client = self.market_client(username=request.user.username)
response_data = client.create_scene_label(data)

create_response = self._handle_response(response_data, "Failed to create label")
if create_response:
return create_response

return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

@action(detail=False, methods=["get"])
def get_risk_level(self, request, *args, **kwargs):
client = self.market_client(username=request.user.username)
response_data = client.get_risk_level()
error_response = self._handle_response(response_data, "Failed to obtain the market risk level list")
error_response = self._handle_response(response_data, "Failed to obtain the risk level list")
if error_response:
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

def 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 market template list")
error_response = self._handle_response(response_data, "Failed to obtain the list")
if error_response:
return error_response
return Response({"result": True, "data": response_data, "code": err_code.SUCCESS.code})
Expand All @@ -125,9 +163,9 @@ 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)
error_response = self._handle_response(response_data, "Failed to create market template record")
if error_response:
return error_response
create_response = self._handle_response(response_data, "Failed to create market template record")
if create_response:
return create_response

TemplateSharedRecord.objects.update_shared_record(
project_id=int(serializer.validated_data["project_code"]),
Expand All @@ -145,15 +183,18 @@ def partial_update(self, request, *args, **kwargs):

client = self.market_client(username=request.user.username)
existing_records = client.get_template_scene_detail(market_record_id)
detail_response = self._handle_response(existing_records, "Failed to get details")
if detail_response:
return detail_response
existing_market_template_ids = set(
[template["id"] for template in json.loads(existing_records["data"]["templates"])]
)

data = self._build_template_data(serializer, market_record_id=market_record_id)
response_data = client.patch_template_scene(data, market_record_id)
error_response = self._handle_response(response_data, "Failed to update market template record")
if error_response:
return error_response
update_response = self._handle_response(response_data, "Failed to update market template record")
if update_response:
return update_response

TemplateSharedRecord.objects.update_shared_record(
project_id=int(serializer.validated_data["project_code"]),
Expand Down

0 comments on commit dd8766e

Please sign in to comment.