def save_entity(model, id, base_data, data): model_cls = build_model(model) if id == -1: base_entity = Entity() entity = model_cls() else: base_entity = get_base_entity(id) entity = getattr(base_entity, model.name) populate(base_entity, base_data, Entity) populate(entity, data, model_cls) for i in range(len(base_entity.categories)-1,-1,-1): del base_entity.categories[i] for cid in base_data.cids: if cid: base_entity.categories.append(get_category(int(cid))) if id == -1: base_entity.model = model entity.entity = base_entity web.ctx.orm.add(base_entity) web.ctx.orm.add(entity) else: web.ctx.orm.flush()
def get_entities(model, cid=None, limit=None): model_cls = build_model(model) if cid is None: return web.ctx.orm.query(model_cls).order_by(model_cls.id.desc()).all() else: if limit is None: return web.ctx.orm.query(model_cls).filter(model_cls.categories.any(id=cid)).order_by(model_cls.id.desc()).all() else: return web.ctx.orm.query(model_cls).filter(model_cls.categories.any(id=cid)).order_by(model_cls.id.desc()).limit(limit)
def get_entity(model, id): model_cls = build_model(model) return web.ctx.orm.query(model_cls).get(id)
def create(): session = DBSession() model = session.query(Model).get(1) model_cls = build_model(model) create_table(model_cls)
def drop(): session = DBSession() model = session.query(Model).get(1) model_cls = build_model(model) drop_table(model_cls)