Exemple #1
0
 def get_attributes_by_type_id(type_id):
     attrs = CITypeAttributeCache.get(type_id)
     result = list()
     for attr in sorted(attrs, key=lambda x: (x.order, x.id)):
         attr_dict = AttributeManager().get_attribute(attr.attr_id)
         attr_dict["is_required"] = attr.is_required
         attr_dict["order"] = attr.order
         attr_dict["default_show"] = attr.default_show
         result.append(attr_dict)
     return result
Exemple #2
0
def ci_type_attribute_order_rebuild(type_id):
    current_app.logger.info('rebuild attribute order')
    db.session.remove()

    from api.lib.cmdb.ci_type import CITypeAttributeGroupManager

    attrs = CITypeAttributeCache.get(type_id)
    id2attr = {attr.attr_id: attr for attr in attrs}

    res = CITypeAttributeGroupManager.get_by_type_id(type_id, True)
    order = 0
    for group in res:
        for _attr in group.get('attributes'):
            if order != id2attr.get(_attr['id']) and id2attr.get(_attr['id']):
                id2attr.get(_attr['id']).update(order=order)

            order += 1
Exemple #3
0
    def create_or_update_tree_view(type_id, levels):
        attrs = CITypeAttributeCache.get(type_id)
        for idx, i in enumerate(levels):
            for attr in attrs:
                attr = AttributeCache.get(attr.attr_id)
                if i == attr.id or i == attr.name or i == attr.alias:
                    levels[idx] = str(attr.id)
        levels = ",".join(levels)

        existed = PreferenceTreeView.get_by(uid=g.user.uid,
                                            type_id=type_id,
                                            to_dict=False,
                                            first=True)
        if existed is not None:
            if not levels:
                existed.soft_delete()
                return existed
            return existed.update(levels=levels)
        elif levels:
            return PreferenceTreeView.create(levels=levels,
                                             type_id=type_id,
                                             uid=g.user.uid)
Exemple #4
0
 def __check_is_required(type_id, attr, value):
     type_attr = CITypeAttributeCache.get(type_id, attr.id)
     if type_attr and type_attr.is_required and not value and value != 0:
         return abort(400, "attribute <{0}> value is required".format(attr.alias))
Exemple #5
0
 def get_attr_names_by_type_id(type_id):
     return [
         AttributeCache.get(attr.attr_id).name
         for attr in CITypeAttributeCache.get(type_id)
     ]