def get_by_type_id(type_id, need_other=None): groups = CITypeAttributeGroup.get_by(type_id=type_id) groups = sorted(groups, key=lambda x: x["order"]) grouped = list() for group in groups: items = CITypeAttributeGroupItem.get_by(group_id=group["id"], to_dict=False) items = sorted(items, key=lambda x: x.order) group["attributes"] = [ AttributeCache.get(i.attr_id).to_dict() for i in items ] grouped.extend([i.attr_id for i in items]) if need_other is not None: grouped = set(grouped) attributes = CITypeAttributeManager.get_attributes_by_type_id( type_id) other_attributes = [ attr for attr in attributes if attr["id"] not in grouped ] groups.append(dict(attributes=other_attributes)) return groups
def delete(ci_id): ci = CI.get_by_id(ci_id) or abort(404, "CI <{0}> is not found".format(ci_id)) attrs = CITypeAttribute.get_by(type_id=ci.type_id, to_dict=False) attr_names = set([AttributeCache.get(attr.attr_id).name for attr in attrs]) for attr_name in attr_names: value_table = TableMap(attr_name=attr_name).table for item in value_table.get_by(ci_id=ci_id, to_dict=False): item.delete() for item in CIRelation.get_by(first_ci_id=ci_id, to_dict=False): item.delete() for item in CIRelation.get_by(second_ci_id=ci_id, to_dict=False): item.delete() ci.delete() # TODO: soft delete AttributeHistoryManger.add(ci_id, [(None, OperateType.DELETE, None, None)]) ci_delete.apply_async([ci.id], queue=CMDB_QUEUE) return ci_id
def create_or_update_show_attributes(cls, type_id, attr_order): existed_all = PreferenceShowAttributes.get_by(type_id=type_id, uid=g.user.uid, to_dict=False) for _attr, order in attr_order: attr = AttributeCache.get(_attr) or abort( 404, "Attribute <{0}> does not exist".format(_attr)) existed = PreferenceShowAttributes.get_by(type_id=type_id, uid=g.user.uid, attr_id=attr.id, first=True, to_dict=False) if existed is None: PreferenceShowAttributes.create(type_id=type_id, uid=g.user.uid, attr_id=attr.id, order=order) else: existed.update(order=order) attr_dict = {int(i): j for i, j in attr_order} for i in existed_all: if i.attr_id not in attr_dict: i.soft_delete()
def _sort_handler(sort_by, query_sql): if sort_by.startswith("+"): sort_type = "asc" sort_by = sort_by[1:] elif sort_by.startswith("-"): sort_type = "desc" sort_by = sort_by[1:] else: sort_type = "asc" attr = AttributeCache.get(sort_by) if attr is None: return query_sql attr_id = attr.id value_table = TableMap(attr_name=sort_by).table ci_table = query_sql.subquery() query_sql = db.session.query(ci_table.c.id, value_table.value).join( value_table, value_table.ci_id == ci_table.c.id).filter( value_table.attr_id == attr_id).filter(ci_table.deleted.is_(False)).order_by( getattr(value_table.value, sort_type)()) return query_sql
def update(cls, type_id, **kwargs): ci_type = cls.check_is_existed(type_id) cls._validate_unique(type_id=type_id, name=kwargs.get('name')) cls._validate_unique(type_id=type_id, alias=kwargs.get('alias')) unique_key = kwargs.pop("unique_key", None) unique_key = AttributeCache.get(unique_key) if unique_key is not None: kwargs["unique_id"] = unique_key.id type_attr = CITypeAttribute.get_by(type_id=type_id, attr_id=unique_key.id, first=True, to_dict=False) if type_attr is None: CITypeAttributeManager.add(type_id, [unique_key.id], is_required=True) ci_type.update(**kwargs) CITypeCache.clean(type_id) return type_id
def get_attr_names_by_type_id(type_id): return [ AttributeCache.get(attr.attr_id).name for attr in CITypeAttributesCache.get(type_id) ]
def _get_attr(key): """ :param key: id, name or alias :return: attribute instance """ return AttributeCache.get(key)
def delete_attr_value(attr_id, ci_id): attr = AttributeCache.get(attr_id) if attr is not None: value_table = TableMap(attr_name=attr.name).table for item in value_table.get_by(attr_id=attr.id, ci_id=ci_id, to_dict=False): item.delete()
def get_attribute(self, key): attr = AttributeCache.get(key).to_dict() if attr and attr["is_choice"]: attr.update(dict(choice_value=self.get_choice_values(attr["id"], attr["value_type"]))) return attr
def table_name(self): attr = AttributeCache.get(self.attr_name) i = "index_{0}".format( attr.value_type) if attr.is_index else attr.value_type return type_map["table_name"].get(i)
def table_name(self): attr = AttributeCache.get(self.attr_name) i = "index_{0}".format(attr.value_type) if attr.is_index else attr.value_type return ValueTypeMap.table_name.get(i)