class ArticleAdd(BaseHandler): @api_define('article_add', '/article/add', [ Param('title', True, 'str', 'API Docs', '标题'), Param('content', True, 'str', '一个构建Web API的工具', '内容'), Param('author', True, 'str', 'Lyon', '作者'), ], desc='添加文章') def post(self, request): title = self.data.get('title') content = self.data.get('content') author = self.data.get('author') if not all((title, content, author)): return self.write({ 'return_code': CODE.INVALID_PARAMETER, 'return_msg': MSG.INVALID_PARAMETER }) article = Article.create_data(title=title, content=content, author=author) if not article: return self.write({ 'return_code': CODE.FAIL, 'return_msg': MSG.FAIL }) return self.write({'return_code': CODE.SUCCESS, 'return_data': {}})
class UserLogin(RequestHandler): @define_api('/fund/user/login', [ Param('phone', True, str, '', '手机号'), Param('password', False, str, '', '密码'), ], desc='登录') async def post(self): phone = self.get_arg('phone') password = self.get_arg('password') user = await FundUser.async_get(phone=phone, password=password, is_delete=FundUser.DELETE_NO) if user: return self.write_success({'token': FundUser.encode_token(user)}) return self.write_fail(Code.User.USERNAME_OR_PASSWORD_INVALID, Message.User.USERNAME_OR_PASSWORD_INVALID)
class UserRegister(RequestHandler): @define_api('/fund/user/register', [ Param('phone', True, str, '', '手机号'), Param('password', True, str, '', '密码'), Param('name', True, str, '', '昵称'), ], desc='注册') async def post(self): phone = self.get_arg('phone') name = self.get_arg('name') password = self.get_arg('password') user = await FundUser.async_get(phone=phone, password=password, is_delete=FundUser.DELETE_NO) if user: return self.write_fail(Code.User.USER_IS_EXIST, Message.User.USER_IS_EXIST) user = await FundUser.async_create(phone=phone, name=name, password=password) data = {'token': user.encode_token()} return self.write_success(data)
class FundOptionalDetail(RequestHandler): @define_api('/fund/optional/detail', [ Param('code', True, str, '', '基金代码'), ], desc='自选基金详情') @login_required async def get(self): code = self.get_arg('code') records = FundHistoryNetWorth.query.find({ 'code': code, 'is_delete': FundHistoryNetWorth.DELETE_NO }).sort([('date', 1)]) data = dict() labels, series = list(), list() async for record in records: if not labels: data['fund_name'] = record['fund_name'] data['fund_name'] = record['fund_name'] series.append(round(int(record['net_worth']) / 1000, 4)) labels.append(dt.dt_to_str(record['date'], '%m-%d')) data = {'labels': labels, 'series': series} return self.write_success(data)
class ArticleDelete(BaseHandler): @api_define('article_delete', '/article/delete', [ Param('article_id', True, 'int', '1', '文章ID'), ], desc='删除文章') def post(self, request): article_id = self.data.get('article_id') article = Article.objects.filter(id=article_id).first() if not article: return self.write({ 'return_code': CODE.INVALID_PARAMETER, 'return_msg': MSG.INVALID_PARAMETER }) flag = Article.delete_data(article) if not flag: return self.write({ 'return_code': CODE.FAIL, 'return_msg': MSG.FAIL }) return self.write({ 'return_code': CODE.SUCCESS, 'return_data': { 'article_id': article_id } })
class FundOptionalDetail(RequestHandler): @define_api('/fund/optional/detail', [ Param('code', True, str, '', '基金代码'), ], desc='自选基金详情') @login_required async def get(self): code = self.get_arg('code') records = FundHistoryNetWorth.query.find({ 'code': code, 'is_delete': FundHistoryNetWorth.DELETE_NO }) data = list() async for record in records: data.append({ 'code': record['code'], 'fund_name': record['fund_name'], 'net_worth': record['net_worth'], 'date': dt.dt_to_str(record['date'], '%Y-%m-%d'), 'daily_growth_rate': record['daily_growth_rate'] }) return self.write_success(data)
class FundGet(RequestHandler): @define_api('/fund/get', [ Param('code', True, int, '', '基金代码'), ], desc='获取当前基金净值') async def get(self): net_worth = await FundData.get_current_net_worth(self.get_arg('code')) if net_worth is None: return self.write_fail(Code.Fund.NET_WORTH_NOT_EXIST, Message.Fund.NET_WORTH_NOT_EXIST) return self.write_success({'net_worth': net_worth})
class UserRegister(RequestHandler): @define_api('/fund/user/register', [ Param('phone', True, str, '', '手机号'), Param('password', True, str, '', '密码'), Param('name', True, str, '', '昵称'), ], desc='注册') async def post(self): phone = self.get_arg('phone') name = self.get_arg('name') password = self.get_arg('password') user = await FundUser.objects.get(FundUser.select().where( FundUser.phone == phone, FundUser.password == password)) if user: return self.write_fail(Code.USER_IS_EXIST, Message.USER_IS_EXIST) user = await FundUser.async_create(phone=phone, name=name, password=password) return self.write_success(Code.SUCCESS, {'token': self.token_encode(user.id)})
class UserLogin(RequestHandler): @define_api('/fund/user/login', [ Param('phone', True, str, '', '手机号'), Param('password', False, str, '', '密码'), ], desc='登录') async def post(self): phone = self.get_arg('phone') password = self.get_arg('password') try: user = await FundUser.objects.get(FundUser.select().where( FundUser.phone == phone, FundUser.password == password)) if user.id: return self.write_success( {'token': self.token_encode(user.id)}) except Exception as e: self.app.logger.info(e) return self.write_fail(Code.USERNAME_OR_PASSWORD_INVALID, Message.USERNAME_OR_PASSWORD_INVALID) return self.write_fail(Code.USERNAME_OR_PASSWORD_INVALID, Message.USERNAME_OR_PASSWORD_INVALID)
class FundOptionalDelete(RequestHandler): @define_api('/fund/optional/delete', [ Param('code', True, str, '', '基金代码'), ], desc='删除自选基金') @login_required async def post(self): code = self.get_arg('code') await FundOptionalRecord.objects.execute(FundOptionalRecord.update( {FundOptionalRecord.is_delete: FundOptionalRecord.DELETE_IS} ).where(FundOptionalRecord.code == code, FundOptionalRecord.user_id == self.user_id, FundOptionalRecord.is_delete == FundOptionalRecord.DELETE_NO)) return self.write_success()
class ArticleEdit(BaseHandler): @api_define('article_edit', '/article/edit', [ Param('article_id', True, 'int', '1', '文章ID'), Param('title', True, 'str', 'API Docs', '标题'), Param('content', True, 'str', '一个构建Web API的工具', '内容'), Param('author', True, 'str', 'Lyon', '作者'), ], desc='编辑文章') def post(self, request): article_id = self.data.get('article_id') title = self.data.get('title') content = self.data.get('content') author = self.data.get('author') if not all((title, content, author)): return self.write({ 'return_code': CODE.INVALID_PARAMETER, 'return_msg': MSG.INVALID_PARAMETER }) article = Article.objects.filter(id=article_id).first() if not article: return self.write({ 'return_code': CODE.INVALID_PARAMETER, 'return_msg': MSG.INVALID_PARAMETER }) flag = Article.edit_data(article, title, content, author) if not flag: return self.write({ 'return_code': CODE.FAIL, 'return_msg': MSG.FAIL }) return self.write({ 'return_code': CODE.SUCCESS, 'return_data': { 'article_id': article_id } })
class FundDelete(RequestHandler): @define_api('/fund/delete', [ Param('code', True, str, '', '基金代码'), ], desc='删除基金') @login_required async def post(self): code = self.get_arg('code') record = await TradeRecord.async_get(code=code, user_id=self.user_id, is_delete=TradeRecord.DELETE_NO) if not record: return self.write_bad_request() await record.async_delete() return self.write_success()
class TradeRecordDelete(RequestHandler): @define_api('/fund/trade/record/delete', [ Param('record_id', True, int, '', '记录id'), ], desc='删除交易记录') @login_required async def post(self): record_id = self.get_arg_int('record_id') await TradeRecord.objects.execute( TradeRecord.update({ TradeRecord.is_delete: TradeRecord.DELETE_IS }).where(TradeRecord.id == record_id, TradeRecord.user_id == self.user_id, TradeRecord.is_delete == TradeRecord.DELETE_NO)) return self.write_success()
class TradeRecordAdd(RequestHandler): @define_api('/fund/trade/record/add', [ Param('name', False, str, '', '基金名称'), Param('code', True, str, '', '基金代码'), Param('amount', True, int, '', '金额, x1000'), Param('date', True, str, '', '日期'), Param('type', True, int, '', '类型, 1: 买入, 2: 卖出'), ], desc='添加交易记录') @login_required async def post(self): code = self.get_arg('code') amount = self.get_arg_int('amount') date = self.get_arg('date') type = self.get_arg_int('type') net_worth = await FundData.get_date_net_worth(code, date) fund_name = await FundData.get_fund_name(code) if net_worth is None: return self.write_fail(Code.NET_WORTH_NOT_EXIST, Message.NET_WORTH_NOT_EXIST) copies = amount / net_worth / 1000 await TradeRecord.async_create(self.user_id, code, fund_name, amount, net_worth * 10000, copies, date, type) return self.write_success()
class TradeRecordDelete(RequestHandler): @define_api('/fund/trade/record/delete', [ Param('record_id', True, int, '', '记录id'), ], desc='删除交易记录') @login_required async def post(self): record_id = self.get_arg_int('record_id') record = await TradeRecord.async_get(id=record_id, user_id=self.user_id, is_delete=TradeRecord.DELETE_NO) if not record: return self.write_bad_request() await record.async_delete() return self.write_success()
class FundOptionalAdd(RequestHandler): @define_api('/fund/optional/add', [ Param('code', True, str, '', '基金代码'), ], desc='添加自选基金') @login_required async def post(self): code = self.get_arg('code') exist = await FundOptionalRecord.execute_sql( "SELECT code FROM fund_optional_record WHERE code = %s AND user_id = %s AND is_delete = %s", code, self.current_user_id, FundOptionalRecord.DELETE_NO) if exist: return self.write_fail(code=Code.FUND_IN_OPTIONAL_LIST, msg=Message.FUND_IN_OPTIONAL_LIST) fund_name = await FundData.get_fund_name(code) await FundOptionalRecord.async_create(self.current_user_id, code, fund_name) grab_last_month_data.delay(code) return self.write_success()