Ejemplo n.º 1
0
    def create_or_update(type_id, name, attr_order, group_order=0):
        """
        create or update
        :param type_id:
        :param name:
        :param group_order: group order
        :param attr_order:
        :return:
        """
        existed = CITypeAttributeGroup.get_by(type_id=type_id,
                                              name=name,
                                              first=True,
                                              to_dict=False)
        existed = existed or CITypeAttributeGroup.create(
            type_id=type_id, name=name, order=group_order)
        existed.update(order=group_order)
        attr_order = dict(attr_order)
        current_app.logger.info(attr_order)
        existed_items = CITypeAttributeGroupItem.get_by(group_id=existed.id,
                                                        to_dict=False)
        for item in existed_items:
            if item.attr_id not in attr_order:
                item.soft_delete()
            else:
                item.update(order=attr_order[item.attr_id])

        existed_items = {item.attr_id: 1 for item in existed_items}
        for attr_id, order in attr_order.items():
            if attr_id not in existed_items:
                CITypeAttributeGroupItem.create(group_id=existed.id,
                                                attr_id=attr_id,
                                                order=order)

        return existed
Ejemplo n.º 2
0
    def update(cls, group_id, name, attr_order, group_order=0):
        group = CITypeAttributeGroup.get_by_id(group_id) or abort(404, "Group <{0}> does not exist".format(group_id))
        other = CITypeAttributeGroup.get_by(type_id=group.type_id, name=name, first=True, to_dict=False)
        if other is not None and other.id != group.id:
            return abort(400, "Group <{0}> duplicate".format(name))
        if name is not None:
            group.update(name=name)

        cls.create_or_update(group.type_id, name, attr_order, group_order)
Ejemplo n.º 3
0
    def transfer(cls, type_id, _from, _to):
        current_app.logger.info("CIType[{0}] {1} -> {2}".format(
            type_id, _from, _to))
        from_group = CITypeAttributeGroup.get_by_id(_from)
        from_group or abort(404, "Group <{0}> is not found".format(_from))

        to_group = CITypeAttributeGroup.get_by_id(_to)
        to_group or abort(404, "Group <{0}> is not found".format(_to))

        from_order, to_order = from_group.order, to_group.order

        from_group.update(order=to_order)
        to_group.update(order=from_order)

        CITypeAttributesCache.clean(type_id)

        from api.tasks.cmdb import ci_type_attribute_order_rebuild
        ci_type_attribute_order_rebuild.apply_async(args=(type_id, ),
                                                    queue=CMDB_QUEUE)
Ejemplo n.º 4
0
    def delete(group_id):
        group = CITypeAttributeGroup.get_by_id(group_id) \
                or abort(404, "AttributeGroup <{0}> does not exist".format(group_id))
        group.soft_delete()

        items = CITypeAttributeGroupItem.get_by(group_id=group_id, to_dict=False)
        for item in items:
            item.soft_delete()

        return group_id
Ejemplo n.º 5
0
def init_attribute_groups(num=1):
    ci_types = init_ci_types(num)

    ags = []
    for i in range(num):
        ags.append(
            CITypeAttributeGroup.create(name=uuid.uuid4().hex[:8],
                                        type_id=ci_types[i].id,
                                        order=i))
    return ags
Ejemplo n.º 6
0
    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