From ef1d436c1340f74ffb61540d669b1ea2bc799786 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Thu, 22 Apr 2021 12:19:33 -0500 Subject: [PATCH] Split auth_base to create errors_base --- src/pgwui_common/templates/auth_base.mak | 111 +--------------- src/pgwui_common/templates/errors_base.mak | 144 +++++++++++++++++++++ src/pgwui_common/view.py | 12 +- tests/test_view.py | 21 ++- 4 files changed, 180 insertions(+), 108 deletions(-) create mode 100644 src/pgwui_common/templates/errors_base.mak diff --git a/src/pgwui_common/templates/auth_base.mak b/src/pgwui_common/templates/auth_base.mak index 51d3065..bb7f0be 100644 --- a/src/pgwui_common/templates/auth_base.mak +++ b/src/pgwui_common/templates/auth_base.mak @@ -25,8 +25,12 @@ 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. @@ -46,111 +50,10 @@ <%! 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']: - | Logout - % endif - - -<%block name="summary_info"> - % if filename != '': -

File supplied: ${filename}

- % endif - - -<%doc> Error reporting - -<%def name="error_file_summary(e_cnt, lines, filepath, relation)"> - % if e_cnt: -

${e_cnt} errors - % 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 -

- % endif - - -<%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: -
- % endif - % endif - -

- - % if lineno: - % if caution or not loop.first: - CAUTION -- This error may not be - real; prior uploaded row(s) were rejected: -
- % endif - - Line ${lineno}: - % endif - - ${error.e}${descr} -

- - ## Messages delivered here are all generated. Allow markup. - % if hasattr(error, 'detail'): -

${error.detail | n}

- % endif - % if hasattr(error, 'data') and error.data: -

Line is:

-

${error.data}

- % endif - - % if not loop.last and lineno: -
- % endif - - % endfor - - -<%block name="error_report"> - <% self.error_file_summary(e_cnt, lines, None, None) %> - <% self.error_items(errors) %> - +<%inherit file="${errors_base_mak}" /> <%def name="upload_form(extra_rows=[], upload_nulls=True)">
diff --git a/src/pgwui_common/templates/errors_base.mak b/src/pgwui_common/templates/errors_base.mak new file mode 100644 index 0000000..2a6c4ee --- /dev/null +++ b/src/pgwui_common/templates/errors_base.mak @@ -0,0 +1,144 @@ + +<%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 + . + + Base template for a form that reports multiple errors + and has "logout" in the navbar + + Karl O. Pinc + + 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. + + +<%! + 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']: + | Logout + % endif + + +<%block name="summary_info"> + % if filename != '': +

File supplied: ${filename}

+ % endif + + +<%doc> Error reporting + +<%def name="error_file_summary(e_cnt, lines, filepath, relation)"> + % if e_cnt: +

${e_cnt} errors + % 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 +

+ % endif + + +<%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: +
+ % endif + % endif + +

+ + % if lineno: + % if caution or not loop.first: + CAUTION -- This error may not be + real; prior uploaded row(s) were rejected: +
+ % endif + + Line ${lineno}: + % endif + + ${error.e}${descr} +

+ + ## Messages delivered here are all generated. Allow markup. + % if hasattr(error, 'detail'): +

${error.detail | n}

+ % endif + % if hasattr(error, 'data') and error.data: +

Line is:

+

${error.data}

+ % endif + + % if not loop.last and lineno: +
+ % endif + + % endfor + + +<%block name="error_report"> + <% self.error_file_summary(e_cnt, lines, None, None) %> + <% self.error_items(errors) %> + diff --git a/src/pgwui_common/view.py b/src/pgwui_common/view.py index d74560a..3032ff4 100644 --- a/src/pgwui_common/view.py +++ b/src/pgwui_common/view.py @@ -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 diff --git a/tests/test_view.py b/tests/test_view.py index 4677f44..9936785 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -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() -- 2.34.1