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
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)
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