Skip to content

Commit

Permalink
fix: 代码复杂度优化 #7626
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Dec 16, 2024
1 parent 2ab28e4 commit 5602c8e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 70 deletions.
8 changes: 4 additions & 4 deletions config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ def check_engine_admin_permission(request, *args, **kwargs):
ENABLE_TEMPLATE_MARKET = env.ENABLE_TEMPLATE_MARKET
# 流程商店 API 地址
TEMPLATE_MARKET_API_URL = env.TEMPLATE_MARKET_API_URL
# SRE商店路由
SRE_STORE_API_URL = env.SRE_STORE_API_URL
# SRE商店帮助文档路由
SRE_HELP_DOCUMENT_URL = env.SRE_HELP_DOCUMENT_URL
# 模板市场路由
TEMPLATE_MARKET_HOST = env.TEMPLATE_MARKET_HOST
# 模板市场文档路由
TEMPLATE_MARKET_DOC_URL = env.TEMPLATE_MARKET_DOC_URL
8 changes: 4 additions & 4 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
ENABLE_TEMPLATE_MARKET = False if os.getenv("ENABLE_TEMPLATE_MARKET") is None else True
# 流程商店 API 地址
TEMPLATE_MARKET_API_URL = os.getenv("TEMPLATE_MARKET_API_URL", "")
# SRE商店路由
SRE_STORE_API_URL = os.getenv("SRE_STORE_API_URL", "")
# SRE商店帮助文档路由
SRE_HELP_DOCUMENT_URL = os.getenv("SRE_HELP_DOCUMENT_URL", "")
# 模板市场路由
TEMPLATE_MARKET_HOST = os.getenv("TEMPLATE_MARKET_HOST", "")
# 模板市场文档路由
TEMPLATE_MARKET_DOC_URL = os.getenv("TEMPLATE_MARKET_DOC_URL", "")
24 changes: 15 additions & 9 deletions gcloud/contrib/template_market/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

import json
import requests

from gcloud.conf import settings
Expand All @@ -19,41 +19,47 @@
class MarketAPIClient:
def __init__(self):
self.base_url = settings.TEMPLATE_MARKET_API_URL
self.headers = {
"X-Bkapi-Authorization": json.dumps(
{"bk_app_code": settings.APP_CODE, "bk_app_secret": settings.SECRET_KEY}
),
"Content-Type": "application/json",
}

def _get_url(self, endpoint):
return f"{self.base_url}{endpoint}"

def get_service_category(self):
url = self._get_url("/category/get_service_category/")
response = requests.get(url)
response = requests.get(url, headers=self.headers)
return response.json()

def get_scene_label(self):
url = self._get_url("/sre_property/scene_label/")
response = requests.get(url)
response = requests.get(url, headers=self.headers)
return response.json()

def get_risk_level(self, request):
def get_risk_level(self):
url = self._get_url("/sre_scene/risk_level/")
response = requests.get(url)
response = requests.get(url, headers=self.headers)
return response.json()

def get_template_scene_detail(self, market_record_id):
url = self._get_url(f"/sre_scene/flow_template_scene/{market_record_id}/")
response = requests.get(url)
response = requests.get(url, headers=self.headers)
return response.json()

def get_template_scene_list(self):
url = self._get_url("/sre_scene/flow_template_scene/?is_all=true")
response = requests.get(url)
response = requests.get(url, headers=self.headers)
return response.json()

def create_template_scene(self, data):
url = self._get_url("/sre_scene/flow_template_scene/")
response = requests.post(url, json=data)
response = requests.post(url, json=data, headers=self.headers)
return response.json()

def patch_template_scene(self, data, market_record_id):
url = self._get_url(f"/sre_scene/flow_template_scene/{market_record_id}/")
response = requests.patch(url, json=data)
response = requests.patch(url, json=data, headers=self.headers)
return response.json()
4 changes: 3 additions & 1 deletion gcloud/contrib/template_market/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.15 on 2024-12-13 10:48
# Generated by Django 3.2.15 on 2024-12-16 11:45

from django.db import migrations, models

