fix upload template and add /api/v0.1/attributes/<int:attr_id>/calc_computed_attribute

This commit is contained in:
pycook 2023-09-11 19:15:31 +08:00
parent 2c74d107dd
commit 94cdb42477
3 changed files with 29 additions and 21 deletions

View File

@ -163,13 +163,15 @@ class AttributeManager(object):
if RoleEnum.CONFIG not in session.get("acl", {}).get("parentRoles", []) and not is_app_admin('cmdb'): if RoleEnum.CONFIG not in session.get("acl", {}).get("parentRoles", []) and not is_app_admin('cmdb'):
return abort(403, ErrFormat.role_required.format(RoleEnum.CONFIG)) return abort(403, ErrFormat.role_required.format(RoleEnum.CONFIG))
@staticmethod @classmethod
def calc_computed_attribute(attr_id): def calc_computed_attribute(cls, attr_id):
""" """
calculate computed attribute for all ci calculate computed attribute for all ci
:param attr_id: :param attr_id:
:return: :return:
""" """
cls.can_create_computed_attribute()
from api.tasks.cmdb import calc_computed_attribute from api.tasks.cmdb import calc_computed_attribute
calc_computed_attribute.apply_async(args=(attr_id, current_user.uid), queue=CMDB_QUEUE) calc_computed_attribute.apply_async(args=(attr_id, current_user.uid), queue=CMDB_QUEUE)

View File

@ -1,4 +1,4 @@
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
import copy import copy
import datetime import datetime
@ -114,7 +114,7 @@ class CITypeManager(object):
@kwargs_required("name") @kwargs_required("name")
def add(cls, **kwargs): def add(cls, **kwargs):
unique_key = kwargs.pop("unique_key", None) unique_key = kwargs.pop("unique_key", None) or kwargs.pop("unique_id", None)
unique_key = AttributeCache.get(unique_key) or abort(404, ErrFormat.unique_key_not_define) unique_key = AttributeCache.get(unique_key) or abort(404, ErrFormat.unique_key_not_define)
kwargs["alias"] = kwargs["name"] if not kwargs.get("alias") else kwargs["alias"] kwargs["alias"] = kwargs["name"] if not kwargs.get("alias") else kwargs["alias"]
@ -276,10 +276,10 @@ class CITypeGroupManager(object):
def update(gid, name, type_ids): def update(gid, name, type_ids):
""" """
update part update part
:param gid: :param gid:
:param name: :param name:
:param type_ids: :param type_ids:
:return: :return:
""" """
existed = CITypeGroup.get_by_id(gid) or abort( existed = CITypeGroup.get_by_id(gid) or abort(
404, ErrFormat.ci_type_group_not_found.format("id={}".format(gid))) 404, ErrFormat.ci_type_group_not_found.format("id={}".format(gid)))
@ -386,10 +386,10 @@ class CITypeAttributeManager(object):
def add(cls, type_id, attr_ids=None, **kwargs): def add(cls, type_id, attr_ids=None, **kwargs):
""" """
add attributes to CIType add attributes to CIType
:param type_id: :param type_id:
:param attr_ids: list :param attr_ids: list
:param kwargs: :param kwargs:
:return: :return:
""" """
attr_ids = list(set(attr_ids)) attr_ids = list(set(attr_ids))
@ -416,9 +416,9 @@ class CITypeAttributeManager(object):
def update(cls, type_id, attributes): def update(cls, type_id, attributes):
""" """
update attributes to CIType update attributes to CIType
:param type_id: :param type_id:
:param attributes: list :param attributes: list
:return: :return:
""" """
cls._check(type_id, [i.get('attr_id') for i in attributes]) cls._check(type_id, [i.get('attr_id') for i in attributes])
@ -446,9 +446,9 @@ class CITypeAttributeManager(object):
def delete(cls, type_id, attr_ids=None): def delete(cls, type_id, attr_ids=None):
""" """
delete attributes from CIType delete attributes from CIType
:param type_id: :param type_id:
:param attr_ids: list :param attr_ids: list
:return: :return:
""" """
from api.tasks.cmdb import ci_cache from api.tasks.cmdb import ci_cache
@ -823,6 +823,12 @@ class CITypeTemplateManager(object):
for added_id in set(id2obj_dicts.keys()) - set(existed_ids): for added_id in set(id2obj_dicts.keys()) - set(existed_ids):
if cls == CIType: if cls == CIType:
CITypeManager.add(**id2obj_dicts[added_id]) CITypeManager.add(**id2obj_dicts[added_id])
elif cls == CITypeRelation:
CITypeRelationManager.add(id2obj_dicts[added_id].get('parent_id'),
id2obj_dicts[added_id].get('child_id'),
id2obj_dicts[added_id].get('relation_type_id'),
id2obj_dicts[added_id].get('constraint'),
)
else: else:
cls.create(flush=True, **id2obj_dicts[added_id]) cls.create(flush=True, **id2obj_dicts[added_id])

View File

@ -56,12 +56,7 @@ class AttributeView(APIView):
@args_required("name") @args_required("name")
@args_validate(AttributeManager.cls) @args_validate(AttributeManager.cls)
def post(self, attr_id=None): def post(self):
if request.url.endswith("/calc_computed_attribute"):
AttributeManager.calc_computed_attribute(attr_id)
return self.jsonify(attr_id=attr_id)
choice_value = handle_arg_list(request.values.get("choice_value")) choice_value = handle_arg_list(request.values.get("choice_value"))
params = request.values params = request.values
params["choice_value"] = choice_value params["choice_value"] = choice_value
@ -74,6 +69,11 @@ class AttributeView(APIView):
@args_validate(AttributeManager.cls) @args_validate(AttributeManager.cls)
def put(self, attr_id): def put(self, attr_id):
if request.url.endswith("/calc_computed_attribute"):
AttributeManager.calc_computed_attribute(attr_id)
return self.jsonify(attr_id=attr_id)
choice_value = handle_arg_list(request.values.get("choice_value")) choice_value = handle_arg_list(request.values.get("choice_value"))
params = request.values params = request.values
params["choice_value"] = choice_value params["choice_value"] = choice_value