routes.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. cache = {}
  10. def _cache_get_group():
  11. if "group" not in cache:
  12. res = api.Request({"action":"get","function":"Group"})
  13. cache["group"] = res["group"]
  14. return cache["group"]
  15. def _cache_get_other_pass():
  16. if "ruleclass" not in cache:
  17. res = api.Request({"action":"search","function":"Ruleclass"})
  18. cache["ruleclass"] = res["ruleclass"]
  19. cache["other_pass"] = []
  20. for r in cache["ruleclass"]:
  21. if r["ruleclass"] != "OTHER": continue
  22. cache["other_pass"].append(r)
  23. return cache["other_pass"]
  24. def _cache():
  25. _cache_get_group()
  26. _cache_get_other_pass()
  27. return cache
  28. @app.route('/favicon.ico')
  29. def favicon():
  30. return send_from_directory(os.path.join(app.root_path, 'static'),
  31. 'favicon.ico',
  32. mimetype='favicon.ico')
  33. @app.route("/api/v1/User")
  34. def api_user():
  35. field_names = [ "username", "first_name", "last_name",
  36. "email", "phone", "address", "city", "state", "zip",
  37. "shipping_name", "shipping_address", "shipping_city",
  38. "shipping_state", "shipping_country_code", "shipping_country_name",
  39. "shipping_zip", "password"];
  40. args = request.args
  41. res = {}
  42. if 'action' in args:
  43. if args['action'] == 'add':
  44. api_req = { "action":"add", "function":"User" }
  45. for f in field_names:
  46. if f in args:
  47. api_req[f] = args[f]
  48. res = api.Request(api_req)
  49. elif args['action'] == 'get':
  50. api_req = { "action":"get", "function":"User" }
  51. if "userid" in args:
  52. api_req["userid"] = args["userid"]
  53. res = api.Request(api_req)
  54. elif args['action'] == 'update':
  55. api_req = { "action":"update", "function":"User" }
  56. for f in field_names:
  57. if f in args:
  58. api_req[f] = args[f]
  59. if "userid" in args:
  60. api_req["userid"] = args["userid"]
  61. res = api.Request(api_req)
  62. return jsonify(res)
  63. @app.route("/api/v1/Card")
  64. def api_card():
  65. field_names = [ "logical_card_id", "mag_token", "rfid_token", "userid" ]
  66. args = request.args
  67. res = {}
  68. print("api.card args:", args)
  69. if 'action' in args:
  70. if args['action'] == 'add':
  71. api_req = { "action":"add", "function":"Card" }
  72. for f in field_names:
  73. if f in args:
  74. api_req[f] = args[f]
  75. print("avpi/v1/Card send:", api_req)
  76. res = api.Request(api_req)
  77. print("got:...", res)
  78. elif args['action'] == 'update':
  79. cardid = -1
  80. if ("logical_card_id" in args) and (args["logical_card_id"] != ''):
  81. cardid = args["logical_card_id"]
  82. else:
  83. subreq = {"action":"search", "function":"Card"}
  84. if ("mag_token" in args) and ("mag_token" != ""):
  85. subreq["mag_token"] = args["mag_token"]
  86. elif ("rfid_token" in args) and ("rfid_token" != ""):
  87. subreq["rfid_token"] = args["rfid_token"]
  88. x = api.Request(subreq)
  89. if ("logical_card_ids" in x) and (len(x["logical_card_ids"]) > 0):
  90. cardid = x["logical_card_ids"][0]
  91. api_req = { "action":"update", "function":"Card" }
  92. if "userid" in args:
  93. api_req["userid"] = args["userid"]
  94. api_req["logical_card_id"] = cardid
  95. res = api.Request(api_req)
  96. return jsonify(res)
  97. @app.route("/api/v1/Pass")
  98. def api_pass():
  99. field_names = [ "logical_card_id",
  100. "nrides_orig", "nrides_remain",
  101. "nday_orig", "nday_expiration",
  102. "rule", "issued", "firstused", "lastused"]
  103. args = request.args
  104. res = {}
  105. print("api.pass args:", args)
  106. if 'action' in args:
  107. if args['action'] == 'add':
  108. api_req = { "action":"add", "function":"Pass" }
  109. for f in field_names:
  110. if f in args:
  111. api_req[f] = args[f]
  112. print("avpi/v1/Pass send:", api_req)
  113. res = api.Request(api_req)
  114. print("got:...", res)
  115. elif args['action'] == 'update':
  116. api_req = { "action":"update", "function":"Pass" }
  117. if "user_pass_id" in args:
  118. api_req["user_pass_id"] = args["user_pass_id"]
  119. print("avpi/v1/Pass.update send:", api_req)
  120. res = api.Request(api_req)
  121. elif args["action"] == "deactivate":
  122. api_req = { "action":"deactivate", "function":"Pass" }
  123. if "user_pass_id" in args:
  124. api_req["user_pass_id"] = args["user_pass_id"]
  125. print("avpi/v1/Pass.deactivate send:", api_req)
  126. res = api.Request(api_req)
  127. elif args["action"] == "search":
  128. api_req = {"action":"search", "function":"Pass" }
  129. return jsonify(res)
  130. @app.route("/api/v1/CardInfo")
  131. def api_card_info():
  132. res = {}
  133. args = request.args
  134. if 'action' in args:
  135. if args['action'] == 'search':
  136. api_req = { "action":"search", "function":"CardInfo" }
  137. search_fields = ["logical_card_id", "mag_token", "rfid_token"]
  138. for sf in search_fields:
  139. if sf in args:
  140. api_req[sf] = args[sf]
  141. res = api.Request(api_req)
  142. print("got:...", res)
  143. else:
  144. api_req = { "function":"CardInfo" }
  145. search_fields = ["logical_card_id"]
  146. for sf in search_fields:
  147. if sf in args:
  148. api_req[sf] = args[sf]
  149. res = api.Request(api_req)
  150. print("got:...", res)
  151. return jsonify(res)
  152. @app.route("/api/v1/UserInfo")
  153. def api_user_info():
  154. res = {}
  155. args = request.args
  156. userid = -1
  157. if "username" in args:
  158. res = api.Request({"action":"search", "function":"User", "username":args["username"]})
  159. if ("userids" in res) and (len(res["userids"]) > 0):
  160. userid = res["userids"][0]
  161. else:
  162. return jsonify({"error":"error", "message":"user not found"})
  163. elif "userid" in args:
  164. userid = args["userid"]
  165. else:
  166. return jsonify({"error":"error", "message":"no userid"})
  167. api_req = { "function":"UserInfo", "userid":userid }
  168. res = api.Request(api_req)
  169. return jsonify(res)
  170. ###
  171. ###
  172. @app.route("/api/v1/Group")
  173. def api_group_info():
  174. return jsonify(_cache_get_group())
  175. ###
  176. ###
  177. @app.route("/api/v1/Search")
  178. def api_search():
  179. res = {}
  180. args = request.args
  181. field_names = ["search_type", "search_string"]
  182. api_req = { "function":"Search" }
  183. for f in field_names:
  184. if f in args:
  185. api_req[f] = args[f]
  186. res = api.Request(api_req)
  187. return jsonify(res)
  188. ###
  189. ###
  190. @app.route("/api/v1/AddCardBlock")
  191. def api_add_card_block():
  192. res = {}
  193. args = request.args
  194. field_names = ["mag_token", "rfid_token", "group_id", "count",
  195. "pass_class",
  196. "pass_nrides_remain", "pass_nrides_orig",
  197. "pass_nday_orig", "pass_rule" ]
  198. api_req = { "function":"AddCardBlock" }
  199. for f in field_names:
  200. if f in args:
  201. api_req[f] = args[f]
  202. res = api.Request(api_req)
  203. return jsonify(res)
  204. ###
  205. ###
  206. @app.route("/api/v1/Pending")
  207. def api_pending():
  208. res = {}
  209. args = request.args
  210. field_names = ["org_card_order_queue_id", "userid", "logical_card_id",
  211. "created", "processed", "comment" ]
  212. vals = {}
  213. for f in field_names:
  214. if f in args:
  215. vals[f] = args[f]
  216. api_req = { "function":"Pending" }
  217. if "action" in args:
  218. if args["action"] == "getall":
  219. api_req["action"] = "getall"
  220. res = api.Request(api_req)
  221. elif args["action"] == "process":
  222. api_req["action"] = "process"
  223. if "org_card_order_queue_id" in vals:
  224. api_req["org_card_order_queue_id"] = vals["org_card_order_queue_id"]
  225. res = api.Request(api_req)
  226. else:
  227. res["result"] = "fail"
  228. res["api_comment"] = "org_card_order_queue_id must be specified"
  229. elif args["action"] == "get":
  230. api_req["action"] = "get"
  231. if "org_card_order_queue_id" in vals:
  232. api_req["org_card_order_queue_id"] = vals["org_card_order_queue_id"]
  233. res = api.Request(api_req)
  234. else:
  235. res["result"] = "fail"
  236. res["api_comment"] = "org_card_order_queue_id must be specified"
  237. return jsonify(res)
  238. ###
  239. ###
  240. @app.route("/api/v1/RecycleCard")
  241. def api_recycle_card():
  242. res = {}
  243. args = request.args
  244. field_names = ["logical_card_id", "mag_token", "rfid_token", "group",
  245. "pass_rule", "pass_class", "pass_val" ]
  246. vals = {}
  247. for f in field_names:
  248. if f in args:
  249. vals[f] = args[f]
  250. api_req = { "function":"RecycleCard" }
  251. if "logical_card_id" in vals: api_req["logical_card_id"] = vals["logical_card_id"]
  252. if "mag_token" in vals: api_req["mag_token"] = vals["mag_token"]
  253. if "rfid_token" in vals: api_req["rfid_token"] = vals["rfid_token"]
  254. print("approute recycle:", vals)
  255. if "pass_rule" in vals:
  256. if vals["pass_class"] == "NRIDE":
  257. api_req["pass_class"] = "NRIDE"
  258. api_req["nrides_orig"] = vals["pass_val"]
  259. api_req["nrides_remain"] = vals["pass_val"]
  260. api_req["rule"] = vals["pass_rule"]
  261. elif vals["pass_class"] == "NDAY":
  262. api_req["pass_class"] = "NDAY"
  263. api_req["nday_orig"] = vals["pass_val"]
  264. api_req["rule"] = vals["pass_rule"]
  265. elif vals["pass_class"] == "OTHER":
  266. api_req["pass_class"] = "OTHER"
  267. api_req["rule"] = vals["pass_rule"]
  268. print("approute recycle:", api_req)
  269. res = api.Request(api_req)
  270. return jsonify(res)
  271. ###
  272. ###
  273. @app.route("/")
  274. @app.route("/index")
  275. def index():
  276. usr = { "username":"clementine", "content" : "index" }
  277. return render_template( usr["content"] + '.html', title='home', user=usr)
  278. @app.route("/manage_card")
  279. def manage_card():
  280. usr = { "username":"clementine" }
  281. x = _cache_get_group()
  282. print(x)
  283. x = _cache()
  284. print(x)
  285. return render_template( 'manage_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  286. @app.route("/manage_user")
  287. def manage_user():
  288. usr = { "username":"clementine" }
  289. return render_template( 'manage_user.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  290. @app.route("/create_user")
  291. def create_user():
  292. usr = { "username":"clementine" }
  293. return render_template( 'create_user.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  294. @app.route("/reissue_card")
  295. def reissue_card():
  296. usr = { "username":"clementine" }
  297. return render_template( 'reissue_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  298. @app.route("/recycle_card")
  299. def recycle_card():
  300. usr = { "username":"clementine" }
  301. return render_template( 'recycle_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  302. @app.route("/add_card_block")
  303. def add_card_block():
  304. usr = { "username":"clementine" }
  305. return render_template( 'add_card_block.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  306. @app.route("/process_pending_card")
  307. def process_pending_card():
  308. usr = { "username":"clementine" }
  309. return render_template( 'process_pending_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  310. @app.route("/search")
  311. def search():
  312. usr = { "username":"clementine" }
  313. return render_template( 'search.html', title='home', user=usr)
  314. @app.route("/help")
  315. def help():
  316. usr = { "username":"clementine" }
  317. return render_template( 'help.html', title='home', user=usr, groups=_cache_get_group())
  318. @app.route("/api/v1/hello")
  319. def api_hello():
  320. #data = { "version":"v1", "type":"hello", "data":"hello" }
  321. sessid = request.cookies.get('sessionid')
  322. uname = request.cookies.get('username')
  323. sess_data = { "sessionid":sessid, "username":uname }
  324. data = popfareapi.hello(sess_data)
  325. return jsonify(data)
  326. ###
  327. #class Database:
  328. # def __init__(self):
  329. # host = "127.0.0.1"
  330. # user = "bus"
  331. # password = ""
  332. # db = "busdb"
  333. #
  334. # self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
  335. # self.cur = self.con.cursor()
  336. #
  337. # def ok(self):
  338. # self.cur.execute("select rulename from rule_class where id = %s", 1)
  339. # res = self.cur.fetchall()
  340. # return res
  341. #
  342. #@app.route("/api/v1/card")
  343. #def api_card():
  344. #
  345. # print(request.args)
  346. # param = request.args
  347. #
  348. # cardid = param.get("id")
  349. # mag_token = param.get("mag_token")
  350. # rfid_token = param.get("rfid_token")
  351. #
  352. # db = Database()
  353. # ok = db.ok()
  354. #
  355. # print(ok)
  356. #
  357. # if cardid:
  358. # print("cardid:", cardid)
  359. # elif mag_token:
  360. # print("mag:", mag_token)
  361. # elif rfid_token:
  362. # print("rfid:", rfid_token)
  363. #
  364. #
  365. # return jsonify({ "message": "card not implemented"})
  366. #
  367. #@app.route("/api/v1/user")
  368. #def api_user():
  369. # return jsonify({ "message": "user not implemented"})
  370. #
  371. #@app.route("/api/v1/pass")
  372. #def api_pass():
  373. # return jsonify({ "message": "pass not implemented"})
  374. #
  375. #@app.route("/api/v1/group")
  376. #def api_group():
  377. # return jsonify({ "message": "group not implemented"})