Пример #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
Пример #2
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