routes.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. return jsonify(res)
  55. @app.route("/api/v1/Card")
  56. def api_card():
  57. field_names = [ "logical_card_id", "mag_token", "rfid_token", "userid" ]
  58. args = request.args
  59. res = {}
  60. print("api.card args:", args)
  61. if 'action' in args:
  62. if args['action'] == 'add':
  63. api_req = { "action":"add", "function":"Card" }
  64. for f in field_names:
  65. if f in args:
  66. api_req[f] = args[f]
  67. print("avpi/v1/Card send:", api_req)
  68. res = api.Request(api_req)
  69. print("got:...", res)
  70. elif args['action'] == 'update':
  71. cardid = -1
  72. if ("logical_card_id" in args) and (args["logical_card_id"] != ''):
  73. cardid = args["logical_card_id"]
  74. else:
  75. subreq = {"action":"search", "function":"Card"}
  76. if ("mag_token" in args) and ("mag_token" != ""):
  77. subreq["mag_token"] = args["mag_token"]
  78. elif ("rfid_token" in args) and ("rfid_token" != ""):
  79. subreq["rfid_token"] = args["rfid_token"]
  80. x = api.Request(subreq)
  81. if ("logical_card_ids" in x) and (len(x["logical_card_ids"]) > 0):
  82. cardid = x["logical_card_ids"][0]
  83. api_req = { "action":"update", "function":"Card" }
  84. if "userid" in args:
  85. api_req["userid"] = args["userid"]
  86. api_req["logical_card_id"] = cardid
  87. res = api.Request(api_req)
  88. return jsonify(res)
  89. @app.route("/api/v1/Pass")
  90. def api_pass():
  91. field_names = [ "logical_card_id",
  92. "nrides_orig", "nrides_remain",
  93. "nday_orig", "nday_expiration",
  94. "rule", "issued", "firstused", "lastused"]
  95. args = request.args
  96. res = {}
  97. print("api.pass args:", args)
  98. if 'action' in args:
  99. if args['action'] == 'add':
  100. api_req = { "action":"add", "function":"Pass" }
  101. for f in field_names:
  102. if f in args:
  103. api_req[f] = args[f]
  104. print("avpi/v1/Pass send:", api_req)
  105. res = api.Request(api_req)
  106. print("got:...", res)
  107. elif args['action'] == 'update':
  108. api_req = { "action":"update", "function":"Pass" }
  109. if "user_pass_id" in args:
  110. api_req["user_pass_id"] = args["user_pass_id"]
  111. print("avpi/v1/Pass.update send:", api_req)
  112. res = api.Request(api_req)
  113. elif args["action"] == "deactivate":
  114. api_req = { "action":"deactivate", "function":"Pass" }
  115. if "user_pass_id" in args:
  116. api_req["user_pass_id"] = args["user_pass_id"]
  117. print("avpi/v1/Pass.deactivate send:", api_req)
  118. res = api.Request(api_req)
  119. elif args["action"] == "search":
  120. api_req = {"action":"search", "function":"Pass" }
  121. return jsonify(res)
  122. @app.route("/api/v1/CardInfo")
  123. def api_card_info():
  124. res = {}
  125. args = request.args
  126. print("....", args)
  127. if 'action' in args:
  128. if args['action'] == 'search':
  129. api_req = { "action":"search", "function":"CardInfo" }
  130. search_fields = ["logical_card_id", "mag_token", "rfid_token"]
  131. for sf in search_fields:
  132. if sf in args:
  133. api_req[sf] = args[sf]
  134. res = api.Request(api_req)
  135. print("got:...", res)
  136. else:
  137. api_req = { "function":"CardInfo" }
  138. search_fields = ["logical_card_id"]
  139. for sf in search_fields:
  140. if sf in args:
  141. api_req[sf] = args[sf]
  142. res = api.Request(api_req)
  143. print("got:...", res)
  144. return jsonify(res)
  145. @app.route("/api/v1/UserInfo")
  146. def api_user_info():
  147. res = {}
  148. args = request.args
  149. userid = -1
  150. if "username" in args:
  151. res = api.Request({"action":"search", "function":"User", "username":args["username"]})
  152. if ("userids" in res) and (len(res["userids"]) > 0):
  153. userid = res["userids"][0]
  154. else:
  155. return jsonify({"error":"error", "message":"user not found"})
  156. elif "userid" in args:
  157. userid = args["userid"]
  158. else:
  159. return jsonify({"error":"error", "message":"no userid"})
  160. api_req = { "function":"UserInfo", "userid":userid }
  161. res = api.Request(api_req)
  162. return jsonify(res)
  163. @app.route("/api/v1/Group")
  164. def api_group_info():
  165. return jsonify(_cache_get_group())
  166. @app.route("/")
  167. @app.route("/index")
  168. def index():
  169. usr = { "username":"clementine", "content" : "index" }
  170. return render_template( usr["content"] + '.html', title='home', user=usr)
  171. @app.route("/manage_card")
  172. def manage_card():
  173. usr = { "username":"clementine" }
  174. x = _cache_get_group()
  175. print(x)
  176. x = _cache()
  177. print(x)
  178. return render_template( 'manage_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  179. @app.route("/manage_user")
  180. def manage_user():
  181. usr = { "username":"clementine" }
  182. return render_template( 'manage_user.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  183. @app.route("/create_user")
  184. def create_user():
  185. usr = { "username":"clementine" }
  186. return render_template( 'create_user.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  187. @app.route("/reissue_card")
  188. def reissue_card():
  189. usr = { "username":"clementine" }
  190. return render_template( 'reissue_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  191. @app.route("/recycle_card")
  192. def recycle_card():
  193. usr = { "username":"clementine" }
  194. return render_template( 'recycle_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  195. @app.route("/add_card_block")
  196. def add_card_block():
  197. usr = { "username":"clementine" }
  198. return render_template( 'add_card_block.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  199. @app.route("/process_pending_card")
  200. def process_pending_card():
  201. usr = { "username":"clementine" }
  202. return render_template( 'process_pending_card.html', title='home', user=usr, groups=_cache_get_group(), cache=_cache())
  203. @app.route("/search")
  204. def search():
  205. usr = { "username":"clementine" }
  206. return render_template( 'search.html', title='home', user=usr)
  207. @app.route("/help")
  208. def help():
  209. usr = { "username":"clementine" }
  210. return render_template( 'help.html', title='home', user=usr, groups=_cache_get_group())
  211. @app.route("/api/v1/hello")
  212. def api_hello():
  213. #data = { "version":"v1", "type":"hello", "data":"hello" }
  214. sessid = request.cookies.get('sessionid')
  215. uname = request.cookies.get('username')
  216. sess_data = { "sessionid":sessid, "username":uname }
  217. data = popfareapi.hello(sess_data)
  218. return jsonify(data)
  219. ###
  220. #class Database:
  221. # def __init__(self):
  222. # host = "127.0.0.1"
  223. # user = "bus"
  224. # password = ""
  225. # db = "busdb"
  226. #
  227. # self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
  228. # self.cur = self.con.cursor()
  229. #
  230. # def ok(self):
  231. # self.cur.execute("select rulename from rule_class where id = %s", 1)
  232. # res = self.cur.fetchall()
  233. # return res
  234. #
  235. #@app.route("/api/v1/card")
  236. #def api_card():
  237. #
  238. # print(request.args)
  239. # param = request.args
  240. #
  241. # cardid = param.get("id")
  242. # mag_token = param.get("mag_token")
  243. # rfid_token = param.get("rfid_token")
  244. #
  245. # db = Database()
  246. # ok = db.ok()
  247. #
  248. # print(ok)
  249. #
  250. # if cardid:
  251. # print("cardid:", cardid)
  252. # elif mag_token:
  253. # print("mag:", mag_token)
  254. # elif rfid_token:
  255. # print("rfid:", rfid_token)
  256. #
  257. #
  258. # return jsonify({ "message": "card not implemented"})
  259. #
  260. #@app.route("/api/v1/user")
  261. #def api_user():
  262. # return jsonify({ "message": "user not implemented"})
  263. #
  264. #@app.route("/api/v1/pass")
  265. #def api_pass():
  266. # return jsonify({ "message": "pass not implemented"})
  267. #
  268. #@app.route("/api/v1/group")
  269. #def api_group():
  270. # return jsonify({ "message": "group not implemented"})