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_item(cls, group_id, attr_id, order): db.session.remove() existed = CITypeAttributeGroupItem.get_by(group_id=group_id, attr_id=attr_id, first=True, to_dict=False) existed or abort( 404, "Group<{0}> - Attribute<{1}> is not found".format( group_id, attr_id)) if existed.order > order: # forward, +1 items = db.session.query(CITypeAttributeGroupItem).filter( CITypeAttributeGroupItem.deleted.is_(False)).filter( CITypeAttributeGroupItem.order >= order).filter( CITypeAttributeGroupItem.order < existed.order) for item in items: item.update(order=item.order + 1) elif existed.order < order: # backward, -1 items = db.session.query(CITypeAttributeGroupItem).filter( CITypeAttributeGroupItem.deleted.is_(False)).filter( CITypeAttributeGroupItem.order > existed.order).filter( CITypeAttributeGroupItem.order <= order) for item in items: item.update(order=item.order - 1) existed.update(order=order)
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
def add_item(cls, group_id, attr_id, order): db.session.remove() existed = CITypeAttributeGroupItem.get_by(group_id=group_id, attr_id=attr_id, first=True, to_dict=False) if existed is not None: existed.update(order=order) else: CITypeAttributeGroupItem.create(group_id=group_id, attr_id=attr_id, order=order) gt_items = db.session.query(CITypeAttributeGroupItem).filter( CITypeAttributeGroupItem.deleted.is_(False)).filter( CITypeAttributeGroupItem.order > order) for _item in gt_items: _order = _item.order _item.update(order=_order + 1)
def delete_item(cls, group_id, attr_id): db.session.remove() item = CITypeAttributeGroupItem.get_by(group_id=group_id, attr_id=attr_id, first=True, to_dict=False) if item is not None: item.soft_delete() order = item.order gt_items = db.session.query(CITypeAttributeGroupItem).filter( CITypeAttributeGroupItem.deleted.is_(False)).filter( CITypeAttributeGroupItem.order > order) for _item in gt_items: _order = _item.order _item.update(order=_order - 1)
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(_id): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) name = attr.name if attr.is_choice: choice_table = ValueTypeMap.choice.get(attr.value_type) db.session.query(choice_table).filter(choice_table.attr_id == _id).delete() # FIXME: session conflict db.session.flush() AttributeCache.clean(attr) attr.soft_delete() for i in CITypeAttribute.get_by(attr_id=_id, to_dict=False): i.soft_delete() for i in PreferenceShowAttributes.get_by(attr_id=_id, to_dict=False): i.soft_delete() for i in CITypeAttributeGroupItem.get_by(attr_id=_id, to_dict=False): i.soft_delete() return name