| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- #!/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)
|