def get_posts(cls, user, limit=20, offset=0, post_type=0): if not user.is_admin(): cond = (cls.deleted_at == 0) & (cls.post_type == post_type) else: cond = cls.post_type == post_type if post_type == cls.DIARY: cond &= (cls.user_id == user.id) total = db_session.query(func.count(cls.id)).filter(cond).scalar() items = db_session.query(cls).filter(cond).order_by( cls.id).offset(offset).limit(limit).all() return total, items
def delete(self, user_id, *args, **kwargs): """ TODO 删除一个用户需要删除其 posts, todos, todo_items TODO 都是软删除,即将deleted_at置为当前时间戳 :param user_id: :param args: :param kwargs: :return: """ query_user = User.find_by_id(user_id) if not query_user: return self.error(MESSAGES[404], status_code=404) db_session.query(User).update({User.deleted_at: generate_timestamp()}) db_session.commit() return self.data(query_user.to_dict())
def get_todo_by_user(cls, user_id, limit=20, offset=0, is_admin=False): if not is_admin: cond = (cls.deleted_at == 0) & (cls.user_id == user_id) else: cond = cls.user_id == user_id total = db_session.query(func.count(cls.id)).filter(cond).scalar() items = db_session.query(cls).filter(cond).order_by( cls.date).offset(offset).limit(limit).all() items = [item.to_dict() for item in items] # 非管理员无法查看到已经删除的todo_item子项 if not is_admin: for todo in items: todo['todo_items'] = [ todo_item for todo_item in todo['todo_items'] if todo_item.deleted_at == 0 ] return total, items
def get_todo_item_by_id(cls, todo_item_id): return db_session.query(cls).filter(cls.id == todo_item_id).first()
def find_post_by_id(cls, post_id): return db_session.query(cls).filter(cls.id == post_id).first()
def get_users(cls, limit=20, offset=0): return db_session.query(cls).offset(offset).order_by(cls.id).limit(limit).all()
def find_by_name(cls, username): return db_session.query(cls).filter_by(username=username).first()
def find_by_id(cls, user_id): return db_session.query(cls).filter_by(id=user_id).first()