PopufareAPI.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #!/usr/bin/python3
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ## WORK IN PROGRESS
  21. import json
  22. import mysql.connector
  23. import time
  24. import datetime
  25. import copy
  26. import hashlib
  27. #conn = mysql.connector.connect(user='bus', password='bus', host='localhost', database='busdb', port=3306)
  28. _USER = 'busapi'
  29. _PASSWORD = 'bus'
  30. _HOST = 'localhost'
  31. _DATABASE = 'busdb'
  32. _PORT = 5506
  33. Function = [
  34. "User",
  35. "Card",
  36. "Pass",
  37. "Log",
  38. "PricePoints",
  39. "AdminGetCard", "AdminGetCards", "AdminGetPass", "AdminGetUser",
  40. "AdminGetAdmin", "AdminGetPassesOnCard", "AdminGetPendingQueue", "AdminProcessPendingQueue",
  41. "AdminRemovePendingQueue", "AdminCreateCardBlock", "AdminCreateCard", "AdminSetUser",
  42. "AdminSetAdmin", "AdminAddPass", "AdminAddCard", "AdminAddUser",
  43. "AdminAddAdmin", "AdminRemovePass", "AdminRemovePasses", "AdminRemoveCard",
  44. "AdminRemoveUser", "AdminRemoveAdmin", "AdminTransferCard", "AdminTransferPass",
  45. "AdminGetPassOptions", "AdminAddCardToUser", "AdminRemoveCardFromUser", "AdminGetAdminPermissions",
  46. "AdminAddAdminPermissions", "AdminRemoveAdminPermissions", "AdminAddAdminApiPermissions", "AdminRemoveAdminApiPermissions",
  47. "AdminSetAdminApiPermissions", "AdminGetCustomCard", "AdminGetAdmins", "AdminSearchCards",
  48. "AdminSearchUsers", "AdminSearchAdmins"]
  49. PASS_FIELDS = ["logical_card_id", "issued", "activated", "deactivated", "firstused", "lastused",
  50. "nrides_orig", "nrides_remain", "nday_orig", "nday_expiration",
  51. "active", "expired", "queue_order",
  52. "rule", "comment", "paytype" ]
  53. CARD_FIELDS = ["mag_token", "rfid_token", "comment", "userid", "issued", "deactivated", "lastused",
  54. "firstused", "group_id", "issuetype"]
  55. USER_FIELDS = ["username", "comment", "first_name", "last_name", "phone",
  56. "email", "address", "city", "state", "zip", "passwordhash",
  57. "shipping_address", "shipping_city", "shipping_state", "shipping_zip",
  58. "shipping_name", "shipping_country_code", "shipping_country_name"]
  59. GROUP_FIELDS = ["id", "group_id", "group_name"]
  60. RULECLASS_FIELDS = ["id", "group_id", "group_name"]
  61. def Request(ctx):
  62. _conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
  63. res = {}
  64. if "function" in ctx:
  65. if ctx["function"] == "CardInfo":
  66. res = CardInfo(_conn, ctx)
  67. elif ctx["function"] == "UserInfo":
  68. res = UserInfo(_conn, ctx)
  69. elif ctx["function"] == "User":
  70. res = User(_conn, ctx)
  71. elif ctx["function"] == "Card":
  72. res = Card(_conn, ctx)
  73. elif ctx["function"] == "Pass":
  74. res = Pass(_conn, ctx)
  75. elif ctx["function"] == "Group":
  76. res = Group(_conn, ctx)
  77. elif ctx["function"] == "Ruleclass":
  78. res = Ruleclass(_conn, ctx)
  79. _conn.close()
  80. return res
  81. ## _ _ __
  82. ## ___ __ _ _ __ __| (_)_ __ / _| ___
  83. ## / __/ _` | '__/ _` | | '_ \| |_ / _ \
  84. ## | (_| (_| | | | (_| | | | | | _| (_) |
  85. ## \___\__,_|_| \__,_|_|_| |_|_| \___/
  86. ##
  87. def CardInfo(db, ctx):
  88. card_res = {}
  89. action = "get"
  90. if "action" in ctx:
  91. action = ctx["action"]
  92. if action == "get":
  93. print("CardInfo:", ctx)
  94. cardid = -1
  95. if "logical_card_id" in ctx:
  96. cardid = ctx["logical_card_id"]
  97. card_res["logical_card_id"] = cardid
  98. card_res = Card(db, {"action":"get", "logical_card_id": cardid})
  99. card_res["pass"] = []
  100. if card_res["result"] == "success":
  101. ## through each of the passes on the card
  102. ##
  103. pass_query = "select user_pass_id from user_pass where logical_card_id = %s and expired = 0 order by queue_order asc"
  104. pass_cursor = db.cursor()
  105. pass_cursor.execute(pass_query, [card_res["logical_card_id"]])
  106. pass_rows = pass_cursor.fetchall()
  107. for pass_row in pass_rows:
  108. pass_res = Pass(db, {"action":"get", "user_pass_id":pass_row[0]})
  109. card_res["pass"].append(pass_res)
  110. card_res["user"] = {}
  111. if ((card_res["userid"] is not None) and (int(card_res["userid"]) >= 0)):
  112. card_res["user"] = User(db, {"action":"get", "userid": card_res["userid"] })
  113. elif action == "search":
  114. card_res["cards"] = []
  115. res_cardid = Card(db, ctx)
  116. for cid in res_cardid["logical_card_ids"]:
  117. _c = CardInfo(db, {"action":"get", "logical_card_id":cid})
  118. card_res["cards"].append(_c)
  119. card_res["result"] = "success"
  120. return card_res
  121. ## _ __
  122. ## _ _ ___ ___ _ __(_)_ __ / _| ___
  123. ## | | | / __|/ _ \ '__| | '_ \| |_ / _ \
  124. ## | |_| \__ \ __/ | | | | | | _| (_) |
  125. ## \__,_|___/\___|_| |_|_| |_|_| \___/
  126. ##
  127. def UserInfo(db, ctx):
  128. res = {}
  129. res["result"] = "fail"
  130. userid = -1
  131. if ("userid" in ctx):
  132. userid = ctx["userid"]
  133. pass_fields = PASS_FIELDS.copy()
  134. card_fields = CARD_FIELDS.copy()
  135. user_fields = USER_FIELDS.copy()
  136. res["userid"] = userid
  137. cursor = db.cursor()
  138. ## fill in user data
  139. ##
  140. res["user"] = {}
  141. fields = USER_FIELDS.copy()
  142. query = "select " + ",".join(fields) + " from users where userid = %s"
  143. cursor.execute(query, [userid])
  144. row = cursor.fetchone()
  145. if row is None:
  146. res["api_comment"] = "user not found"
  147. return res
  148. res["user"]["userid"] = userid
  149. for idx,f in enumerate(user_fields):
  150. res["user"][f] = row[idx]
  151. ## go through each card and fill in card data and pass data
  152. ##
  153. res["card"] = []
  154. query = "select logical_card_id from user_card where userid = %s and active = 1 order by logical_card_id asc"
  155. card_cursor = db.cursor()
  156. card_cursor.execute(query, [userid])
  157. rows = card_cursor.fetchall()
  158. for row in rows:
  159. card_res = CardInfo(db, {"logical_card_id":row[0]})
  160. res["card"].append(card_res)
  161. res["result"] = "success"
  162. return res
  163. def _update_pass_bits(cursor, passid):
  164. q = "select logical_card_id from user_pass where user_pass_id = %s"
  165. cursor.execute(q, [passid])
  166. rows = cursor.fetchall()
  167. print("\n\nupdating pass bits", passid, "\n\n")
  168. cardid = -1
  169. for row in rows:
  170. cardid = row[0]
  171. break
  172. print("\n\nupdating pass bits cardid:", cardid, "\n\n")
  173. if cardid < 0: return
  174. q = "update user_pass set active = 0 where logical_card_id = %s"
  175. cursor.execute(q, [cardid])
  176. q = "update user_pass set active = 1 where logical_card_id = %s and expired = 0 and queue_order = " + \
  177. "( select min(x.queue_order) from user_pass x where x.logical_card_id = %s and x.expired = 0 )"
  178. cursor.execute(q, [cardid,cardid])
  179. ## _ _
  180. ## _ __ _ _| | ___ ___| | __ _ ___ ___
  181. ## | '__| | | | |/ _ \/ __| |/ _` / __/ __|
  182. ## | | | |_| | | __/ (__| | (_| \__ \__ \
  183. ## |_| \__,_|_|\___|\___|_|\__,_|___/___/
  184. ##
  185. def Ruleclass(db, ctx):
  186. res = {}
  187. ruleclass_fields = RULECLASS_FIELDS.copy()
  188. cursor = db.cursor()
  189. fields = ruleclass_fields.copy()
  190. field_vals = []
  191. if ctx["action"] == "search":
  192. query = "select id, rulename, ruleclass from rule_class"
  193. cursor.execute(query)
  194. rows = cursor.fetchall()
  195. res["ruleclass"] = []
  196. for row in rows:
  197. res["ruleclass"].append({"id":row[0], "rulename":row[1], "ruleclass":row[2]})
  198. db.commit()
  199. return res
  200. ##
  201. ## _ __ __ _ ___ ___
  202. ## | '_ \ / _` / __/ __|
  203. ## | |_) | (_| \__ \__ \
  204. ## | .__/ \__,_|___/___/
  205. ## |_|
  206. def Pass(db, ctx):
  207. res = {}
  208. passid = -1
  209. if ("user_pass_id" in ctx):
  210. passid = ctx["user_pass_id"]
  211. pass_fields = PASS_FIELDS.copy()
  212. cursor = db.cursor()
  213. fields = pass_fields.copy()
  214. field_vals = []
  215. if (ctx["action"] == "get"):
  216. query = "select " + ",".join(pass_fields) + " from user_pass where user_pass_id = %s"
  217. cursor.execute(query, [passid])
  218. row = cursor.fetchone()
  219. if row is not None:
  220. res["result"] = "success"
  221. res["user_pass_id"] = passid
  222. for idx,f in enumerate(pass_fields):
  223. if isinstance(row[idx], datetime.datetime):
  224. res[f] = row[idx].strftime("%Y-%m-%d %H:%M:%S")
  225. else:
  226. res[f] = row[idx]
  227. else:
  228. res["result"] = "fail"
  229. res["api_comment"] = "pass not found"
  230. elif (ctx["action"] == "add"):
  231. if (not "logical_card_id" in ctx) or (ctx["logical_card_id"] == ''):
  232. res["result"] = "fail"
  233. res["api_comment"] = "must have logical_card_id to add pass"
  234. else:
  235. ## fill in some default values
  236. ##
  237. dt = time.strftime('%Y-%m-%d %H:%M:%S')
  238. if "issued" not in ctx: ctx["issued"] = dt
  239. if "expired" not in ctx: ctx["expired"] = 0
  240. if "active" not in ctx: ctx["active"] = 0
  241. if "logical_card_id" in ctx:
  242. cardid = ctx["logical_card_id"]
  243. _q = "select queue_order from user_pass where logical_card_id = %s and expired = 0 order by queue_order desc limit 1"
  244. _c = db.cursor()
  245. _c.execute(_q, [cardid])
  246. _r = _c.fetchone()
  247. if _r is not None:
  248. ctx["queue_order"] = int(_r[0])+1
  249. else:
  250. ctx["active"] = 1
  251. ctx["queue_order"] = 0
  252. else:
  253. ctx["queue_order"] = 0
  254. for f in pass_fields:
  255. if f in ctx: field_vals.append(ctx[f])
  256. else: field_vals.append(None)
  257. query = "insert into user_pass (" + ",".join(fields) + ") values (" + ",".join(["%s"]*len(fields)) + ")"
  258. print(query)
  259. print(fields, field_vals)
  260. cursor.execute(query, field_vals)
  261. res["user_pass_id"] = cursor.lastrowid
  262. res["result"] = "success"
  263. _update_pass_bits(cursor, passid);
  264. elif (ctx["action"] == "update"):
  265. update_field = []
  266. update_val = []
  267. for f in pass_fields:
  268. if f in ctx:
  269. update_field.append(f + "= %s")
  270. update_val.append(ctx[f])
  271. update_val.append(passid)
  272. query = "update user_pass set " + ",".join(update_field) + " where user_pass_id = %s"
  273. cursor.execute(query, update_val)
  274. res["user_pass_id"] = passid
  275. res["result"] = "success"
  276. _update_pass_bits(cursor, passid);
  277. elif (ctx["action"] == "deactivate"):
  278. update_field = []
  279. update_val = []
  280. for f in pass_fields:
  281. if f in ctx:
  282. update_field.append(f + "= %s")
  283. update_val.append(ctx[f])
  284. update_val.append(passid)
  285. query = "update user_pass set active = 0, expired = 1 where user_pass_id = %s"
  286. cursor.execute(query, [passid])
  287. _update_pass_bits(cursor, passid);
  288. res["user_pass_id"] = passid
  289. res["result"] = "success"
  290. elif (ctx["action"] == "delete"):
  291. query = "delete from user_pass where user_pass_id = %s"
  292. cursor.execute(query, [passid])
  293. _update_pass_bits(cursor, passid);
  294. res["result"] = "success"
  295. db.commit()
  296. return res
  297. ## _
  298. ## ___ __ _ _ __ __| |
  299. ## / __/ _` | '__/ _` |
  300. ## | (_| (_| | | | (_| |
  301. ## \___\__,_|_| \__,_|
  302. ##
  303. def Card(db, ctx):
  304. card_fields = CARD_FIELDS.copy()
  305. res = {}
  306. cardid = -1
  307. if ("logical_card_id" in ctx):
  308. cardid = ctx["logical_card_id"]
  309. cursor = db.cursor()
  310. fields = card_fields.copy()
  311. field_vals = []
  312. if (ctx["action"] == "get"):
  313. query = "select " + ",".join(card_fields) + " from user_card where logical_card_id = %s"
  314. cursor.execute(query, [cardid])
  315. row = cursor.fetchone()
  316. if row is not None:
  317. res["logical_card_id"] = cardid
  318. for idx,f in enumerate(card_fields):
  319. if isinstance(row[idx], datetime.datetime):
  320. res[f] = row[idx].strftime("%Y-%m-%d %H:%M:%S")
  321. else:
  322. res[f] = row[idx]
  323. res["result"] = "success"
  324. else:
  325. res["result"] = "fail"
  326. res["api_comment"] = "card not found"
  327. elif (ctx["action"] == "add"):
  328. fields.append("active")
  329. for f in card_fields:
  330. if f in ctx: field_vals.append(ctx[f])
  331. else: field_vals.append(None)
  332. field_vals.append(1)
  333. query = "insert into user_card (" + ",".join(fields) + ") values (" + ",".join(["%s"]*len(fields)) + ")"
  334. cursor.execute(query, field_vals)
  335. res["logical_card_id"] = cursor.lastrowid
  336. res["result"] = "success"
  337. elif (ctx["action"] == "update"):
  338. if not "logical_card_id" in ctx:
  339. res["result"] = "fail"
  340. res["api_comment"] = "must supply a logical_card_id"
  341. else:
  342. update_field = []
  343. update_val = []
  344. query_card_id = ctx["logical_card_id"]
  345. cursor.execute("select logical_card_id from user_card where logical_card_id = %s", [query_card_id])
  346. rows = cursor.fetchall()
  347. if len(rows) == 0:
  348. res["result"] = "fail"
  349. res["api_comment"] = "card not found"
  350. else:
  351. print(">>>>", len(rows))
  352. for row in rows:
  353. logical_card_id = row[0]
  354. for f in card_fields:
  355. if f in ctx:
  356. update_field.append(f + "= %s")
  357. update_val.append(ctx[f])
  358. update_val.append(cardid)
  359. query = "update user_card set " + ",".join(update_field) + " where logical_card_id = %s"
  360. cursor.execute(query, update_val)
  361. res["logical_card_id"] = cardid
  362. res["result"] = "success"
  363. elif (ctx["action"] == "delete"):
  364. query = "delete from user_card where logical_card_id = %s"
  365. cursor.execute(query, [cardid])
  366. res["result"] = "success"
  367. elif (ctx["action"] == "search"):
  368. query = "select logical_card_id from user_card where "
  369. n_search = 0
  370. if "logical_card_id" in ctx:
  371. query += " logical_card_id = %s"
  372. field_vals.append( ctx["logical_card_id"])
  373. n_search += 1
  374. if "mag_token" in ctx:
  375. query += " mag_token like %s "
  376. field_vals.append( '%' + ctx["mag_token"] + '%')
  377. n_search += 1
  378. if "rfid_token" in ctx:
  379. if len(field_vals)>0: query += " and "
  380. query += " rfid_token like %s "
  381. field_vals.append( '%' + ctx["rfid_token"] + '%')
  382. n_search += 1
  383. query_limit = " "
  384. if "limit" in ctx:
  385. query_limit = " limit %s "
  386. search_vals.append(ctx["limit"])
  387. query += query_limit
  388. res["logical_card_ids"] = []
  389. if n_search > 0:
  390. cursor.execute(query, field_vals)
  391. rows = cursor.fetchall()
  392. for row in rows:
  393. res["logical_card_ids"].append(row[0])
  394. res["result"] = "success"
  395. db.commit()
  396. return res
  397. ##
  398. ## __ _ _ __ ___ _ _ _ __
  399. ## / _` | '__/ _ \| | | | '_ \
  400. ## | (_| | | | (_) | |_| | |_) |
  401. ## \__, |_| \___/ \__,_| .__/
  402. ## |___/ |_|
  403. def Group(db,ctx):
  404. group_res = { }
  405. action = "get"
  406. if "action" in ctx:
  407. cation = ctx["action"]
  408. cursor = db.cursor()
  409. if action == "get":
  410. print("Group:", ctx)
  411. group_res["group"] = []
  412. query = "select group_id, group_name from groups order by group_id asc"
  413. cursor.execute(query)
  414. rows = cursor.fetchall()
  415. for row in rows:
  416. group_res["group"].append({"group_id":row[0], "group_name":row[1]})
  417. db.commit()
  418. return group_res
  419. ##
  420. ## _ _ ___ ___ _ __
  421. ## | | | / __|/ _ \ '__|
  422. ## | |_| \__ \ __/ |
  423. ## \__,_|___/\___|_|
  424. ##
  425. def User(db, ctx):
  426. user_fields = USER_FIELDS.copy()
  427. res = {}
  428. cursor = db.cursor()
  429. fields = user_fields.copy()
  430. user_vals = []
  431. userid = -1
  432. if "userid" in ctx: userid = ctx["userid"]
  433. print("cp.user")
  434. ## USER GET
  435. ##
  436. if (ctx["action"] == "get"):
  437. query = "select " + ",".join(user_fields) + " from users where userid = %s"
  438. cursor.execute(query, [userid])
  439. row = cursor.fetchone()
  440. if row is not None:
  441. res["userid"] = userid
  442. for idx,f in enumerate(user_fields):
  443. if isinstance(row[idx], datetime.datetime):
  444. res[f] = row[idx].strftime("%Y-%m-%d %H:%M:%S")
  445. else:
  446. res[f] = row[idx]
  447. res["result"] = "success"
  448. else:
  449. res["result"] = "fail"
  450. res["api_comment"] = "user not found"
  451. ## USER ADD
  452. ##
  453. elif (ctx["action"] == "add"):
  454. if ((not "password" in ctx) or
  455. (not "username" in ctx) ):
  456. res["api_comment"] = "invalid parameters, need username and password to create account"
  457. res["result"] = "fail"
  458. else:
  459. uname = ctx["username"]
  460. pword = ctx["password"]
  461. fields.append("active")
  462. fields.append("created")
  463. for f in user_fields:
  464. if f in ctx: user_vals.append(ctx[f])
  465. elif f == "passwordhash":
  466. ha = hashlib.sha256()
  467. ha.update(str.encode(uname))
  468. ha.update(str.encode(pword))
  469. user_vals.append(ha.hexdigest())
  470. else: user_vals.append(None)
  471. user_vals.append(1)
  472. user_vals.append(time.strftime('%Y-%m-%d %H:%M:%S'))
  473. query = "insert into users (" + ",".join(fields) + ") values (" + ",".join(["%s"]*len(fields)) + ")"
  474. cursor.execute(query, user_vals)
  475. res["userid"] = cursor.lastrowid
  476. res["result"] = "success"
  477. ## USER UPDATE
  478. ##
  479. elif (ctx["action"] == "update"):
  480. if not "userid" in ctx:
  481. res["result"] = "fail"
  482. res["api_comment"] = "no userid specified"
  483. else:
  484. uname = ''
  485. query = "select username from users where userid = %s";
  486. cursor.execute(query, [userid])
  487. rows = cursor.fetchall()
  488. for row in rows:
  489. uname = row[0]
  490. if uname == '':
  491. res["result"] = "fail"
  492. res["api_comment"] = "could not find username"
  493. else:
  494. update_field = []
  495. update_val = []
  496. print("user_field:", user_fields)
  497. print("ctx:", ctx)
  498. for f in user_fields:
  499. if (f == "passwordhash") and ("password" in ctx):
  500. update_field.append(" passwordhash = %s ")
  501. ha = hashlib.sha256()
  502. ha.update(str.encode(uname))
  503. ha.update(str.encode(ctx["password"]))
  504. update_val.append(ha.hexdigest())
  505. elif f in ctx:
  506. update_field.append(f + "= %s")
  507. update_val.append(ctx[f])
  508. else:
  509. pass
  510. #update_val.append(None)
  511. update_val.append(userid)
  512. if len(update_field) == 0:
  513. print("NOPE")
  514. print("manage_user.update>>>", userid, ":".join(update_field), ":".join(update_val), len(update_field))
  515. query = "update users set " + ",".join(update_field) + " where userid = %s"
  516. print("WTFFF???", query)
  517. cursor.execute(query, update_val)
  518. res["userid"] = userid
  519. res["result"] = "success"
  520. ## USER DELETE
  521. ##
  522. elif (ctx["action"] == "delete"):
  523. query = "delete from users where userid = %s"
  524. cursor.execute(query, [userid])
  525. ## USER SEARCH
  526. ##
  527. elif (ctx["action"] == "search"):
  528. res["userids"] = []
  529. res["userid"] = userid
  530. res["result"] = "success"
  531. search_field = []
  532. search_val = []
  533. for f in user_fields:
  534. if f in ctx:
  535. search_field.append(f + " like %s")
  536. search_val.append('%' + ctx[f] + '%')
  537. query_limit = " "
  538. if "limit" in ctx:
  539. query_limit = " limit %s "
  540. search_val.append(ctx["limit"])
  541. query = "select userid from users where " + " and ".join(search_field) + query_limit
  542. cursor.execute(query, search_val)
  543. rows = cursor.fetchall()
  544. for row in rows:
  545. res["userids"].append(row[0])
  546. db.commit()
  547. return res
  548. def main(db):
  549. print("main")
  550. print("---------")
  551. print("---------")
  552. print("---------")
  553. res = User(db, {"action":"add", "username":"abe" })
  554. print("user.add:", res)
  555. res = User(db, {"action":"update", "username":"abeabe", "userid":res["userid"]})
  556. print("user.update:", res)
  557. res = User(db, {"action":"get", "userid":res["userid"]})
  558. print("user.get:", res)
  559. res = User(db, {"action":"delete", "userid": res["userid"]})
  560. print("user.delete:", res)
  561. print("---------")
  562. print("---------")
  563. print("---------")
  564. res = Card(db, {"action":"get", "logical_card_id":1})
  565. print("card.get:", res)
  566. res = Card(db, {"action":"add", "mag_token":"2:1234", "rfid_token":"26:20:415", "comment":"testing api", "userid":1})
  567. print("card.add:", res)
  568. res = Card(db, {"action":"update", "mag_token":"2:9234", "logical_card_id":res["logical_card_id"]})
  569. print("card.update:", res)
  570. res = Card(db, {"action":"delete", "logical_card_id":res["logical_card_id"]})
  571. print("card.delete:", res)
  572. print("---------")
  573. print("---------")
  574. print("---------")
  575. res = Pass(db, {"action":"get", "user_pass_id":11})
  576. print("pass.get:", res)
  577. res = Pass(db, {"action":"add", "logical_card_id":1, "queue_order":9, "rule":"TEST-ORG-NDAY", "nday_orig":3})
  578. print("pass.add:", res)
  579. res = Pass(db, {"action":"update", "user_pass_id":res["user_pass_id"], "queue_order":10, "rule":"TEST-ORG-NDAY", "nday_orig":5})
  580. print("pass.update:", res)
  581. res = Pass(db, {"action":"delete", "user_pass_id":res["user_pass_id"]})
  582. print("pass.delete:", res)
  583. print("---------")
  584. print("---------")
  585. print("---------")
  586. res = UserInfo(db, {"userid":348})
  587. print("userinfo:", json.dumps(res, indent=2))
  588. print("---------")
  589. print("---------")
  590. print("---------")
  591. res = Request({"function":"CardInfo", "action":"search", "logical_card_id":1})
  592. print("request.card.search:", res)
  593. if __name__ == "__main__":
  594. conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
  595. main(conn)
  596. conn.close()