Parcourir la source

adding more api functionality

clementinecomputing il y a 5 ans
Parent
commit
722086006b
1 fichiers modifiés avec 38 ajouts et 4 suppressions
  1. 38 4
      server/scripts/PopufareAPI.py

+ 38 - 4
server/scripts/PopufareAPI.py

@@ -53,7 +53,8 @@ Function = [
 
 PASS_FIELDS = ["logical_card_id", "issued", "activated", "deactivated", "firstused", "lastused",
                "nrides_orig", "nrides_remain", "nday_orig", "nday_expiration",
-               "expired", "rule", "queue_order", "comment", "paytype" ]
+               "active", "expired", "queue_order",
+               "rule", "comment", "paytype" ]
 
 CARD_FIELDS = ["mag_token", "rfid_token", "comment", "userid", "issued", "deactivated", "lastused",
                "firstused", "group_id", "issuetype"]
@@ -87,12 +88,15 @@ def Request(ctx):
 def CardInfo(db, ctx):
   card_res = {}
 
+
   action = "get"
   if "action" in ctx:
     action = ctx["action"]
 
   if action == "get":
 
+    print("CardInfo:", ctx)
+
     cardid = -1
     if "logical_card_id" in ctx:
       cardid = ctx["logical_card_id"]
@@ -103,7 +107,7 @@ def CardInfo(db, ctx):
 
     ## through each of the passes on the card
     ##
-    pass_query = "select user_pass_id from user_pass where logical_card_id = %s and active = 1 and expired = 0 order by queue_order asc"
+    pass_query = "select user_pass_id from user_pass where logical_card_id = %s and expired = 0 order by queue_order asc"
     pass_cursor = db.cursor()
     pass_cursor.execute(pass_query, [card_res["logical_card_id"]])
     pass_rows = pass_cursor.fetchall()
@@ -112,6 +116,11 @@ def CardInfo(db, ctx):
       pass_res = Pass(db, {"action":"get", "user_pass_id":pass_row[0]})
       card_res["pass"].append(pass_res)
 
+    card_res["user"] = {}
+    if ((card_res["userid"] is not None) and (int(card_res["userid"]) >= 0)):
+      card_res["user"] = User(db, {"action":"get", "userid": card_res["userid"] })
+
+
   elif action == "search":
 
     card_res["cards"] = []
@@ -149,6 +158,7 @@ def UserInfo(db, ctx):
     res["api_comment"] = "user not found"
     return res
 
+  res["user"]["userid"] = userid
   for idx,f in enumerate(user_fields):
     res["user"][f] = row[idx]
 
@@ -195,16 +205,40 @@ def Pass(db, ctx):
 
   elif (ctx["action"] == "add"):
 
-    fields.append("active")
+    ## fill in some default values
+    ##
+    dt = time.strftime('%Y-%m-%d %H:%M:%S')
+    if "issued" not in ctx: ctx["issued"] = dt
+    if "expired" not in ctx: ctx["expired"] = 0
+    if "active" not in ctx: ctx["active"] = 0
+
+    if "queue_order" not in ctx:
+      if "logical_card_id" in ctx:
+        cardid = ctx["logical_card_id"]
+        _q = "select queue_order from user_pass where logical_card_id = %s and expired = 0 order by queue_order desc limit 1"
+        _c = db.cursor()
+        _c.execute(_q, [cardid])
+        _r = _c.fetchone()
+        if _r is not None:
+          ctx["queue_order"] = int(_r[0])+1
+        else:
+          ctx["active"] = 1
+
     for f in pass_fields:
       if f in ctx:  field_vals.append(ctx[f])
       else:         field_vals.append(None)
-    field_vals.append(1)
 
     query = "insert into user_pass (" + ",".join(fields) + ") values (" + ",".join(["%s"]*len(fields)) + ")"
+
+    print(query)
+    print(fields, field_vals)
+
     cursor.execute(query, field_vals)
+
     res["user_pass_id"] = cursor.lastrowid
 
+    print(">>pass.add", query, res)
+
   elif (ctx["action"] == "update"):
     update_field = []
     update_val = []