咱们接着上次的lesson1 接着往下看,首先我们已经利用Flask 创建了 简单的Restful实现 接下去,我们将要看到如何加入数据库
Double Kill:
先上代码,view.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 from app import appfrom datetime import datetimefrom models import Taskfrom app import dbfrom flask import jsonify,request,abort,make_responseimport json@app.errorhandler(404) def not_found (error) : return make_response(jsonify( { 'error' :'Not Found 404' } ),404 ) @app.errorhandler(400) def error_400 (error) : return make_response(jsonify( { 'error' :'Error 400' } ),400 ) @app.route('/') @app.route('/index') def index () : return "hello world" @app.route('/todo/api/v1.0/tasks', methods = ['GET']) def get_tasks () : tasks = Task.query.all() print tasks return jsonify(json_list = [i.to_json() for i in tasks]) @app.route('/todo/api/v1.0/tasks', methods = ['POST']) def create_task () : if not request.json or not 'title' in request.json: abort(400 ) task = Task(title = request.json['title' ],description=request.json.get('description' , "" ),done=False ) db.session.add(task) db.session.commit() return jsonify( task = task.to_json() ), 201 @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods = ['GET']) def get_task (task_id) : task = Task.query.filter_by(id = task_id).first() if task == None : abort(404 ) return jsonify( task = task.to_json() ) @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods = ['PUT']) def update_task (task_id) : task = Task.query.filter_by(id = task_id).first() if task == None : abort(404 ) if not request.json: abort(400 ) if 'title' in request.json and type(request.json['title' ]) != unicode: abort(400 ) if 'description' in request.json and type(request.json['description' ]) is not unicode: abort(400 ) if 'done' in request.json and type(request.json['done' ]) is not bool: abort(400 ) task.title = request.json.get('title' , task.title) task.description = request.json.get('description' , task.description) task.done = request.json.get('done' , task.done) db.session.flush() db.session.commit() return jsonify( task = task.to_json() ) @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods = ['DELETE']) def delete_task (task_id) : task = Task.query.filter_by(id = task_id).first() if task == None : abort(404 ) db.session.delete(task) db.session.commit() return jsonify( { 'result' : True } )
models.py:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 from app import dbROLE_USER = 0 ROLE_ADMIN = 1 class User (db.Model) : id = db.Column(db.Integer, primary_key = True ) nickname = db.Column(db.String(64 ), index = True , unique = True ) email = db.Column(db.String(120 ), index = True , unique = True ) role = db.Column(db.SmallInteger, default = ROLE_USER) def __repr__ (self) : return '<User %r>' % (self.nickname) class Task (db.Model) : id = db.Column(db.Integer, primary_key = True ) title = db.Column(db.String(50 )) description = db.Column(db.String(120 )) done = db.Column(db.Boolean,default= False ) def __repr__ (self) : return '<Task %r>' % (self.title) def to_json (self) : return { 'id' : self.id, 'title' : self.title, 'description' : self.description, 'done' :self.done }
这里就是利用Sqlalchemy链接数据库,Python的ORM,大家可以去了解下,我其实也不太了解,反正这样子就实现了与数据库的链接
感谢鼓励