[fix] update attribute which is list

This commit is contained in:
pycook 2019-12-11 18:12:10 +08:00
parent a71ba83de0
commit 8341e742eb
2 changed files with 31 additions and 26 deletions

View File

@ -10,7 +10,6 @@ from api.lib.cmdb.attribute import AttributeManager
from api.lib.cmdb.cache import AttributeCache from api.lib.cmdb.cache import AttributeCache
from api.lib.cmdb.const import ExistPolicy from api.lib.cmdb.const import ExistPolicy
from api.lib.cmdb.const import OperateType from api.lib.cmdb.const import OperateType
from api.lib.cmdb.const import ValueTypeEnum
from api.lib.cmdb.history import AttributeHistoryManger from api.lib.cmdb.history import AttributeHistoryManger
from api.lib.cmdb.utils import TableMap from api.lib.cmdb.utils import TableMap
from api.lib.cmdb.utils import ValueTypeMap from api.lib.cmdb.utils import ValueTypeMap
@ -119,29 +118,33 @@ class AttributeValueManager(object):
return abort(400, 'attribute {0} does not exist'.format(key)) return abort(400, 'attribute {0} does not exist'.format(key))
value_table = TableMap(attr_name=attr.name).table value_table = TableMap(attr_name=attr.name).table
existed_attr = value_table.get_by(attr_id=attr.id,
ci_id=ci_id,
first=True,
to_dict=False)
existed_value = existed_attr and existed_attr.value
operate_type = OperateType.ADD if existed_attr is None else OperateType.UPDATE
value_list = handle_arg_list(value) if attr.is_list else [value] if attr.is_list:
if not isinstance(value, list): value_list = [self._validate(attr, i, value_table, ci_id) for i in handle_arg_list(value)]
value_list = [value] existed_attrs = value_table.get_by(attr_id=attr.id,
ci_id=ci_id,
to_dict=False)
existed_values = [i.value for i in existed_attrs]
added = set(value_list) - set(existed_values)
deleted = set(existed_values) - set(value_list)
for v in added:
value_table.create(ci_id=ci_id, attr_id=attr.id, value=v)
self._write_change(ci_id, attr.id, OperateType.ADD, None, v)
for v in value_list: for v in deleted:
v = self._validate(attr, v, value_table, ci_id) existed_attr = existed_attrs[existed_values.index(v)]
if not v and attr.value_type != ValueTypeEnum.TEXT: existed_attr.delete()
v = None self._write_change(ci_id, attr.id, OperateType.DELETE, v, None)
else:
if operate_type == OperateType.ADD: value = self._validate(attr, value, value_table, ci_id)
if v is not None: existed_attr = value_table.get_by(attr_id=attr.id,
value_table.create(ci_id=ci_id, attr_id=attr.id, value=v) ci_id=ci_id,
self._write_change(ci_id, attr.id, operate_type, None, v) first=True,
elif existed_attr.value != v: to_dict=False)
if v is not None: existed_value = existed_attr and existed_attr.value
existed_attr.update(value=v) if existed_value is None:
else: value_table.create(ci_id=ci_id, attr_id=attr.id, value=value)
existed_attr.delete() self._write_change(ci_id, attr.id, OperateType.ADD, None, value)
self._write_change(ci_id, attr.id, operate_type, existed_value, v) else:
existed_attr.update(value=value)
self._write_change(ci_id, attr.id, OperateType.UPDATE, existed_value, value)

View File

@ -40,8 +40,10 @@ export default {
joinList: function (itemValue) { joinList: function (itemValue) {
if (typeof itemValue === 'object' && itemValue) { if (typeof itemValue === 'object' && itemValue) {
return itemValue.join(',') return itemValue.join(',')
} else if (itemValue !== null) {
return itemValue + ''
} else { } else {
return itemValue || ' ' return ''
} }
} }
} }