def get(self): self.parser.add_argument("page_num", type=int, location="args", default=1) self.parser.add_argument("page_size", type=int, location="args", default=10) args = self.parser.parse_args() try: profiles = ProfilesExampleModel.query.paginate(args.page_num, args.page_size, error_out=False) except SQLAlchemyError as e: current_app.logger.error(e) db.session.rollback() return pretty_result(Code.DB_ERROR, '数据库错误!') else: items = [] for i in profiles.items: items.append( { 'id': hash_ids.encode(i.id), 'nickname': i.nickname, 'signature': i.signature } ) data = { 'page_num': args.page_num, 'page_size': args.page_size, 'total': profiles.total, 'items': items } return pretty_result(Code.OK, data=data)
def _custom_abort(http_status_code, **kwargs): """自定义abort 400响应数据格式""" if http_status_code == 400: return abort( jsonify(pretty_result(Code.PARAM_ERROR, data=kwargs.get('message')))) return abort(http_status_code)
def post(self): self.parser.add_argument("nickname", type=str, location="json", required=True) self.parser.add_argument("signature", type=str, location="json", required=True) args = self.parser.parse_args() profile = ProfilesExampleModel(nickname=args.nickname, signature=args.signature) try: db.session.add(profile) db.session.commit() except SQLAlchemyError as e: current_app.logger.error(e) db.session.rollback() return pretty_result(Code.DB_ERROR, '数据库错误!') else: return pretty_result(Code.OK, '添加数据成功~')
def delete(id): id = hash_ids.decode(id) if not id: abort(404) try: profile = ProfilesExampleModel.query.get(id[0]) if not profile: abort(404) db.session.delete(profile) db.session.commit() except SQLAlchemyError as e: current_app.logger.error(e) db.session.rollback() return pretty_result(Code.DB_ERROR, '数据库错误!') else: return pretty_result(Code.OK, '删除数据成功~')
def get(id): id = hash_ids.decode(id) if not id: abort(404) try: profile = ProfilesExampleModel.query.get(id[0]) if not profile: abort(404) except SQLAlchemyError as e: current_app.logger.error(e) db.session.rollback() return pretty_result(Code.DB_ERROR, '数据库错误!') else: items = { 'id': hash_ids.encode(profile.id), 'nickname': profile.nickname, 'signature': profile.signature } return pretty_result(Code.OK, data=items)
def post(self): self.parser.add_argument("user", type=str, location="form", required=True) self.parser.add_argument("password", type=str, location="form", required=True) args = self.parser.parse_args(strict=True) return pretty_result(Code.OK, data='hello, %s!' % args.get('user'))
def put(self, id): self.parser.add_argument("nickname", type=str, location="json", required=True) self.parser.add_argument("signature", type=str, location="json", required=True) args = self.parser.parse_args() id = hash_ids.decode(id) if not id: abort(404) try: profile = ProfilesExampleModel.query.get(id[0]) if not profile: abort(404) profile.nickname = args.nickname profile.signature = args.signature db.session.add(profile) db.session.commit() except SQLAlchemyError as e: current_app.logger.error(e) db.session.rollback() return pretty_result(Code.DB_ERROR, '数据库错误!') else: return pretty_result(Code.OK, '修改数据成功~')
def get(self): app.logger.info('hello, world') return pretty_result(Code.OK, data='hello, world!')