Ver Fonte

add card block initial pass done

* need to do more error checking
* lessons learned: keep api as simple as possible, assume it
  gets the values it needs, let the higher level stuff figure out
  how to conform to what it expects
* offload that busy work to the front end
  - might need the fron end to get more state about what the groups should be
    and how to construct the rules, but this is appropriate (imo)
abetusk há 4 anos atrás
pai
commit
231354941d

+ 22 - 0
main/app/routes.py

@@ -246,6 +246,28 @@ def api_group_info():
 ###
 ###
 
+@app.route("/api/v1/AddCardBlock")
+def api_add_card_block():
+  res = {}
+  args = request.args
+
+  field_names = ["mag_token", "rfid_token", "group_id", "count",
+                 "pass_class",
+                 "pass_nrides_remain", "pass_nrides_orig",
+                 "pass_nday_orig", "pass_rule" ]
+
+  api_req = { "function":"AddCardBlock" }
+  for f in field_names:
+    if f in args:
+      api_req[f] = args[f]
+
+  res = api.Request(api_req)
+  return jsonify(res)
+
+
+###
+###
+
 @app.route("/api/v1/RecycleCard")
 def api_recycle_card():
   res = {}

+ 170 - 0
main/app/static/js/popufare_admin.js

@@ -36,6 +36,35 @@ function update_message(ui_id, msg_type, msg_text) {
   ele.innerHTML = msg_text;
 }
 
