routes.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/usr/bin/python3
  2. import sys
  3. import os
  4. from flask import render_template, url_for
  5. from app import app
  6. from flask import request, jsonify, send_from_directory
  7. from flask_restful import Resource, Api
  8. import PopufareAPI as api
  9. @app.route('/favicon.ico')
  10. def favicon():
  11. return send_from_directory(os.path.join(app.root_path, 'static'),
  12. 'favicon.ico',
  13. mimetype='favicon.ico')
  14. @app.route("/api/v1/User")
  15. def api_user():
  16. field_names = [ "username", "first_name", "last_name",
  17. "email", "phone", "address", "city", "state", "zip",
  18. "shipping_name", "shipping_address", "shipping_city",
  19. "shipping_state", "shipping_country_code", "shipping_country_name",
  20. "shipping_zip", "password"];
  21. args = request.args
  22. res = {}
  23. print("....", args)
  24. if 'action' in args:
  25. if args['action'] == 'add':
  26. api_req = { "action":"add", "function":"User" }
  27. for f in field_names:
  28. if f in args:
  29. api_req[f] = args[f]
  30. print("avpi/v1/User send:", api_req)
  31. res = api.Request(api_req)
  32. print("got:...", res)
  33. elif args['action'] == 'get':
  34. print(">>>cp")
  35. api_req = { "action":"get", "function":"User" }
  36. if "userid" in args:
  37. api_req["userid"] = args["userid"]
  38. print("avpi/v1/User.get send:", api_req)
  39. res = api.Request(api_req)
  40. return jsonify(res)
  41. @app.route("/api/v1/Card")
  42. def api_card():
  43. field_names = [ "logical_card_id", "mag_token", "rfid_token", "userid" ]
  44. args = request.args
  45. res = {}
  46. print("api.card args:", args)
  47. if 'action' in args:
  48. if args['action'] == 'add':
  49. api_req = { "action":"add", "function":"Card" }
  50. for f in field_names:
  51. if f in args:
  52. api_req[f] = args[f]
  53. print("avpi/v1/Card send:", api_req)
  54. res = api.Request(api_req)
  55. print("got:...", res)
  56. elif args['action'] == 'update':
  57. api_req = { "action":"update", "function":"Card" }
  58. if "userid" in args:
  59. api_req["userid"] = args["userid"]
  60. if "logical_card_id" in args:
  61. api_req["logical_card_id"] = args["logical_card_id"]
  62. print("avpi/v1/Card.update send:", api_req)
  63. res = api.Request(api_req)
  64. return jsonify(res)
  65. @app.route("/api/v1/Pass")
  66. def api_pass():
  67. field_names = [ "logical_card_id",
  68. "nrides_orig", "nrides_remain",
  69. "nday_orig", "nday_expiration",
  70. "rule", "issued", "firstused", "lastused"]
  71. args = request.args
  72. res = {}
  73. print("api.pass args:", args)
  74. if 'action' in args:
  75. if args['action'] == 'add':
  76. api_req = { "action":"add", "function":"Pass" }
  77. for f in field_names:
  78. if f in args:
  79. api_req[f] = args[f]
  80. print("avpi/v1/Pass send:", api_req)
  81. res = api.Request(api_req)
  82. print("got:...", res)
  83. elif args['action'] == 'update':
  84. api_req = { "action":"update", "function":"Pass" }
  85. if "user_pass_id" in args:
  86. api_req["user_pass_id"] = args["user_pass_id"]
  87. print("avpi/v1/Pass.update send:", api_req)
  88. res = api.Request(api_req)
  89. return jsonify(res)
  90. @app.route("/api/v1/CardInfo")
  91. def api_card_info():
  92. res = {}
  93. args = request.args
  94. print("....", args)
  95. if 'action' in args:
  96. if args['action'] == 'search':
  97. api_req = { "action":"search", "function":"CardInfo" }
  98. search_fields = ["logical_card_id", "mag_token", "rfid_token"]
  99. for sf in search_fields:
  100. if sf in args:
  101. api_req[sf] = args[sf]
  102. res = api.Request(api_req)
  103. print("got:...", res)
  104. else:
  105. api_req = { "function":"CardInfo" }
  106. search_fields = ["logical_card_id"]
  107. for sf in search_fields:
  108. if sf in args:
  109. api_req[sf] = args[sf]
  110. res = api.Request(api_req)
  111. print("got:...", res)
  112. return jsonify(res)
  113. @app.route("/api/v1/UserInfo")
  114. def api_user_info():
  115. res = {}
  116. args = request.args
  117. print("....", args)
  118. userid=-1
  119. if "userid" in args:
  120. userid = args["userid"]
  121. api_req = { "function":"UserInfo", "userid":userid }
  122. print("sending...", api_req)
  123. res = api.Request(api_req)
  124. print("got:...", res)
  125. return jsonify(res)
  126. @app.route("/")
  127. @app.route("/index")
  128. def index():
  129. usr = { "username":"clementine", "content" : "index" }
  130. return render_template( usr["content"] + '.html', title='home', user=usr)
  131. @app.route("/manage_card")
  132. def manage_card():
  133. usr = { "username":"clementine" }
  134. return render_template( 'manage_card.html', title='home', user=usr)
  135. @app.route("/manage_user")
  136. def manage_user():
  137. usr = { "username":"clementine" }
  138. return render_template( 'manage_user.html', title='home', user=usr)
  139. @app.route("/create_user")
  140. def create_user():
  141. usr = { "username":"clementine" }
  142. return render_template( 'create_user.html', title='home', user=usr)
  143. @app.route("/reissue_card")
  144. def reissue_card():
  145. usr = { "username":"clementine" }
  146. return render_template( 'reissue_card.html', title='home', user=usr)
  147. @app.route("/recycle_card")
  148. def recycle_card():
  149. usr = { "username":"clementine" }
  150. return render_template( 'recycle_card.html', title='home', user=usr)
  151. @app.route("/add_card_block")
  152. def add_card_block():
  153. usr = { "username":"clementine" }
  154. return render_template( 'add_card_block.html', title='home', user=usr)
  155. @app.route("/process_pending_card")
  156. def process_pending_card():
  157. usr = { "username":"clementine" }
  158. return render_template( 'process_pending_card.html', title='home', user=usr)
  159. @app.route("/search")
  160. def search():
  161. usr = { "username":"clementine" }
  162. return render_template( 'search.html', title='home', user=usr)
  163. @app.route("/help")
  164. def help():
  165. usr = { "username":"clementine" }
  166. return render_template( 'help.html', title='home', user=usr)
  167. @app.route("/api/v1/hello")
  168. def api_hello():
  169. #data = { "version":"v1", "type":"hello", "data":"hello" }
  170. sessid = request.cookies.get('sessionid')
  171. uname = request.cookies.get('username')
  172. sess_data = { "sessionid":sessid, "username":uname }
  173. data = popfareapi.hello(sess_data)
  174. return jsonify(data)
  175. ###
  176. #class Database:
  177. # def __init__(self):
  178. # host = "127.0.0.1"
  179. # user = "bus"
  180. # password = ""
  181. # db = "busdb"
  182. #
  183. # self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
  184. # self.cur = self.con.cursor()
  185. #
  186. # def ok(self):
  187. # self.cur.execute("select rulename from rule_class where id = %s", 1)
  188. # res = self.cur.fetchall()
  189. # return res
  190. #
  191. #@app.route("/api/v1/card")
  192. #def api_card():
  193. #
  194. # print(request.args)
  195. # param = request.args
  196. #
  197. # cardid = param.get("id")
  198. # mag_token = param.get("mag_token")
  199. # rfid_token = param.get("rfid_token")
  200. #
  201. # db = Database()
  202. # ok = db.ok()
  203. #
  204. # print(ok)
  205. #
  206. # if cardid:
  207. # print("cardid:", cardid)
  208. # elif mag_token:
  209. # print("mag:", mag_token)
  210. # elif rfid_token:
  211. # print("rfid:", rfid_token)
  212. #
  213. #
  214. # return jsonify({ "message": "card not implemented"})
  215. #
  216. #@app.route("/api/v1/user")
  217. #def api_user():
  218. # return jsonify({ "message": "user not implemented"})
  219. #
  220. #@app.route("/api/v1/pass")
  221. #def api_pass():
  222. # return jsonify({ "message": "pass not implemented"})
  223. #
  224. #@app.route("/api/v1/group")
  225. #def api_group():
  226. # return jsonify({ "message": "group not implemented"})