comment——models——bank_card.py
一、创建模型类、映射到数据库中
from comment.models import db
from sqlalchemy import ForeignKey
# 银行卡信息的模型类
class BankCard(db.Model):
__tablename__ = 't_bankcard'
bankInfoId = db.Column(db.Integer, primary_key=True, autoincrement=True)
bankCardNum = db.Column(db.String(64), comment='银行卡号')
holder=db.Column(db.String(64),comment='账户名')
openingBank = db.Column(db.String(64), comment='开户银行')
cityId = db.Column(db.Integer, comment='城市id')
userId = db.Column(db.BIGINT, ForeignKey('t_user.id'), comment='用户表主键')
user = db.relationship('User', backref=db.backref('bankCards', lazy=True))
bankId = db.Column(db.Integer, comment='银行编号')
bankBranch = db.Column(db.String(64), comment='银行支行')
reservePhone = db.Column(db.String(64), comment='绑定手机号码')
set FLASK_APP=financial/main.py
flask db migrate
flask db upgrade
二、创建蓝图、加载资源
from flask_restful import Resource,Api
from flask import Blueprint
from comment.utils.output import output_json
#创建蓝图
card_bp=Blueprint('card',__name__,url_prefix='/cards')
#创建restfui风格的api
card_api=Api(card_bp)
#将资源类中的响应数据转化成json格式的数据
card_api.representation('application/json')(output_json)
from financial.resource.card.card_resource import *
#加载资源
card_api.add_resource(Card_Resource,'/card',endpoint='card')
三、创建资源
from flask_restful import Resource
from flask import g,request,current_app
from flask_restful import reqparse
from comment.models.bank_card import BankCard
from comment.models.user import User
from comment.models import db
from comment.utils.decorators import login_required
class Card_Resource(Resource):
'''
银行卡管理的资源类
'''
method_decorators = [login_required]
def post(self):
'''
添加银行卡
:return:
'''
uid=g.user_id
login_user=User.query.filter(User.id==uid).first()
rp=reqparse.RequestParser()
rp.add_argument('holder', required=True) #账户名字
rp.add_argument('openingBank', required=True) # 开户银行
rp.add_argument('bankBranch', required=True) # 支行
rp.add_argument('cityId', required=True) # 城市id
rp.add_argument('cardNum', required=True) # 卡片号码
args = rp.parse_args()
holder=args.holder
opening_Bank = args.openingBank
bank_Branch = args.bankBranch
city_Id = args.cityId
bank_Card_Num = args.cardNum
#验证银行卡卡号是否唯一
card_info=BankCard.query.filter(BankCard.bankCardNum==bank_Card_Num).first()
if card_info:
return {'message':'银行卡已经存在,请勿重复添加','code':2001}
#把银行卡数据保存到数据库中
card=BankCard(bankCardNum=bank_Card_Num,openingBank=opening_Bank,holder=holder,
bankBranch=bank_Branch,cityId=city_Id,userId=uid,reservePhone=login_user.phone)
db.session.add(card)
db.session.commit()
return {'msg':'success'}
def get(self):
'''
查询当前登录用户所登录用户的所有银行卡
:return:
'''
uid=g.user_id
card_list=BankCard.query.filter(BankCard.userId==uid).all()
from financial.resource.card.serializer import BankCardListSerializer
if card_list: #把对象转换成字典数据
return BankCardListSerializer(card_list).to_dict()
else:
return {}
def delete(self):
'''
删除银行卡
:return:
'''
rp=reqparse.RequestParser()
rp.add_argument('carNum',required=True)
args=rp.parse_args()
carNum=args.carNum
bank_info=BankCard.query.filter(BankCard.bankCardNum==carNum).first()
if bank_info:
card_info.delete()
db.session.commit()
return {'msg':'success'}
else:
return {'code':2001,'message':'no this card'}
查询当前登录用户所的所有银行卡,不需要进行分页操作,但是需要进行序列化操作,将对象转化为字典格式的数据
四、定义银行卡列表数据的序列化的类
financial——resource——card——serializer.py
from comment.utils.serializers import BaseListSerializer
class BankCardListSerializer(BaseListSerializer):
'''
银行卡列表数据的序列化的类
'''
def to_dict(self):
lst=[]
for card in self.data_list:
lst.append(
{
'cardId':card.bankInfoId,
'cardNum':card.bankCardNum,
'bankName':card.openingBank
}
)
return lst
五、测试
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/74107.html