+function update_pass_radio_button(page, pass_choice) {
+  var _rid = ["pass_none", "pass_nride", "pass_nday", "pass_other"];
+
+  for (var ii=0; ii<_rid.length; ii++) {
+    var ele = document.getElementById("ui." + page + "." + _rid[ii]);
+    ele.checked = false;
+  }
+
+  if (pass_choice === "none") {
+    var ele = document.getElementById("ui." + page + ".pass_none");
+    ele.checked = true;
+  }
+  else if (pass_choice === "nride") {
+    var ele = document.getElementById("ui." + page + ".pass_nride");
+    ele.checked = true;
+  }
+  else if (pass_choice === "nday") {
+    var ele = document.getElementById("ui." + page + ".pass_nday");
+    ele.checked = true;
+  }
+  else if (pass_choice === "other") {
+    var ele = document.getElementById("ui." + page + ".pass_other");
+    ele.checked = true;
+  }
+
+}
+
+
+
 function api_resp(xhr,extra) {
   if (xhr.readyState === XMLHttpRequest.DONE) {
     if (xhr.status === 200) {
@@ -174,6 +203,18 @@ function api_resp(xhr,extra) {
         }
       }
 
+      else if (extra["ui_function"] === "add_card_block.add") {
+
+        if (dat["result"] === "success") {
+          update_message("ui.add_card_block.message", "success", "loaded");
+        }
+        else {
+          update_message("ui.add_card_block.message", "error", dat.api_comment);
+        }
+
+
+      }
+
       else if (extra["ui_function"] === "group") {
       }
 
@@ -231,6 +272,14 @@ function _get_select(fid) {
   return null;
 }
 
+function _get_select_id(fid) {
+  var ele = document.getElementById(fid);
+  if (ele) {
+    return ele.options[ele.selectedIndex].id;
+  }
+  return null;
+}
+
 function _date_only(s) {
   if (!s) { return s; }
   if (s.length > 0) {
@@ -524,6 +573,115 @@ function manage_user_find_fill(data) {
   window.history.replaceState({}, document.title, "/manage_user");
 }
 
+// --------------------------------------
+// --------------------------------------
+// --------------------------------------
+// --------------------------------------
+// --------------------------------------
+
+function add_card_block() {
+  var pass = [ "none", "nride", "nday", "other" ];
+
+  var mag_token = _get_input_field("ui.add_card_block.mag_token");
+  var rfid_pfx = _get_input_field("ui.add_card_block.rfid_prefix");
+  var rfid_val = _get_input_field("ui.add_card_block.rfid_value");
+  var group_id = _get_select_id("ui.add_card_block.group");
+  var count = _get_input_field("ui.add_card_block.count")
+
+  var rfid_token = "";
+  if (rfid_pfx && (rfid_pfx.length > 0) && rfid_val && (rfid_val.length > 0)) {
+    rfid_token = "26:" + rfid_pfx + ":" + rfid_val;
+  }
+
+  if ((mag_token.length==0) && (rfid_token.length==0)) {
+    update_message("ui.add_card_block.message", "error", "Please provide Magstripe or RFID");
+    return;
+  }
+
+  if (count == "")  {
+    update_message("ui.add_card_block.message", "error", "Count must be positive");
+    return;
+  }
+
+  update_message("ui.add_card_block.message", "success", "");
+
+  var dat = {
+    "api_function":"AddCardBlock",
+    "ui_function":"add_card_block.add",
+    "data": [
+      ["group_id",group_id],
+      ["count",count]
+    ]
+  };
+
+  if (mag_token.length > 0) {
+    if (mag_token.split(":").length == 1) {
+      mag_token = "2:" + mag_token;
+    }
+    dat.data.push(["mag_token", mag_token]);
+  }
+  if (rfid_token.length > 0) {
+    if (rfid_token.split(":").length  < 2) {
+      rfid_token = "26:" + rfid_token;
+    }
+    dat.data.push(["rfid_token", rfid_token]);
+  }
+
+  var pass_kind = "none";
+  for (var ii=0; ii<pass.length; ii++) {
+    var ele = document.getElementById("ui.add_card_block.pass_" + pass[ii]);
+    if (ele.checked) {
+      pass_kind = pass[ii];
+      break;
+    }
+  }
+
+  if (pass_kind == "other") {
+    dat.data.push(["pass_class" , 'OTHER']);
+    dat.data.push(["pass_rule" , _get_select("ui.add_card_block.pass_other_rule") ]);
+  }
+
+  else if (pass_kind === "nday")  {
+    var pass_nday_type = _get_select("ui.add_card_block.pass_nday_type");
+    var pass_nday_region = _get_select("ui.add_card_block.pass_nday_region");
+    var pass_nday_quantity = _get_select("ui.add_card_block.pass_nday_quantity");
+    var pass_nday_other = _get_input_field("ui.add_card_block.pass_nday_quantity");
+
+    var rule = 'TEST-ORG-NDAY';
+    var nday = pass_nday_quantity;
+    if (pass_nday_other && pass_nday_other.length > 0) {
+      nday = pass_nday_other;
+    }
+
+    dat.data.push(["pass_class" , 'NDAY']);
+    dat.data.push(["pass_rule", rule]);
+    dat.data.push(["pass_nday_orig", nday]);
+  }
+
+  else if (pass_kind === "nride") {
+    var pass_nride_type = _get_select("ui.add_card_block.pass_nride_type");
+    var pass_nride_region = _get_select("ui.add_card_block.pass_nride_region");
+    var pass_nride_quantity = _get_input_field("ui.add_card_block.pass_nride_quantity");
+    var pass_nride_other = _get_input_field("ui.add_card_block.pass_nride_other");
+
+    var rule = 'TEST-ORG-NRIDE';
+    var nride = pass_nride_quantity;
+    if (pass_nride_other && pass_nride_other.length > 0) {
+      nride = pass_nride_other;
+    }
+
+    dat.data.push(["pass_class", 'NRIDE']);
+    dat.data.push(["pass_rule", rule]);
+    dat.data.push(["pass_nrides_orig", nride]);
+    dat.data.push(["pass_nrides_remain", nride]);
+
+  }
+
+  console.log("add_card_block: sending", dat);
+  api_req(dat);
+}
+
+
 // --------------------------------------
 // --------------------------------------
 // --------------------------------------
@@ -1093,6 +1251,18 @@ function admin_api_init() {
     }
   }
 
+  else if (_func === "add_card_block") {
+    _attach_button( "ui.add_card_block.add_card_block", add_card_block);
+
+    _attach_button( "ui.add_card_block.pass_none", (function () { update_pass_radio_button("add_card_block", "none"); }) );
+    _attach_button( "ui.add_card_block.pass_nride", (function () { update_pass_radio_button("add_card_block", "nride"); }) );
+    _attach_button( "ui.add_card_block.pass_nday", (function () { update_pass_radio_button("add_card_block", "nday"); }) );
+    _attach_button( "ui.add_card_block.pass_other", (function () { update_pass_radio_button("add_card_block", "other"); }) );
+
+    update_pass_radio_button("add_card_block", "none");
+
+  }
+
 
 }
 

+ 30 - 28
main/app/templates/add_card_block.html

@@ -2,7 +2,15 @@
 
 {% block content %}
 
-  <h1 class="mt-4">Add Card Block</h1>
+  <div class='row'>
+    <div class='col-6'>
+      <h1 class="mt-4">Add Card Block</h1>
+    </div>
+
+    <div class='col-6'>
+      <h2 class="mt-4" id='ui.add_card_block.message' >...</h2>
+    </div>
+  </div>
 
   <div class='container'>
 
@@ -19,17 +27,17 @@
             <div class='row rowpop row-shade'>
               <div class='col-5'>Start MagStripe</div>
               <div class='col-7'>
-                <input type='text' class='form-control form-control-sm' placeholder=''>
+                <input type='text' class='form-control form-control-sm' placeholder='' id='ui.add_card_block.mag_token'>
               </div>
             </div>
 
             <div class='row rowpop '>
               <div class='col-5'>Start RFID</div>
               <div class='col-3'>
-                <input type='text' class='form-control form-control-sm' placeholder=''>
+                <input type='text' class='form-control form-control-sm' placeholder='' id='ui.add_card_block.rfid_prefix'>
               </div>
               <div class='col-4'>
-                <input type='text' class='form-control form-control-sm' placeholder=''>
+                <input type='text' class='form-control form-control-sm' placeholder='' id='ui.add_card_block.rfid_value'>
               </div>
             </div>
 
@@ -39,7 +47,7 @@
 
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.group'>
                     {% for g in groups %}
                     <option id='{{ g.group_id }}'>{{ g.group_name }}</option>
                     {% endfor %}
@@ -54,20 +62,14 @@
           <div class='col-6'>
 
             <div class='row rowpop row-shade'>
-              <div class='col-5'>End MagStripe</div>
+              <div class='col-5'>Count</div>
               <div class='col-7'>
-                <input type='text' class='form-control form-control-sm' placeholder=''>
+                <input type='text' id='ui.add_card_block.count' class='form-control form-control-sm' placeholder=''>
               </div>
+
             </div>
 
             <div class='row rowpop '>
-              <div class='col-5'>End RFID</div>
-              <div class='col-3'>
-                <input type='text' class='form-control form-control-sm' placeholder=''>
-              </div>
-              <div class='col-4'>
-                <input type='text' class='form-control form-control-sm' placeholder=''>
-              </div>
             </div>
 
           </div>
@@ -80,7 +82,7 @@
 
         <div class='row'>
           <div class='col-12'>
-            <button type='button' class='btn btn-secondary btn-sm'>Add Card Block</button>
+            <button type='button' class='btn btn-secondary btn-sm' id='ui.add_card_block.add_card_block'>Add Card Block</button>
           </div>
         </div>
 
@@ -107,7 +109,7 @@
             <!-- none -->
             <div class='row rowpop row-shade'>
               <div class='col-1'>
-                <input type='radio' aria-label='pass-none'>
+                <input type='radio' aria-label='pass-none' id='ui.add_card_block.pass_none'>
               </div>
               <div class='col-2'>None</div>
               <div class='col-3'></div>
@@ -118,13 +120,13 @@
             <!-- multi ride -->
             <div class='row rowpop '>
               <div class='col-1'>
-                <input type='radio' aria-label='initial-pass-multi-ride'>
+                <input type='radio' aria-label='initial-pass-multi-ride' id='ui.add_card_block.pass_nride'>
               </div>
               <div class='col-2'>Multi Ride</div>
               <div class='col-2'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_pass_nride_type'>
                     <option>Adult</option>
                     <option>Half</option>
                   </select>
@@ -135,7 +137,7 @@
               <div class='col-2'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_nride_region'>
                     <option>Region 0</option>
                     <option>Region 1</option>
                   </select>
@@ -146,7 +148,7 @@
               <div class='col-2'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_nride_quantity'>
                     <option>1</option>
                     <option>2</option>
                     <option>5</option>
@@ -162,7 +164,7 @@
                   <div class='input-group-prepend'>
                     <span class='input-group-text'>Other</span>
                   </div>
-                  <input type='text' class='form-control form-control-sm' placeholder=''>
+                  <input type='text' class='form-control form-control-sm' placeholder='' id='ui.add_card_block.pass_nride_other'>
                 </div>
               </div>
 
@@ -173,14 +175,14 @@
             <div class='row rowpop row-shade'>
 
               <div class='col-1'>
-                <input type='radio' aria-label='initial-pass-multi-day'>
+                <input type='radio' aria-label='initial-pass-multi-day' id='ui.add_card_block.pass_nday'>
               </div>
               <div class='col-2'>Multi Day</div>
 
               <div class='col-2'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_nday_type'>
                     <option>Adult</option>
                     <option>Half</option>
                   </select>
@@ -191,7 +193,7 @@
               <div class='col-2'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_nday_region'>
                     <option>Region 0</option>
                     <option>Region 1</option>
                   </select>
@@ -202,7 +204,7 @@
               <div class='col-2'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_nday_quantity'>
                     <option>1</option>
                     <option>2</option>
                     <option>5</option>
@@ -218,7 +220,7 @@
                   <div class='input-group-prepend'>
                     <span class='input-group-text'>Other</span>
                   </div>
-                  <input type='text' class='form-control form-control-sm' placeholder=''>
+                  <input type='text' class='form-control form-control-sm' placeholder='' id='ui.add_card_block.pass_nday_other'>
                 </div>
               </div>
 
@@ -226,13 +228,13 @@
 
             <div class='row rowpop '>
               <div class='col-1'>
-                <input type='radio' aria-label='initial-pass-other'>
+                <input type='radio' aria-label='initial-pass-other' id='ui.add_card_block.pass_other'>
               </div>
               <div class='col-2'>Other</div>
               <div class='col-9'>
 
                 <div class='input-group input-group-sm mb-3'>
-                  <select class='custom-select'>
+                  <select class='custom-select' id='ui.add_card_block.pass_other_rule'>
                     {% for rc in cache["other_pass"] %}
                       <option id='{{ rc.rulename }}'>{{ rc.rulename }}</option>
                     {% endfor %}