routes.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_address",
  19. "shipping_zip", "password"];
  20. args = request.args
  21. res = {}
  22. print("....", args)
  23. if 'action' in args:
  24. if args['action'] == 'add':
  25. api_req = { "action":"add", "function":"User" }
  26. for f in field_names:
  27. if f in args:
  28. api_req[f] = args[f]
  29. print("avpi/v1/User send:", api_req)
  30. res = api.Request(api_req)
  31. print("got:...", res)
  32. elif args['action'] == 'get':
  33. print(">>>cp")
  34. api_req = { "action":"get", "function":"User" }
  35. if "userid" in args:
  36. api_req["userid"] = args["userid"]
  37. print("avpi/v1/User.get send:", api_req)
  38. res = api.Request(api_req)
  39. return jsonify(res)
  40. @app.route("/api/v1/Card")
  41. def api_card():
  42. field_names = [ "logical_card_id", "mag_token", "rfid_token", "userid" ]
  43. args = request.args
  44. res = {}
  45. print("api.card args:", args)
  46. if 'action' in args:
  47. if args['action'] == 'add':
  48. api_req = { "action":"add", "function":"Card" }
  49. for f in field_names:
  50. if f in args:
  51. api_req[f] = args[f]
  52. print("avpi/v1/Card send:", api_req)
  53. res = api.Request(api_req)
  54. print("got:...", res)
  55. elif args['action'] == 'update':
  56. api_req = { "action":"update", "function":"Card" }
  57. if "userid" in args:
  58. api_req["userid"] = args["userid"]
  59. if "logical_card_id" in args:
  60. api_req["logical_card_id"] = args["logical_card_id"]
  61. print("avpi/v1/Card.update send:", api_req)
  62. res = api.Request(api_req)
  63. return jsonify(res)
  64. @app.route("/api/v1/CardInfo")
  65. def api_card_info():
  66. res = {}
  67. args = request.args
  68. print("....", args)
  69. if 'action' in args:
  70. if args['action'] == 'search':
  71. api_req = { "action":"search", "function":"CardInfo" }
  72. search_fields = ["logical_card_id", "mag_token", "rfid_token"]
  73. for sf in search_fields:
  74. if sf in args:
  75. api_req[sf] = args[sf]
  76. res = api.Request(api_req)
  77. print("got:...", res)
  78. return jsonify(res)
  79. @app.route("/api/v1/UserInfo")
  80. def api_user_info():
  81. res = {}
  82. args = request.args
  83. print("....", args)
  84. userid=-1
  85. if "userid" in args:
  86. userid = args["userid"]
  87. api_req = { "function":"UserInfo", "userid":userid }
  88. print("sending...", api_req)
  89. res = api.Request(api_req)
  90. print("got:...", res)
  91. return jsonify(res)
  92. @app.route("/")
  93. @app.route("/index")
  94. def index():
  95. usr = { "username":"clementine", "content" : "index" }
  96. return render_template( usr["content"] + '.html', title='home', user=usr)
  97. @app.route("/manage_card")
  98. def manage_card():
  99. usr = { "username":"clementine" }
  100. return render_template( 'manage_card.html', title='home', user=usr)
  101. @app.route("/manage_user")
  102. def manage_user():
  103. usr = { "username":"clementine" }
  104. return render_template( 'manage_user.html', title='home', user=usr)
  105. @app.route("/create_user")
  106. def create_user():
  107. usr = { "username":"clementine" }
  108. return render_template( 'create_user.html', title='home', user=usr)
  109. @app.route("/reissue_card")
  110. def reissue_card():
  111. usr = { "username":"clementine" }
  112. return render_template( 'reissue_card.html', title='home', user=usr)
  113. @app.route("/recycle_card")
  114. def recycle_card():
  115. usr = { "username":"clementine" }
  116. return render_template( 'recycle_card.html', title='home', user=usr)
  117. @app.route("/add_card_block")
  118. def add_card_block():
  119. usr = { "username":"clementine" }
  120. return render_template( 'add_card_block.html', title='home', user=usr)
  121. @app.route("/process_pending_card")
  122. def process_pending_card():
  123. usr = { "username":"clementine" }
  124. return render_template( 'process_pending_card.html', title='home', user=usr)
  125. @app.route("/search")
  126. def search():
  127. usr = { "username":"clementine" }
  128. return render_template( 'search.html', title='home', user=usr)
  129. @app.route("/help")
  130. def help():
  131. usr = { "username":"clementine" }
  132. return render_template( 'help.html', title='home', user=usr)
  133. @app.route("/api/v1/hello")
  134. def api_hello():
  135. #data = { "version":"v1", "type":"hello", "data":"hello" }
  136. sessid = request.cookies.get('sessionid')
  137. uname = request.cookies.get('username')
  138. sess_data = { "sessionid":sessid, "username":uname }
  139. data = popfareapi.hello(sess_data)
  140. return jsonify(data)
  141. ###
  142. #class Database:
  143. # def __init__(self):
  144. # host = "127.0.0.1"
  145. # user = "bus"
  146. # password = ""
  147. # db = "busdb"
  148. #
  149. # self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
  150. # self.cur = self.con.cursor()
  151. #
  152. # def ok(self):
  153. # self.cur.execute("select rulename from rule_class where id = %s", 1)
  154. # res = self.cur.fetchall()
  155. # return res
  156. #
  157. #@app.route("/api/v1/card")
  158. #def api_card():
  159. #
  160. # print(request.args)
  161. # param = request.args
  162. #
  163. # cardid = param.get("id")
  164. # mag_token = param.get("mag_token")
  165. # rfid_token = param.get("rfid_token")
  166. #
  167. # db = Database()
  168. # ok = db.ok()
  169. #
  170. # print(ok)
  171. #
  172. # if cardid:
  173. # print("cardid:", cardid)
  174. # elif mag_token:
  175. # print("mag:", mag_token)
  176. # elif rfid_token:
  177. # print("rfid:", rfid_token)
  178. #
  179. #
  180. # return jsonify({ "message": "card not implemented"})
  181. #
  182. #@app.route("/api/v1/user")
  183. #def api_user():
  184. # return jsonify({ "message": "user not implemented"})
  185. #
  186. #@app.route("/api/v1/pass")
  187. #def api_pass():
  188. # return jsonify({ "message": "pass not implemented"})
  189. #
  190. #@app.route("/api/v1/group")
  191. #def api_group():
  192. # return jsonify({ "message": "group not implemented"})