Split out single-db using forms into a separate template
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 30 Jul 2024 23:25:52 +0000 (18:25 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 30 Jul 2024 23:28:54 +0000 (18:28 -0500)
Uses "main_form".  The inheriting template always initiates execution.
Move upload fields into pgwui_upload_core.

src/pgwui_common/templates/auth_base.mak
src/pgwui_common/templates/db_base.mak [new file with mode: 0644]

index 8d4fafef38e104e1b5aaaab2350ffe4f3bc50bc0..633a1fbb9ec9e0328ed2e40969c8463938c4b423 100644 (file)
@@ -19,7 +19,7 @@
      License along with this program.  If not, see
      <http://www.gnu.org/licenses/>.
  
-  Base template for form that authenticates a user and accesses a db.
+  Base template for form that authenticates a user.
  
   Karl O. Pinc <kop@karlpinc.com>
 
        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 show_choice(pgwui, 'null'):
-    <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(upload_nulls=True)">
+<%def name="main_form(tab_index)">
   <form action="" enctype="multipart/form-data" method="post">
     <div>
     ${self.auth_parts.hidden_vars(csrf_token)}
 
     <table>
       <%
-      tab_index = TabIndex()
-      database_row(tab_index)
+      if hasattr(caller, 'database_row'):
+        caller.database_row(tab_index)
       self.auth_parts.user_row(tab_index, havecreds, user)
       self.auth_parts.password_row(tab_index, havecreds)
 
       caller.body(tab_index)
 
-      format_row(tab_index)
-      nulls_row(tab_index)
-      file_row(tab_index)
       %>
     </table>
 
diff --git a/src/pgwui_common/templates/db_base.mak b/src/pgwui_common/templates/db_base.mak
new file mode 100644 (file)
index 0000000..80b2775
--- /dev/null
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<%doc>
+  Copyright (C) 2024 The Meme Factory, Inc. http://www.karlpinc.com/
+     This file is part of PGWUI_Common.
+    
+     This program is free software: you can redistribute it and/or
+     modify it under the terms of the GNU Affero General Public License
+     as published by the Free Software Foundation, either version 3 of
+     the License, or (at your option) any later version.
+    
+     This program is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public
+     License along with this program.  If not, see
+     <http://www.gnu.org/licenses/>.
+  Base template for form that authenticates a user and accesses a db.
+  Karl O. Pinc <kop@karlpinc.com>
+
+  This template uses the following variables in it's context:
+    havecreds   Already logged in
+    errors      A list of UploadError exceptions.
+    lines       (Optional) Number of lines in uploaded file.
+                When omitted, there's no "uploaded file".
+    filename    (Optional) Name of uploaded file.
+    filepath    (Optional) Path of uploaded file.
+    relation    (Optional) Table or view uploaded into.
+    e_cnt       (Optional) Number of errors to display.
+
+    For use with upload_form:
+    csrf_token  Token for detecting CSRF.
+    report_success  Boolean. Whether to tell the user the db was changed.
+    db_changed  Boolean.  Whether the previous upload changed db content.
+                Different from report_success because "previous upload"
+                ignores session expiration.  (Unlike report_success,
+                db_changed is a form variable, not a session variable.)
+    session_expired  Boolean.  Whether the session has expired.
+    last_key     Token to detect duplicate uploads.
+    db
+    user
+    csv_value
+    tab_value
+    tab_checked
+    csv_checked
+    upload_null
+    null_rep
+</%doc>
+
+<%!
+    from pgwui_common.path import asset_abspath
+
+    auth_base_mak = asset_abspath('pgwui_common:templates/auth_base.mak')
+%>
+
+<%inherit file="${auth_base_mak}" />
+
+<%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="main_form(tab_index)">
+  <%parent:main_form tab_index="${tab_index}" args="tab_index">
+    <%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>
+
+    <% caller.body(tab_index) %>
+  </%parent:main_form>
+</%def>