Ver Fonte

recycle cards working

* still needs a lot of cleanup/refactoring/checking etc.

NOT FOR PRODUCTION USE
clementinecomputing há 4 anos atrás
pai
commit
669522c8f5
1 ficheiros alterados com 116 adições e 3 exclusões
  1. 116 3
      server/scripts/PopufareAPI.py

+ 116 - 3
server/scripts/PopufareAPI.py

@@ -87,6 +87,8 @@ def Request(ctx):
       res = Group(_conn, ctx)
     elif ctx["function"] == "Ruleclass":
       res = Ruleclass(_conn, ctx)
+    elif ctx["function"] == "RecycleCard":
+      res = RecycleCard(_conn, ctx)
 
   _conn.close()
 
@@ -519,16 +521,15 @@ def Card(db, ctx):
 ##  |___/                |_|    
 
 def Group(db,ctx):
-  group_res = { }
+  group_res = { "result":"fail"}
 
   action = "get"
   if "action" in ctx:
-    cation = ctx["action"]
+    action = ctx["action"]
 
   cursor = db.cursor()
 
   if action == "get":
-    print("Group:", ctx)
 
     group_res["group"] = []
 
@@ -538,6 +539,24 @@ def Group(db,ctx):
     for row in rows:
       group_res["group"].append({"group_id":row[0], "group_name":row[1]})
 
+    group_res["result"] = "success"
+
+  elif action == "getone":
+
+    group_res["group"] = []
+
+    query = "select group_id, group_name from groups where group_name = %s "
+    cursor.execute(query, [ctx["group"]])
+    row = cursor.fetchone()
+    if not row:
+      group_res["result"] = "fail"
+      group_res["api_comment"] = "invalid group"
+    else:
+      group_res["result"] = "success"
+      group_res["group_id"] = row[0]
+  elif action == "default":
+    return Group(db, {"action":"getone", "group":"TEST-ORG"})
+
   db.commit()
   return group_res
 
@@ -707,6 +726,100 @@ def User(db, ctx):
 
   return res
 
+##                           _                          _
+##  _ __ ___  ___ _   _  ___| | ___    ___ __ _ _ __ __| |
+## | '__/ _ \/ __| | | |/ __| |/ _ \  / __/ _` | '__/ _` |
+## | | |  __/ (__| |_| | (__| |  __/ | (_| (_| | | | (_| |
+## |_|  \___|\___|\__, |\___|_|\___|  \___\__,_|_|  \__,_|
+##                |___/
+
+def RecycleCard(db, ctx):
+  res = {"result":"fail"}
+  cursor = db.cursor()
+
+  fields = ["logical_card_id", "rfid_token", "mag_token", "group",
+            "userid",
+            "rule", "pass_class", "nrides_remain", "nrides_orig", "nday_orig" ]
+  val = {}
+  for f in fields:
+    if f in ctx:
+      if ctx[f] and len(ctx[f]) > 0:
+        val[f] = ctx[f]
+
+  if ((not ("logical_card_id" in val)) and
+      (not ("rfid_token" in val)) and
+      (not ("mag_token" in val))):
+    return res
+
+  logical_card_id = -1
+  if not ("logical_card_id" in val):
+    if "mag_token" in val:
+      query = "select logical_card_id from user_card where mag_token = %s and active = 1" 
+      cursor.execute(query, [val["mag_token"]])
+      cid =  cursor.fetchone()
+      if cid is not None:
+        val["logical_card_id"] = cid[0]
+      else:
+        return res
+    elif "rfid_token" in val:
+      query = "select logical_card_id from user_card where rfid_token = %s and active = 1" 
+      cursor.execute(query, [val["rfid_token"]])
+      cid = cursor.fetchone()
+      if cid is not None:
+        val["logical_card_id"] = cid[0]
+      else:
+        return res
+
+
+  group_info = Group(db, {"action":"default"})
+  if "group" in val:
+    group_info = Group(db, {"action":"getone", "group":val["group"]})
+
+  if group_info["result"] != "success":
+    res["api_comment"] = "invalid group"
+    return res
+
+  query = "update user_card set active = 0 where logical_card_id = %s"
+  cursor.execute(query, [val["logical_card_id"]])
+
+  card_info = {"action":"add", "group_id":group_info["group_id"], "active":1}
+  if "mag_token" in val: card_info["mag_token"] = val["mag_token"]
+  if "rfid_token" in val: card_info["rfid_token"] = val["rfid_token"]
+
+  card_res = Card(db, card_info)
+  if card_res["result"] != "success":
+    res["result"] = "fail"
+    res["api_comment"] = "failed to find card"
+    return res
+
+  res["logical_card_id"] = card_res["logical_card_id"]
+
+  if "pass_class" in val:
+
+    pass_opt = {"action":"add", "logical_card_id": res["logical_card_id"] }
+
+    if val["pass_class"] == "OTHER":
+      pass_opt["rule"] = val["rule"]
+      Pass(db, pass_opt)
+    elif val["pass_class"] == "NRIDE":
+      pass_opt["rule"] = val["rule"]
+      pass_opt["nrides_orig"] = val["nrides_orig"]
+      pass_opt["nrides_remain"] = val["nrides_remain"]
+      Pass(db, pass_opt)
+    elif val["pass_class"] == "NDAY":
+      pass_opt["rule"] = val["rule"]
+      pass_opt["nday_orig"] = val["nday_orig"]
+      Pass(db, pass_opt)
+    
+
+  return res
+
+##                  _       
+##  _ __ ___   __ _(_)_ __  
+## | '_ ` _ \ / _` | | '_ \ 
+## | | | | | | (_| | | | | |
+## |_| |_| |_|\__,_|_|_| |_|
+##                          
 
 def main(db):
   print("main")