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

feat: 新增get_template_list接口支持标签过滤能力 #7603 #7610

19 changes: 19 additions & 0 deletions gcloud/apigw/views/get_template_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from gcloud.iam_auth.utils import get_flow_allowed_actions_for_user
from gcloud.iam_auth.view_interceptors.apigw import ProjectViewInterceptor
from apigw_manager.apigw.decorators import apigw_require
from gcloud.label.models import TemplateLabelRelation, Label


@login_exempt
Expand All @@ -40,6 +41,7 @@ def get_template_list(request, project_id):
template_source = request.GET.get("template_source", PROJECT)
id_in = request.GET.get("id_in", None)
name_keyword = request.GET.get("name_keyword", None)
label_names = request.GET.get("label_names", None)

if id_in:
try:
Expand All @@ -48,6 +50,19 @@ def get_template_list(request, project_id):
id_in = None
logger.exception("[API] id_in[{}] resolve fail, ignore.".format(id_in))

if label_names:
try:
label_names = label_names.split(",")
label_ids = Label.objects.filter(name__in=label_names).values_list("id", flat=True)
label_template_ids = TemplateLabelRelation.objects.fetch_template_ids_using_labels(label_ids)
guohelu marked this conversation as resolved.
Show resolved Hide resolved
label_ids = list(map(str, label_template_ids))
if id_in is None:
id_in = label_ids
else:
id_in = list(set(id_in + label_ids))
guohelu marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
logger.exception("[API] label_names[{}] resolve fail, ignore.".format(label_names))

filter_kwargs = dict(is_deleted=False)
if id_in:
filter_kwargs["id__in"] = id_in
Expand All @@ -72,4 +87,8 @@ def get_template_list(request, project_id):
if allowed:
template_info["auth_actions"].append(action)

templates_labels = TemplateLabelRelation.objects.fetch_templates_labels(template_id_list)
guohelu marked this conversation as resolved.
Show resolved Hide resolved
for obj in template_list:
obj["template_labels"] = templates_labels.get(obj["id"], [])

return {"result": True, "data": template_list, "code": err_code.SUCCESS.code}
2 changes: 2 additions & 0 deletions gcloud/tests/apigw/views/test_get_template_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_get_template_list__for_project_template(self):
"bk_biz_id": TEST_PROJECT_ID,
"bk_biz_name": TEST_PROJECT_NAME,
"auth_actions": ["TEST_ACTION"],
"template_labels": [],
}
for tmpl in task_templates
]
Expand Down Expand Up @@ -151,6 +152,7 @@ def test_get_template_list__for_common_template(self):
"bk_biz_id": TEST_PROJECT_ID,
"bk_biz_name": TEST_PROJECT_NAME,
"auth_actions": [],
"template_labels": [],
}
for tmpl in task_templates
]
Expand Down
Loading