Expand All @@ -17,6 +17,8 @@ class Migration(migrations.Migration):
("project_id", models.IntegerField(default=-1, help_text="项目 ID", verbose_name="项目 ID")),
("template_id", models.IntegerField(db_index=True, help_text="模板 ID", verbose_name="模板 ID")),
("creator", models.CharField(default="", max_length=32, verbose_name="创建者")),
("create_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
("update_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
("extra_info", models.JSONField(blank=True, null=True, verbose_name="额外信息")),
],
options={
Expand Down
2 changes: 2 additions & 0 deletions gcloud/contrib/template_market/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class TemplateSharedRecord(models.Model):
project_id = models.IntegerField(_("项目 ID"), default=-1, help_text="项目 ID")
template_id = models.IntegerField(_("模板 ID"), help_text="模板 ID", db_index=True)
creator = models.CharField(_("创建者"), max_length=32, default="")
create_at = models.DateTimeField(verbose_name=_("创建时间"), auto_now_add=True)
update_at = models.DateTimeField(verbose_name=_("更新时间"), auto_now=True)
extra_info = models.JSONField(_("额外信息"), blank=True, null=True)

objects = TemplateSharedManager()
Expand Down
81 changes: 31 additions & 50 deletions gcloud/contrib/template_market/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,59 +67,47 @@ def _build_template_data(self, serializer, **kwargs):
data["id"] = market_record_id
return data

@action(detail=False, methods=["get"])
def get_service_category(self, request, *args, **kwargs):
response_data = self.market_client.get_service_category()
if not response_data["result"]:
logging.warning("Failed to obtain the market service category")
def _handle_response(self, response_data, error_message):
if not response_data.get("result"):
logging.exception(error_message)
return Response(
{
"result": False,
"message": "Failed to obtain the market service category",
"message": error_message,
"code": err_code.OPERATION_FAIL.code,
}
)
return None

@action(detail=False, methods=["get"])
def get_service_category(self, request, *args, **kwargs):
response_data = self.market_client.get_service_category()
error_response = self._handle_response(response_data, "Failed to obtain scene category")
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):
response_data = self.market_client.get_scene_label()
if not response_data["result"]:
logging.exception("Failed to obtain scene tag list")
return Response(
{
"result": False,
"message": "Failed to obtain scene tag list",
"code": err_code.OPERATION_FAIL.code,
}
)
error_response = self._handle_response(response_data, "Failed to obtain scene tag list")
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_risk_level(self, request, *args, **kwargs):
response_data = self.market_client.get_risk_level(request)
if not response_data["result"]:
logging.exception("Failed to obtain the market risk level list")
return Response(
{
"result": False,
"message": "Failed to obtain the market risk level list",
"code": err_code.OPERATION_FAIL.code,
}
)
response_data = self.market_client.get_risk_level()
error_response = self._handle_response(response_data, "Failed to obtain the market 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):
response_data = self.market_client.get_template_scene_list()
if not response_data["result"]:
logging.exception("Failed to obtain the market template list")
return Response(
{
"result": False,
"message": "Failed to obtain the market template list",
"code": err_code.OPERATION_FAIL.code,
}
)
error_response = self._handle_response(response_data, "Failed to obtain the market template list")
if error_response:
return error_response
return Response({"result": True, "data": response_data, "code": err_code.SUCCESS.code})

@swagger_auto_schema(request_body=TemplateSharedRecordSerializer)
Expand All @@ -129,14 +117,10 @@ def create(self, request, *args, **kwargs):

data = self._build_template_data(serializer)
response_data = self.market_client.create_template_scene(data)
if not response_data.get("result"):
return Response(
{
"result": False,
"message": "Failed to create market template record",
"code": err_code.OPERATION_FAIL.code,
}
)
error_response = self._handle_response(response_data, "Failed to create market template record")
if error_response:
return error_response

TemplateSharedRecord.objects.update_shared_record(
project_id=int(serializer.validated_data["project_code"]),
new_template_ids=serializer.validated_data["template_ids"],
Expand All @@ -155,16 +139,13 @@ def partial_update(self, request, *args, **kwargs):
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 = self.market_client.patch_template_scene(data, market_record_id)
if not response_data.get("result"):
return Response(
{
"result": False,
"message": "Failed to update market template record",
"code": err_code.OPERATION_FAIL.code,
}
)
error_response = self._handle_response(response_data, "Failed to update market template record")
if error_response:
return error_response

TemplateSharedRecord.objects.update_shared_record(
project_id=int(serializer.validated_data["project_code"]),
new_template_ids=serializer.validated_data["template_ids"],
Expand Down
4 changes: 2 additions & 2 deletions gcloud/core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def mysetting(request):
"REMOTE_ANALYSIS_URL": settings.REMOTE_ANALYSIS_URL,
"REMOTE_API_URL": settings.REMOTE_API_URL,
"ENABLE_TEMPLATE_MARKET": settings.ENABLE_TEMPLATE_MARKET,
"SRE_STORE_API_URL": settings.SRE_STORE_API_URL,
"SRE_HELP_DOCUMENT_URL": settings.SRE_HELP_DOCUMENT_URL,
"TEMPLATE_MARKET_HOST": settings.TEMPLATE_MARKET_HOST,
"TEMPLATE_MARKET_DOC_URL": settings.TEMPLATE_MARKET_DOC_URL,
"USERNAME": request.user.username,
# 'NICK': request.session.get('nick', ''), # 用户昵称
"NICK": request.user.username, # 用户昵称
Expand Down

0 comments on commit 5602c8e

Please sign in to comment.