Abstract template table rows
authorKarl O. Pinc <kop@karlpinc.com>
Mon, 29 Jul 2024 16:28:22 +0000 (11:28 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Mon, 29 Jul 2024 16:30:30 +0000 (11:30 -0500)
src/pgwui_common/templates/auth_base.mak

index 133ab3c839e128d55dedbe67ddb1da44dd367835..e4d2d129e124705981e383c480eb0aaa62bb685c 100644 (file)
 
 <%!
     from pgwui_common.path import asset_abspath
+    import attrs
 
     errors_base_mak = asset_abspath('pgwui_common:templates/errors_base.mak')
     auth_parts_mak = asset_abspath('pgwui_common:templates/auth_parts.mak')
     lib_mak = asset_abspath('pgwui_common:templates/lib.mak')
+
+    @attrs.define
+    class TabIndex():
+        val = attrs.field(default=1)
+
+        def inc(self, amount=1):
+            self.val += amount
 %>
 
 <%inherit file="${errors_base_mak}" />
   </%parent:navbar_content>
 </%def>
 
+<%doc> All the *_row() defs take a tab_index and, as a side effect,
+       increment the tab_index with the number of rows added to the
+       table.
+</%doc>
+
+<%def name="database_row(tab_index)">
+  <tr>
+    <td class="label">
+      <label for="db_id">Database:</label>
+    </td>
+    <td>
+      <input name="db"
+             tabindex="${tab_index.val}"
+             id="db_id"
+             type="text"
+             size="30"
+             value="${db}"
+             />
+    </td>
+  </tr>
+  <% tab_index.inc() %>
+</%def>
+
+<%def name="format_row(tab_index)">
+  <tr>
+    <td class="label">Uploaded Data Format:</td>
+    <td>
+      <input name="upload_fmt"
+             id="upload_fmt_csv_id"
+             tabindex="${tab_index.val}"
+             type="radio"
+             value="${csv_value}"
+             ${csv_checked | n}
+             />
+      <label class="label" for="upload_fmt_csv_id">CSV</label>
+      <br />
+      <input name="upload_fmt"
+             id="upload_fmt_tab_id"
+             tabindex="${tab_index.val + 1}"
+             type="radio"
+             value="${tab_value}"
+             ${tab_checked | n}
+             />
+      <label class="label" for="upload_fmt_tab_id">Tab delimited</label>
+    </td>
+  </tr>
+  <% tab_index.inc(2) %>
+</%def>
+
+<%def name="nulls_row(tab_index)">
+  % if upload_nulls:
+    <tr>
+      <td class="label">
+        <label for="upload_null_id">Upload NULL Values: </label>
+      </td>
+      <td>
+        <input name="upload_null"
+               id="upload_null_id"
+               tabindex="${tab_index.val}"
+               type="checkbox"
+               ${upload_null | n}
+               />
+      </td>
+    </tr>
+    <tr>
+      <td class="label">
+        <label for="null_rep_id">NULL Representation: </label>
+      </td>
+      <td>
+        <input name="null_rep"
+               id="null_rep_id"
+               tabindex="${tab_index.val + 1}"
+               type="text"
+               size="10"
+               value="${null_rep}"
+               />
+      </td>
+    </tr>
+    <% tab_index.inc(2) %>
+  % endif
+</%def>
+
+<%def name="file_row(tab_index)">
+  <tr>
+    <td class="label">
+      <label for="datafile_id">
+        File to upload:
+      </label>
+    </td>
+    <td colspan="2">
+      <input name="datafile"
+             tabindex="${tab_index.val}"
+             id="datafile_id"
+             type="file"
+             size="75"
+             />
+    </td>
+  </tr>
+  <% tab_index.inc() %>
+</%def>
+
 <%def name="upload_form(extra_rows=[], upload_nulls=True)">
   <form action="" enctype="multipart/form-data" method="post">
     <div>
     % endif
     </div>
 
+    <% tab_index = TabIndex() %>
     <table>
-      <tr>
-        <td class="label">
-          <label for="db_id">Database:</label>
-        </td>
-        <td>
-          <input name="db"
-                 tabindex="1"
-                 id="db_id"
-                 type="text"
-                 size="30"
-                 value="${db}"
-                 />
-        </td>
-      </tr>
-      ${self.auth_parts.user_row(2, havecreds, user)}
-      <% tab_index = 3 %>
-      ${self.auth_parts.password_row(tab_index, havecreds)}
+      ${database_row(tab_index)}
+      ${self.auth_parts.user_row(tab_index.val + 1, havecreds, user)}
+      ${self.auth_parts.password_row(tab_index.val + 2, havecreds)}
+      <% tab_index.inc(2) %>
 
       % for row in extra_rows:
         <%
-          tab_index += 1
+          tab_index.inc()
         %>
-        ${row(tab_index) | n}
+        ${row(tab_index.val) | n}
       % endfor
 
-      <tr>
-        <td class="label">Uploaded Data Format:</td>
-        <td>
-          <input name="upload_fmt"
-                 id="upload_fmt_csv_id"
-                 tabindex="${tab_index + 1}"
-                 type="radio"
-                 value="${csv_value}"
-                 ${csv_checked | n}
-                 />
-          <label class="label" for="upload_fmt_csv_id">CSV</label>
-          <br />
-          <input name="upload_fmt"
-                 id="upload_fmt_tab_id"
-                 tabindex="${tab_index + 2}"
-                 type="radio"
-                 value="${tab_value}"
-                 ${tab_checked | n}
-                 />
-          <label class="label" for="upload_fmt_tab_id">Tab delimited</label>
-        </td>
-      </tr>
-      % if upload_nulls:
-        <tr>
-          <td class="label">
-            <label for="upload_null_id">Upload NULL Values: </label>
-          </td>
-          <td>
-            <input name="upload_null"
-                   id="upload_null_id"
-                   tabindex="${tab_index + 3}"
-                   type="checkbox"
-                   ${upload_null | n}
-                   />
-          </td>
-        </tr>
-        <tr>
-          <td class="label">
-            <label for="null_rep_id">NULL Representation: </label>
-          </td>
-          <td>
-            <input name="null_rep"
-                   id="null_rep_id"
-                   tabindex="${tab_index + 4}"
-                   type="text"
-                   size="10"
-                   value="${null_rep}"
-                   />
-          </td>
-        </tr>
-      % endif
-      <tr>
-        <td class="label">
-          <label for="datafile_id">
-            File to upload:
-          </label>
-        </td>
-        <td colspan="2">
-          <input name="datafile"
-                 tabindex="${tab_index + 5}"
-                 id="datafile_id"
-                 type="file"
-                 size="75"
-                 />
-        </td>
-      </tr>
+      ${format_row(tab_index)}
+      ${nulls_row(tab_index)}
+      ${file_row(tab_index)}
     </table>
 
     <p>
-      <input value="Upload" tabindex="${tab_index + 6}" type="submit" />
+      <input value="Upload" tabindex="${tab_index.val}" type="submit" />
     </p>
   </form>
 </%def>