article_bp = Blueprint('article', __name__, url_prefix='/api')
api = Api(article_bp)
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, help='必须填写名称', required=True)
channel_fields = {
'id': fields.Integer,
'cname': fields.String
}
class ChannelResource(Resource):
def get(self):
channels = Channel.query.all()
return marshal(channels, channel_fields)
def post(self):
args = parser.parse_args()
if args:
channel = Channel()
channel.cname = args.get('name')
channel.save()
return {'msg': '频道添加成功', 'channel': marshal(channel, channel_fields)}
else:
return {'msg': '频道添加失败'}