Split auth_base to create errors_base
authorKarl O. Pinc <kop@karlpinc.com>
Thu, 22 Apr 2021 17:19:33 +0000 (12:19 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Thu, 22 Apr 2021 17:19:33 +0000 (12:19 -0500)
src/pgwui_common/templates/auth_base.mak
src/pgwui_common/templates/errors_base.mak [new file with mode: 0644]
src/pgwui_common/view.py
tests/test_view.py

index 51d3065e4c71160a08de41596ab0c5fdc093b70b..bb7f0be2f4aa07e888cb51156000806568b6ce5e 100644 (file)
 
   This template uses the following variables in it's context:
     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.
-    lines       (Optional) Number of lines in an uploaded file.
 
     For use with upload_form:
     csrf_token  Token for detecting CSRF.
 <%!
     from pgwui_common.path import asset_abspath
 
-    base_mak = asset_abspath('pgwui_common:templates/base.mak')
+    errors_base_mak = asset_abspath('pgwui_common:templates/errors_base.mak')
 %>
 
-<%inherit file="${base_mak}" />
-
-<%def name="navbar_content()">
-  ${parent.navbar_content()}
-  % if 'pgwui_logout' in pgwui['urls']:
-    | <a href="${pgwui['urls']['pgwui_logout']}">Logout</a>
-  % endif
-</%def>
-
-<%block name="summary_info">
-  % if filename != '':
-      <p>File supplied: <em class="filename">${filename}</em></p>
-  % endif
-</%block>
-
-<%doc> Error reporting </%doc>
-
-<%def name="error_file_summary(e_cnt, lines, filepath, relation)">
-  % if e_cnt:
-      <p><em class="error">${e_cnt} errors</em>
-      % if lines == UNDEFINED or lines == 0:
-          found.
-      % else:
-          found
-        % if relation is None:
-          in
-        % else:
-          while inserting data into ${relation} from
-        % endif
-        % if filepath is None:
-          a file of
-        % else:
-          file ${filepath}, containing
-        % endif
-        ${lines} lines.  (Including column headings.)
-      % endif
-      </p>
-  % endif
-</%def>
-
-<%def name="error_items(errors, sepclass='errorsep', caution=False)">
-  % for error in errors:
-
-      ## Get error attributes
-      <%
-      if hasattr(error, 'lineno'):
-          lineno = error.lineno
-      else:
-          lineno = False
-
-      if hasattr(error, 'descr') and error.descr:
-          # We really do want to do this formatting here instead of in
-          # the error handler.
-          if error.e[-1] == ':':
-              descr = ' ' + error.descr
-          else:
-              descr = ': ' + error.descr
-      else:
-          descr = ''
-      %>
-
-      % if lineno:
-          % if loop.first:
-              <hr class="errorsep"></hr>
-          % endif
-      % endif
-
-      <p>
-
-      % if lineno:
-          % if caution or not loop.first:
-              <span class="notfirstcaution">CAUTION -- This error may not be
-              real; prior uploaded row(s) were rejected:</span>
-             <br />
-          % endif
-
-          Line ${lineno}:
-      % endif
-
-      <em class="error">${error.e}</em>${descr}
-      </p>
-
-      ## Messages delivered here are all generated.  Allow markup.
-      % if hasattr(error, 'detail'):
-          <p>${error.detail | n}</p>
-      % endif
-      % if hasattr(error, 'data') and error.data:
-          <p>Line is:</p>
-          <blockquote><p>${error.data}</p></blockquote>
-      % endif       
-
-      % if not loop.last and lineno:
-          <hr class="${sepclass}"></hr>
-      % endif
-
-  % endfor
-</%def>
-
-<%block name="error_report">
-  <% self.error_file_summary(e_cnt, lines, None, None) %>
-  <% self.error_items(errors) %>
-</%block>
+<%inherit file="${errors_base_mak}" />
 
 <%def name="upload_form(extra_rows=[], upload_nulls=True)">
   <form action="" enctype="multipart/form-data" method="post">
diff --git a/src/pgwui_common/templates/errors_base.mak b/src/pgwui_common/templates/errors_base.mak
new file mode 100644 (file)
index 0000000..2a6c4ee
--- /dev/null
@@ -0,0 +1,144 @@
+<?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 a form that reports multiple errors
+  and has "logout" in the navbar
+  Karl O. Pinc <kop@karlpinc.com>
+
+  This template uses the following variables in it's context:
+    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.
+</%doc>
+
+<%!
+    from pgwui_common.path import asset_abspath
+
+    base_mak = asset_abspath('pgwui_common:templates/base.mak')
+%>
+
+<%inherit file="${base_mak}" />
+
+<%def name="navbar_content()">
+  ${parent.navbar_content()}
+  % if 'pgwui_logout' in pgwui['urls']:
+    | <a href="${pgwui['urls']['pgwui_logout']}">Logout</a>
+  % endif
+</%def>
+
+<%block name="summary_info">
+  % if filename != '':
+      <p>File supplied: <em class="filename">${filename}</em></p>
+  % endif
+</%block>
+
+<%doc> Error reporting </%doc>
+
+<%def name="error_file_summary(e_cnt, lines, filepath, relation)">
+  % if e_cnt:
+      <p><em class="error">${e_cnt} errors</em>
+      % if lines == UNDEFINED or lines == 0:
+          found.
+      % else:
+          found
+        % if relation is None:
+          in
+        % else:
+          while inserting data into ${relation} from
+        % endif
+        % if filepath is None:
+          a file of
+        % else:
+          file ${filepath}, containing
+        % endif
+        ${lines} lines.  (Including column headings.)
+      % endif
+      </p>
+  % endif
+</%def>
+
+<%def name="error_items(errors, sepclass='errorsep', caution=False)">
+  % for error in errors:
+
+      ## Get error attributes
+      <%
+      if hasattr(error, 'lineno'):
+          lineno = error.lineno
+      else:
+          lineno = False
+
+      if hasattr(error, 'descr') and error.descr:
+          # We really do want to do this formatting here instead of in
+          # the error handler.
+          if error.e[-1] == ':':
+              descr = ' ' + error.descr
+          else:
+              descr = ': ' + error.descr
+      else:
+          descr = ''
+      %>
+
+      % if lineno:
+          % if loop.first:
+              <hr class="errorsep"></hr>
+          % endif
+      % endif
+
+      <p>
+
+      % if lineno:
+          % if caution or not loop.first:
+              <span class="notfirstcaution">CAUTION -- This error may not be
+              real; prior uploaded row(s) were rejected:</span>
+             <br />
+          % endif
+
+          Line ${lineno}:
+      % endif
+
+      <em class="error">${error.e}</em>${descr}
+      </p>
+
+      ## Messages delivered here are all generated.  Allow markup.
+      % if hasattr(error, 'detail'):
+          <p>${error.detail | n}</p>
+      % endif
+      % if hasattr(error, 'data') and error.data:
+          <p>Line is:</p>
+          <blockquote><p>${error.data}</p></blockquote>
+      % endif       
+
+      % if not loop.last and lineno:
+          <hr class="${sepclass}"></hr>
+      % endif
+
+  % endfor
+</%def>
+
+<%block name="error_report">
+  <% self.error_file_summary(e_cnt, lines, None, None) %>
+  <% self.error_items(errors) %>
+</%block>
index d74560a608b69cd23efcfcd555fbef21d739f98d..3032ff482bea80509f4158d78c79df38d0c8d077 100644 (file)
@@ -47,11 +47,21 @@ def base_view(wrapped):
     return wrapper
 
 
+def errors_base_view(wrapped):
+    '''Decorator for any view which includes errors_base.mk.
+    '''
+    def wrapper(request):
+        '''Add variables needed by errors_base.mk to the response.
+        '''
+        return base_view(wrapped)(request)
+    return wrapper
+
+
 def auth_base_view(wrapped):
     '''Decorator for any view which includes auth_base.mk.
     '''
     def wrapper(request):
         '''Add variables needed by auth_base.mk to the response.
         '''
-        return base_view(wrapped)(request)
+        return errors_base_view(wrapped)(request)
     return wrapper
index 4677f44870dabc90ac7b8e0d2085027eb1acce4f..99367853c11f145e1c821ec0ec36f96653486be7 100644 (file)
@@ -111,13 +111,28 @@ def test_base_view_default(mock_merge_urls):
 mock_base_view = testing.make_mock_fixture(view, 'base_view')
 
 
-# auth_base_view()
+# errors_base_view()
 
-def test_auth_base_view(mock_base_view):
+def test_errors_base_view(mock_base_view):
     '''Wrapper calls base_view()
     '''
-    wrapper = view.auth_base_view(mock_view)
+    wrapper = view.errors_base_view(mock_view)
     request = get_current_request()
     wrapper(request)
 
     mock_base_view.assert_called_once()
+
+
+mock_errors_base_view = testing.make_mock_fixture(view, 'errors_base_view')
+
+
+# auth_base_view()
+
+def test_auth_base_view(mock_errors_base_view):
+    '''Wrapper calls errors_base_view()
+    '''
+    wrapper = view.auth_base_view(mock_view)
+    request = get_current_request()
+    wrapper(request)
+
+    mock_errors_base_view.assert_called_once()