瀏覽代碼

wip

* this work in in parallel to the Admin UI, see that project for more details
  on added functionality
clementinecomputing 5 年之前
父節點
當前提交
6c12b69836
共有 1 個文件被更改,包括 121 次插入22 次删除
  1. 121 22
      server/scripts/PopufareAPI.py

+ 121 - 22
server/scripts/PopufareAPI.py

@@ -64,6 +64,9 @@ USER_FIELDS = ["username", "comment", "first_name", "last_name", "phone",
                "shipping_address", "shipping_city", "shipping_state", "shipping_zip",
                "shipping_name", "shipping_country_code", "shipping_country_name"]
 
+GROUP_FIELDS = ["id", "group_id", "group_name"]
+RULECLASS_FIELDS = ["id", "group_id", "group_name"]
+
 def Request(ctx):
   _conn = mysql.connector.connect(user=_USER, password=_PASSWORD, host=_HOST, database=_DATABASE, port=_PORT)
 
@@ -79,12 +82,15 @@ def Request(ctx):
       res = Card(_conn, ctx)
     elif ctx["function"] == "Pass":
       res = Pass(_conn, ctx)
+    elif ctx["function"] == "Group":
+      res = Group(_conn, ctx)
+    elif ctx["function"] == "Ruleclass":
+      res = Ruleclass(_conn, ctx)
 
   _conn.close()
 
   return res
 
-
 def CardInfo(db, ctx):
   card_res = {}
 
@@ -175,6 +181,54 @@ def UserInfo(db, ctx):
 
   return res
 
+def _update_pass_bits(cursor, passid):
+  q = "select logical_card_id from user_pass where user_pass_id = %s"
+  cursor.execute(q, [passid])
+  rows = cursor.fetchall()
+
+  print("\n\nupdating pass bits", passid, "\n\n")
+
+  cardid = -1
+  for row in rows:
+    cardid = row[0]
+    break
+
+  print("\n\nupdating pass bits cardid:", cardid, "\n\n")
+
+  if cardid < 0: return
+
+  q = "update user_pass set active = 0 where logical_card_id = %s"
+  cursor.execute(q, [cardid])
+
+  q = "update user_pass set active = 1 where logical_card_id = %s and expired = 0 and queue_order = " + \
+  "( select min(x.queue_order) from user_pass x where x.logical_card_id = %s and x.expired = 0 )"
+  cursor.execute(q, [cardid,cardid])
+
+
+def Ruleclass(db, ctx):
+  res = {}
+
+  ruleclass_fields = RULECLASS_FIELDS.copy()
+
+  cursor = db.cursor()
+  fields = ruleclass_fields.copy()
+  field_vals = []
+
+  if ctx["action"] == "search":
+    query = "select id, rulename, ruleclass from rule_class"
+
+    cursor.execute(query)
+    rows = cursor.fetchall()
+
+    res["ruleclass"] = []
+
+    for row in rows:
+      res["ruleclass"].append({"id":row[0], "rulename":row[1], "ruleclass":row[2]})
+
+  db.commit()
+  return res
+
+
 def Pass(db, ctx):
   res = {}
   passid = -1
@@ -196,7 +250,7 @@ def Pass(db, ctx):
       res["user_pass_id"] = passid 
       for idx,f in enumerate(pass_fields):
         if isinstance(row[idx], datetime.datetime):
-          res[f] = row[idx].strftime("%Y-%M-%d %H:%m:%S")
+          res[f] = row[idx].strftime("%Y-%m-%d %H:%M:%S")
         else:
           res[f] = row[idx]
     else:
@@ -212,17 +266,20 @@ def Pass(db, ctx):
     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
+
+    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
+        ctx["queue_order"] = 0
+    else:
+      ctx["queue_order"] = 0
 
     for f in pass_fields:
       if f in ctx:  field_vals.append(ctx[f])
@@ -230,14 +287,14 @@ def Pass(db, ctx):
 
     query = "insert into user_pass (" + ",".join(fields) + ") values (" + ",".join(["%s"]*len(fields)) + ")"
 
-    print(query)
-    print(fields, field_vals)
+    #print(query)
+    #print(fields, field_vals)
 
     cursor.execute(query, field_vals)
 
     res["user_pass_id"] = cursor.lastrowid
 
-    print(">>pass.add", query, res)
+    _update_pass_bits(cursor, passid);
 
   elif (ctx["action"] == "update"):
     update_field = []
@@ -253,9 +310,29 @@ def Pass(db, ctx):
     cursor.execute(query, update_val)
     res["user_pass_id"] = passid
 
+    _update_pass_bits(cursor, passid);
+
+  elif (ctx["action"] == "deactivate"):
+    update_field = []
+    update_val = []
+
+    for f in pass_fields:
+      if f in ctx:
+        update_field.append(f + "= %s")
+        update_val.append(ctx[f])
+    update_val.append(passid)
+
+    query = "update user_pass set active = 0, expired = 1 where user_pass_id = %s"
+    cursor.execute(query, [passid])
+
+    _update_pass_bits(cursor, passid);
+    res["user_pass_id"] = passid
+
+
   elif (ctx["action"] == "delete"):
     query = "delete from user_pass where user_pass_id = %s"
     cursor.execute(query, [passid])
+    _update_pass_bits(cursor, passid);
 
   db.commit()
   return res
@@ -281,7 +358,7 @@ def Card(db, ctx):
       res["logical_card_id"] = cardid 
       for idx,f in enumerate(card_fields):
         if isinstance(row[idx], datetime.datetime):
-          res[f] = row[idx].strftime("%Y-%M-%d %H:%m:%S")
+          res[f] = row[idx].strftime("%Y-%m-%d %H:%M:%S")
         else:
           res[f] = row[idx]
     else:
@@ -311,8 +388,6 @@ def Card(db, ctx):
 
     query = "update user_card set " + ",".join(update_field) + " where logical_card_id = %s"
 
-    print(">>>", query, update_val)
-
     cursor.execute(query, update_val)
     res["logical_card_id"] = cardid
 
@@ -360,6 +435,30 @@ def Card(db, ctx):
 
   return res
 
+def Group(db,ctx):
+  group_res = { }
+
+  action = "get"
+  if "action" in ctx:
+    cation = ctx["action"]
+
+  cursor = db.cursor()
+
+  if action == "get":
+    print("Group:", ctx)
+
+    group_res["group"] = []
+
+    query = "select group_id, group_name from groups order by group_id asc"
+    cursor.execute(query)
+    rows = cursor.fetchall()
+    for row in rows:
+      group_res["group"].append({"group_id":row[0], "group_name":row[1]})
+
+  db.commit()
+  return group_res
+
+
 def User(db, ctx):
   user_fields = USER_FIELDS.copy()
   res = {}
@@ -380,7 +479,7 @@ def User(db, ctx):
       res["userid"] = userid
       for idx,f in enumerate(user_fields):
         if isinstance(row[idx], datetime.datetime):
-          res[f] = row[idx].strftime("%Y-%M-%d %H:%m:%S")
+          res[f] = row[idx].strftime("%Y-%m-%d %H:%M:%S")
         else:
           res[f] = row[idx]
     else:
@@ -433,10 +532,10 @@ def User(db, ctx):
     query_limit = " "
     if "limit" in ctx:
       query_limit = " limit %s "
-      search_vals.append(ctx["limit"])
+      search_val.append(ctx["limit"])
 
     query = "select userid from users where " + " and ".join(search_field) + query_limit
-    cursor.execute(query, search_vals)
+    cursor.execute(query, search_val)
     rows = cursor.fetchall()
     for row in rows:
       res["userids"].append(row[0])