From 575e217c68c00dfda9f75fe6573d322494f6204f Mon Sep 17 00:00:00 2001 From: Taka Date: Wed, 6 May 2020 05:55:41 +0000 Subject: [PATCH] Pass some scenario parameters through to extras templates for their forms. Updateed the "count remaining" extras template to reflect the scenario year. --- .../extras/count-remaining.j2 | 28 +++++--- vasl_templates/webapp/static/extras.js | 23 ++++++- .../webapp/tests/test_extras_templates.py | 68 ++++++++++++++++++- 3 files changed, 107 insertions(+), 12 deletions(-) diff --git a/vasl_templates/webapp/data/default-template-pack/extras/count-remaining.j2 b/vasl_templates/webapp/data/default-template-pack/extras/count-remaining.j2 index 108140d..4dff46b 100644 --- a/vasl_templates/webapp/data/default-template-pack/extras/count-remaining.j2 +++ b/vasl_templates/webapp/data/default-template-pack/extras/count-remaining.j2 @@ -4,25 +4,33 @@ + --> {# NOTE: We specify the font size in pixels, rather than as a percentage, since this label should be added to a counter. #} diff --git a/vasl_templates/webapp/static/extras.js b/vasl_templates/webapp/static/extras.js index 7c5150b..e4dc942 100644 --- a/vasl_templates/webapp/static/extras.js +++ b/vasl_templates/webapp/static/extras.js @@ -111,7 +111,28 @@ function _show_extra_template( template_id ) buf.push( "" ) ; } buf.push( "" ) ; - var $form = $( buf.join("") ) ; + buf = buf.join( "" ) ; + + // pass some basic parameters through to the form + // NOTE: We should really re-generate the form every time it gets focus (since the scenario parameters + // might have changed) but this would be horribly expensive (not to mention mostly un-necessary). + // The user can always click on the navbar to re-generate it. + try { + func = jinja.compile( buf ).render ; + } catch( ex ) { + showErrorMsg( "Can't compile template:
" + escapeHTML(ex) + "
" ) ; + return ; + } + var params = unload_snippet_params( true, null ) ; + try { + buf = func( params, { autoEscape: false } ) ; + } catch( ex ) { + showErrorMsg( "Can't process template: " + template_id + "
" + escapeHTML(ex) + "
" ) ; + return ; + } + + // generate the form + var $form = $( buf ) ; $form.find( "select" ).select2( { minimumResultsForSearch: -1 } ).on( "select2:open", function() { diff --git a/vasl_templates/webapp/tests/test_extras_templates.py b/vasl_templates/webapp/tests/test_extras_templates.py index 6305ee1..0f184de 100644 --- a/vasl_templates/webapp/tests/test_extras_templates.py +++ b/vasl_templates/webapp/tests/test_extras_templates.py @@ -3,7 +3,8 @@ from selenium.webdriver.support.ui import Select from selenium.webdriver.common.keys import Keys -from vasl_templates.webapp.tests.utils import init_webapp, select_tab, get_droplist_vals, select_droplist_val, \ +from vasl_templates.webapp.tests.utils import init_webapp, select_tab, \ + set_template_params, get_droplist_vals, select_droplist_val, \ find_child, find_children, wait_for, wait_for_clipboard from vasl_templates.webapp.tests.test_template_packs import make_zip_from_files, upload_template_pack_zip @@ -174,6 +175,71 @@ def test_edit_extras_template( webapp, webdriver ): # --------------------------------------------------------------------- +def test_count_remaining_hilites( webapp, webdriver ): + """Test highlighting in the "count remaining" extras template.""" + + # initialize + init_webapp( webapp, webdriver, + reset = lambda ct: ct.set_data_dir( dtype="real" ) + ) + + def do_test( year, expected ): #pylint: disable=missing-docstring + + # set the specified year + set_template_params( { + "SCENARIO_DATE": "01/01/{}".format( year ) if year else "" + } ) + + # select the "count remaining" template and check what's been highlighted + select_tab( "extras" ) + _select_extras_template( webdriver, "extras/count-remaining" ) + for count_type in expected: + table = find_child( "table.{}".format( count_type ) ) + cells = [] + for row in find_children( "tr", table ): + row = list( find_children( "td", row ) ) + assert len(row) == 2 + bgd_col = row[1].value_of_css_property( "background-color" ) + assert bgd_col.startswith( ( "rgb(", "rgba(" ) ) + cells.append( bgd_col != "rgba(0, 0, 0, 0)" ) + assert cells == expected[count_type] + + # do the tests + do_test( None, { + "pf": [ False, False, False ], + "thh": [ False, False, False, False ] + } ) + do_test( 1940, { + "pf": [ True, False, False ], + "thh": [ True, False, False, False ] + } ) + do_test( 1941, { + "pf": [ True, False, False ], + "thh": [ True, False, False, False ] + } ) + do_test( 1942, { + "pf": [ True, False, False ], + "thh": [ True, False, False, False ] + } ) + do_test( 1943, { + "pf": [ True, False, False ], + "thh": [ False, True, False, False ] + } ) + do_test( 1944, { + "pf": [ False, True, False ], + "thh": [ False, False, True, False ] + } ) + do_test( 1945, { + "pf": [ False, False, True ], + "thh": [ False, False, False, True ] + } ) + do_test( 1946, { + "pf": [ False, False, False ], + "thh": [ False, False, False, False ] + } ) + +# --------------------------------------------------------------------- + def _get_extras_template_index(): """Get the list of extras templates from the sidebar.""" def get_child_text( child_class, elem ): #pylint: disable=missing-docstring