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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| from flask import Flask,request from flask.ext.restful import reqparse, abort, Api, Resource from monitorDB import db,Piclist,User
app = Flask(__name__) api = Api(app)
#Deal with data form parser = reqparse.RequestParser() parser.add_argument('picid', type=int) parser.add_argument('userid', type=int) parser.add_argument('status', type=int) parser.add_argument('username', type=str) parser.add_argument('password', type=str) parser.add_argument('email', type=str)
#get piclist def getPiclist(): lim = request.args.get('limit', 10) off = request.args.get('offset', 0) results = Piclist.query.limit(lim).offset(off).all()
json_results = [] for result in results: d = {'picid': result.id, 'picname': result.picname, 'time': result.time, 'status':result.status } json_results.append(d)
return json_results
#get userlist def getUserlist(): lim = request.args.get('limit', 10) off = request.args.get('offset', 0) results = User.query.limit(lim).offset(off).all()
json_results = [] for result in results: d = {'id': result.id, 'username': result.username, 'password': result.password, 'email':result.email } json_results.append(d)
return json_results
def abort_if_pic_doesnt_exist(picid): if (Piclist.query.filter_by(id=picid).first() == None): abort(404, message="Pic {} doesn't exist".format(picid))
def abort_if_user_id_doesnt_exist(userid): if (User.query.filter_by(id=userid).first() == None): abort(404, message="User {} doesn't exist".format(userid))
# Pic # shows a single Pic item and lets you change or delete item class pic(Resource): def get(self, picid): abort_if_pic_doesnt_exist(picid) result = Piclist.query.filter_by(id=picid).first() json_result = {'id': result.id, 'picname': result.picname, 'time': result.time, 'status':result.status }
return json_result
def delete(self, picid): abort_if_pic_doesnt_exist(picid) result = Piclist.query.filter_by(id=picid).first() db.session.delete(result) db.session.commit() return 204
def put(self, picid): args = parser.parse_args() status = args['status'] pic = Piclist.query.filter_by(id=picid).first() pic.status = status db.session.commit() return 201
# shows a list of all piclist class piclists(Resource): def get(self): return getPiclist()
# show a user class user(Resource):
def get(self, userid): abort_if_user_id_doesnt_exist(userid) result = User.query.filter_by(id=userid).first() json_result = {'id': result.id, 'username': result.username, 'password': result.password, 'email':result.email } return json_result
def delete(self, userid): abort_if_user_id_doesnt_exist(userid) result = User.query.filter_by(id=userid).first() db.session.delete(result) db.session.commit() return 204
def put(self, userid): abort_if_user_id_doesnt_exist(userid) args = parser.parse_args() user = User.query.filter_by(id=userid).first() user.userid = userid user.username = args['username'] user.password = args['password'] user.email = args['email'] db.session.add(user) db.session.commit() return 201
# shows a list of all Users class userlists(Resource): def get(self): return getUserlist()
def post(self): args = parser.parse_args() user = User(args['username'],args['password'],args['email']) db.session.add(user) db.session.commit() return 201
## Actually setup the Api resource routing here api.add_resource(piclists, '/monitor/api/v1.0/piclist/') api.add_resource(pic, '/monitor/api/v1.0/piclist/<int:picid>') api.add_resource(userlists, '/monitor/api/v1.0/user/') api.add_resource(user, '/monitor/api/v1.0/user/<int:userid>')
if __name__ == '__main__': app.run(host='0.0.0.0',debug=True)
|