|
|
@@ -1,7 +1,212 @@
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
from flask import Flask
|
|
|
+from flask_restful import Resource, Api
|
|
|
+
|
|
|
+from bson import json_util
|
|
|
+import json
|
|
|
+
|
|
|
+import pymysql
|
|
|
+
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
+api = Api(app)
|
|
|
+
|
|
|
+class Database:
|
|
|
+ def __init__(self):
|
|
|
+ host = "127.0.0.1"
|
|
|
+ user = "bus"
|
|
|
+ password = ""
|
|
|
+ db = "busdb"
|
|
|
+
|
|
|
+ self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
|
|
|
+ self.cur = self.con.cursor()
|
|
|
+
|
|
|
+ def ok(self):
|
|
|
+ self.cur.execute("select rulename from rule_class where id = %s", 1)
|
|
|
+ res = self.cur.fetchall()
|
|
|
+ return res
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#from app import routes
|
|
|
+
|
|
|
+class Card(Resource):
|
|
|
+ def get(self, cardid = None):
|
|
|
+
|
|
|
+ db = Database()
|
|
|
+ db.cur.execute("select logical_card_id, " \
|
|
|
+ "mag_token, rfid_token, comment, " \
|
|
|
+ "userid, issuetype, "\
|
|
|
+ "issued, lastused, firstused, " \
|
|
|
+ "active, deactivated, group_id " \
|
|
|
+ "from user_card " \
|
|
|
+ "where logical_card_id = %s", cardid)
|
|
|
+ res = db.cur.fetchall()
|
|
|
+ print(res)
|
|
|
+ if len(res)>0:
|
|
|
+ s = json.dumps(res[0], default=json_util.default)
|
|
|
+ return json.loads(s)
|
|
|
+ return {"msg":"no card found", "param":cardid}
|
|
|
+
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete card not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update card not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert card not implemented"}
|
|
|
+
|
|
|
+class Pass(Resource):
|
|
|
+ def get(self, passid = None):
|
|
|
+
|
|
|
+ db = Database()
|
|
|
+ db.cur.execute("select " \
|
|
|
+ "user_pass_id, logical_card_id, " \
|
|
|
+ "issued, activated, deactivated, " \
|
|
|
+ "firstused, lastused, " \
|
|
|
+ "nrides_orig, nrides_remain, " \
|
|
|
+ "nday_orig, nday_expiration, " \
|
|
|
+ " active, expired, " \
|
|
|
+ "rule, queue_order, " \
|
|
|
+ "comment, paytype " \
|
|
|
+ "from user_pass where user_pass_id = %s",
|
|
|
+ passid)
|
|
|
+ res = db.cur.fetchall()
|
|
|
+ print(res)
|
|
|
+ if len(res)>0:
|
|
|
+ s = json.dumps(res[0], default=json_util.default)
|
|
|
+ return json.loads(s)
|
|
|
+ return {"msg":"pass not found", "param":passid}
|
|
|
+
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete pass not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update pass not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert pass not implemented"}
|
|
|
+
|
|
|
+class User(Resource):
|
|
|
+ def get(self, userid = None):
|
|
|
+
|
|
|
+ db = Database()
|
|
|
+ db.cur.execute("select " \
|
|
|
+ "username, userid, comment, " \
|
|
|
+ "first_name, last_name, phone, email, " \
|
|
|
+ "address, city, state, zip, created, active, " \
|
|
|
+ "passwordhash, " \
|
|
|
+ "shipping_address, shipping_city, shipping_state, shipping_zip, " \
|
|
|
+ "shipping_country_code, shipping_country_name, " \
|
|
|
+ "reset_attempts " \
|
|
|
+ "from users where userid = %s",
|
|
|
+ userid)
|
|
|
+ res = db.cur.fetchall()
|
|
|
+ print(res)
|
|
|
+ if len(res)>0:
|
|
|
+ s = json.dumps(res[0], default=json_util.default)
|
|
|
+ return json.loads(s)
|
|
|
+ return {"msg":"user not found", "param":passid}
|
|
|
+
|
|
|
+
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete user not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update user not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert user not implemented"}
|
|
|
+
|
|
|
+class Group(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get group not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete group not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update group not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert group not implemented"}
|
|
|
+
|
|
|
+class PassOption(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get passoption not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete passoption not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update passoption not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert passoption not implemented"}
|
|
|
+
|
|
|
+class Rule(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get rule not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete rule not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update rule not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert rule not implemented"}
|
|
|
+
|
|
|
+
|
|
|
+## Admin related
|
|
|
+##
|
|
|
+
|
|
|
+class AdminUser(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get adminuser not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete adminuser not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update adminuser not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert adminuser not implemented"}
|
|
|
+
|
|
|
+class AdminGroup(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get admingroup not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete admingroup not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update admingroup not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert admingroup not implemented"}
|
|
|
+
|
|
|
+class Driver(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get driver not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete driver not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update driver not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert driver not implemented"}
|
|
|
+
|
|
|
+class Paddle(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get paddle not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete paddle not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update paddle not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert paddle not implemented"}
|
|
|
+
|
|
|
+class Bus(Resource):
|
|
|
+ def get(self):
|
|
|
+ return {"msg":"get bus not implemented"}
|
|
|
+ def delete(self):
|
|
|
+ return {"msg":"delete bus not implemented"}
|
|
|
+ def put(self):
|
|
|
+ return {"msg":"put/update bus not implemented"}
|
|
|
+ def post(self):
|
|
|
+ return {"msg":"post/insert bus not implemented"}
|
|
|
+
|
|
|
|
|
|
from app import routes
|
|
|
+
|
|
|
+api.add_resource(Card, "/api/v1/card/", "/api/v1/card/<int:cardid>")
|
|
|
+api.add_resource(Pass, "/api/v1/pass/", "/api/v1/pass/<int:passid>")
|
|
|
+api.add_resource(User, "/api/v1/user/", "/api/v1/user/<int:userid>")
|
|
|
+api.add_resource(Group, "/api/v1/group/", "/api/v1/group/<int:groupid>")
|
|
|
+api.add_resource(PassOption, "/api/v1/passoption/", "/api/v1/passoption/<int:passoptionid>")
|
|
|
+api.add_resource(Rule, "/api/v1/rule/", "/api/v1/rule/<int:ruleid>")
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ app.run(debug=True)
|