__init__.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python3
  2. from flask import Flask
  3. from flask_restful import Resource, Api
  4. from bson import json_util
  5. import json
  6. import pymysql
  7. app = Flask(__name__)
  8. api = Api(app)
  9. class Database:
  10. def __init__(self):
  11. host = "127.0.0.1"
  12. user = "bus"
  13. password = ""
  14. db = "busdb"
  15. self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
  16. self.cur = self.con.cursor()
  17. def ok(self):
  18. self.cur.execute("select rulename from rule_class where id = %s", 1)
  19. res = self.cur.fetchall()
  20. return res
  21. #from app import routes
  22. class Card(Resource):
  23. def get(self, cardid = None):
  24. db = Database()
  25. db.cur.execute("select logical_card_id, " \
  26. "mag_token, rfid_token, comment, " \
  27. "userid, issuetype, "\
  28. "issued, lastused, firstused, " \
  29. "active, deactivated, group_id " \
  30. "from user_card " \
  31. "where logical_card_id = %s", cardid)
  32. res = db.cur.fetchall()
  33. print(res)
  34. if len(res)>0:
  35. s = json.dumps(res[0], default=json_util.default)
  36. return json.loads(s)
  37. return {"msg":"no card found", "param":cardid}
  38. def delete(self):
  39. return {"msg":"delete card not implemented"}
  40. def put(self):
  41. return {"msg":"put/update card not implemented"}
  42. def post(self):
  43. return {"msg":"post/insert card not implemented"}
  44. class Pass(Resource):
  45. def get(self, passid = None):
  46. db = Database()
  47. db.cur.execute("select " \
  48. "user_pass_id, logical_card_id, " \
  49. "issued, activated, deactivated, " \
  50. "firstused, lastused, " \
  51. "nrides_orig, nrides_remain, " \
  52. "nday_orig, nday_expiration, " \
  53. " active, expired, " \
  54. "rule, queue_order, " \
  55. "comment, paytype " \
  56. "from user_pass where user_pass_id = %s",
  57. passid)
  58. res = db.cur.fetchall()
  59. print(res)
  60. if len(res)>0:
  61. s = json.dumps(res[0], default=json_util.default)
  62. return json.loads(s)
  63. return {"msg":"pass not found", "param":passid}
  64. def delete(self):
  65. return {"msg":"delete pass not implemented"}
  66. def put(self):
  67. return {"msg":"put/update pass not implemented"}
  68. def post(self):
  69. return {"msg":"post/insert pass not implemented"}
  70. class User(Resource):
  71. def get(self, userid = None):
  72. db = Database()
  73. db.cur.execute("select " \
  74. "username, userid, comment, " \
  75. "first_name, last_name, phone, email, " \
  76. "address, city, state, zip, created, active, " \
  77. "passwordhash, " \
  78. "shipping_address, shipping_city, shipping_state, shipping_zip, " \
  79. "shipping_country_code, shipping_country_name, " \
  80. "reset_attempts " \
  81. "from users where userid = %s",
  82. userid)
  83. res = db.cur.fetchall()
  84. print(res)
  85. if len(res)>0:
  86. s = json.dumps(res[0], default=json_util.default)
  87. return json.loads(s)
  88. return {"msg":"user not found", "param":passid}
  89. def delete(self):
  90. return {"msg":"delete user not implemented"}
  91. def put(self):
  92. return {"msg":"put/update user not implemented"}
  93. def post(self):
  94. return {"msg":"post/insert user not implemented"}
  95. class Group(Resource):
  96. def get(self):
  97. return {"msg":"get group not implemented"}
  98. def delete(self):
  99. return {"msg":"delete group not implemented"}
  100. def put(self):
  101. return {"msg":"put/update group not implemented"}
  102. def post(self):
  103. return {"msg":"post/insert group not implemented"}
  104. class PassOption(Resource):
  105. def get(self):
  106. return {"msg":"get passoption not implemented"}
  107. def delete(self):
  108. return {"msg":"delete passoption not implemented"}
  109. def put(self):
  110. return {"msg":"put/update passoption not implemented"}
  111. def post(self):
  112. return {"msg":"post/insert passoption not implemented"}
  113. class Rule(Resource):
  114. def get(self):
  115. return {"msg":"get rule not implemented"}
  116. def delete(self):
  117. return {"msg":"delete rule not implemented"}
  118. def put(self):
  119. return {"msg":"put/update rule not implemented"}
  120. def post(self):
  121. return {"msg":"post/insert rule not implemented"}
  122. ## Admin related
  123. ##
  124. class AdminUser(Resource):
  125. def get(self):
  126. return {"msg":"get adminuser not implemented"}
  127. def delete(self):
  128. return {"msg":"delete adminuser not implemented"}
  129. def put(self):
  130. return {"msg":"put/update adminuser not implemented"}
  131. def post(self):
  132. return {"msg":"post/insert adminuser not implemented"}
  133. class AdminGroup(Resource):
  134. def get(self):
  135. return {"msg":"get admingroup not implemented"}
  136. def delete(self):
  137. return {"msg":"delete admingroup not implemented"}
  138. def put(self):
  139. return {"msg":"put/update admingroup not implemented"}
  140. def post(self):
  141. return {"msg":"post/insert admingroup not implemented"}
  142. class Driver(Resource):
  143. def get(self):
  144. return {"msg":"get driver not implemented"}
  145. def delete(self):
  146. return {"msg":"delete driver not implemented"}
  147. def put(self):
  148. return {"msg":"put/update driver not implemented"}
  149. def post(self):
  150. return {"msg":"post/insert driver not implemented"}
  151. class Paddle(Resource):
  152. def get(self):
  153. return {"msg":"get paddle not implemented"}
  154. def delete(self):
  155. return {"msg":"delete paddle not implemented"}
  156. def put(self):
  157. return {"msg":"put/update paddle not implemented"}
  158. def post(self):
  159. return {"msg":"post/insert paddle not implemented"}
  160. class Bus(Resource):
  161. def get(self):
  162. return {"msg":"get bus not implemented"}
  163. def delete(self):
  164. return {"msg":"delete bus not implemented"}
  165. def put(self):
  166. return {"msg":"put/update bus not implemented"}
  167. def post(self):
  168. return {"msg":"post/insert bus not implemented"}
  169. from app import routes
  170. api.add_resource(Card, "/api/v1/card/", "/api/v1/card/<int:cardid>")
  171. api.add_resource(Pass, "/api/v1/pass/", "/api/v1/pass/<int:passid>")
  172. api.add_resource(User, "/api/v1/user/", "/api/v1/user/<int:userid>")
  173. api.add_resource(Group, "/api/v1/group/", "/api/v1/group/<int:groupid>")
  174. api.add_resource(PassOption, "/api/v1/passoption/", "/api/v1/passoption/<int:passoptionid>")
  175. api.add_resource(Rule, "/api/v1/rule/", "/api/v1/rule/<int:ruleid>")
  176. if __name__ == '__main__':
  177. app.run(debug=True)