Abstract out common auth form elements
authorKarl O. Pinc <kop@karlpinc.com>
Fri, 30 Apr 2021 03:57:06 +0000 (22:57 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sun, 9 May 2021 16:32:27 +0000 (11:32 -0500)
src/pgwui_common/templates/auth_base.mak
src/pgwui_common/templates/auth_parts.mak [new file with mode: 0644]

index bb7f0be2f4aa07e888cb51156000806568b6ce5e..ca76372163c9642fe33a89f24bf4b8fa1e1eabcd 100644 (file)
@@ -24,6 +24,7 @@
   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".
     from pgwui_common.path import asset_abspath
 
     errors_base_mak = asset_abspath('pgwui_common:templates/errors_base.mak')
+    auth_parts_mak = asset_abspath('pgwui_common:templates/auth_parts.mak')
 %>
 
 <%inherit file="${errors_base_mak}" />
+<%namespace file="${auth_parts_mak}"
+            name="auth_parts"
+            inheritable="True" />
 
 <%def name="upload_form(extra_rows=[], upload_nulls=True)">
   <form action="" enctype="multipart/form-data" method="post">
     <div>
-    <input name="action"
-           type="hidden"
-           value="u"
-           />
-    <input type="hidden"
-           name="csrf_token"
-           value="${csrf_token}"
-           />
+    ${self.auth_parts.hidden_vars(csrf_token)}
     % if (db_changed or session_expired) and last_key:
       <input type="hidden"
              name="last_key"
                  />
         </td>
       </tr>
-      <tr>
-        <td class="label">
-          <label for="user_id">Username:</label>
-        </td>
-        <td>
-          % if havecreds:
-            <span id="user_id">${user}</span>
-          % else:
-            <input name="user"
-                   tabindex="2"
-                   id="user_id"
-                   type="text"
-                   size="30"
-                   value=""
-                   />
-          % endif
-        </td>
-      </tr>
-      % if not havecreds:
-        <tr>
-          <td class="label">
-            <label for="password_id">Password:</label>
-          </td>
-          <td>
-            <input name="password"
-                   tabindex="3"
-                   id="password_id"
-                   type="password"
-                   size="30"
-                   />
-          </td>
-        </tr>
-      % endif
+      ${self.auth_parts.user_row(2, havecreds, user)}
+      <% tab_index = 3 %>
+      ${self.auth_parts.password_row(tab_index, havecreds)}
 
-      <%
-        tab_index = 3
-      %>
       % for row in extra_rows:
         <%
           tab_index += 1
diff --git a/src/pgwui_common/templates/auth_parts.mak b/src/pgwui_common/templates/auth_parts.mak
new file mode 100644 (file)
index 0000000..12d77ab
--- /dev/null
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<%doc>
+  Copyright (C) 2015, 2020, 2021 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
+    user
+</%doc>
+
+<%def name="hidden_vars(csrf_token)">
+    <input name="action"
+           type="hidden"
+           value="u"
+           />
+    <input type="hidden"
+           name="csrf_token"
+           value="${csrf_token}"
+           />
+</%def>
+
+<%def name="user_row(tabindex, havecreds, user)">
+      <tr>
+        <td class="label">
+          <label for="user_id">Username:</label>
+        </td>
+        <td>
+          % if havecreds:
+            <span id="user_id">${user}</span>
+          % else:
+            <input name="user"
+                   tabindex="${tabindex}"
+                   id="user_id"
+                   type="text"
+                   size="30"
+                   value=""
+                   />
+          % endif
+        </td>
+      </tr>
+</%def>
+
+<%def name="password_row(tabindex, havecreds)">
+      % if not havecreds:
+        <tr>
+          <td class="label">
+            <label for="password_id">Password:</label>
+          </td>
+          <td>
+            <input name="password"
+                   tabindex="${tabindex}"
+                   id="password_id"
+                   type="password"
+                   size="30"
+                   />
+          </td>
+        </tr>
+      % endif
+</%def>