diff --git a/vasl_templates/main.py b/vasl_templates/main.py index 671b560..cf31de4 100755 --- a/vasl_templates/main.py +++ b/vasl_templates/main.py @@ -15,7 +15,7 @@ import click from vasl_templates.main_window import MainWindow from vasl_templates.webapp import app as webapp -from vasl_templates.webapp import generate, load_debug_config +from vasl_templates.webapp import snippets, load_debug_config # --------------------------------------------------------------------- @@ -53,7 +53,7 @@ def main( template_pack, debug ): if not rc: click.echo( "ERROR: The template pack must be a ZIP file or a directory containing the template files." ) return 1 - generate.autoload_template_pack = template_pack + snippets.autoload_template_pack = template_pack # install the debug config file if debug: diff --git a/vasl_templates/webapp/__init__.py b/vasl_templates/webapp/__init__.py index bdb6a61..d881a95 100644 --- a/vasl_templates/webapp/__init__.py +++ b/vasl_templates/webapp/__init__.py @@ -41,7 +41,7 @@ if os.path.isfile( _fname ): # load the application import vasl_templates.webapp.main #pylint: disable=cyclic-import -import vasl_templates.webapp.generate #pylint: disable=cyclic-import +import vasl_templates.webapp.snippets #pylint: disable=cyclic-import # initialize the application logger = logging.getLogger( "startup" ) diff --git a/vasl_templates/webapp/data/nationalities.json b/vasl_templates/webapp/data/nationalities.json index 281091d..2984867 100644 --- a/vasl_templates/webapp/data/nationalities.json +++ b/vasl_templates/webapp/data/nationalities.json @@ -1,35 +1,43 @@ -[ +{ -{ "display_name": "German", +"german": { + "display_name": "German", "ob_colors": [ "OBCOL:german", "OBCOL2:german" ] }, -{ "display_name": "Russian", +"russian": { + "display_name": "Russian", "ob_colors": [ "OBCOL:russian", "OBCOL2:russian" ] }, -{ "display_name": "American", +"american": { + "display_name": "American", "ob_colors": [ "OBCOL:american", "OBCOL2:american" ] }, -{ "display_name": "British", +"british": { + "display_name": "British", "ob_colors": [ "OBCOL:british", "OBCOL2:british" ] }, -{ "display_name": "French", +"french": { + "display_name": "French", "ob_colors": [ "OBCOL:french", "OBCOL2:french" ] }, -{ "display_name": "Italian", +"italian": { + "display_name": "Italian", "ob_colors": [ "OBCOL:italian", "OBCOL2:italian" ] }, -{ "display_name": "Finnish", +"finnish": { + "display_name": "Finnish", "ob_colors": [ "OBCOL:finns", "OBCOL2:finns" ] }, -{ "display_name": "Japanese", +"japanese": { + "display_name": "Japanese", "ob_colors": [ "OBCOL:japanese", "OBCOL2:japanese" ] } -] +} diff --git a/vasl_templates/webapp/generate.py b/vasl_templates/webapp/snippets.py similarity index 92% rename from vasl_templates/webapp/generate.py rename to vasl_templates/webapp/snippets.py index 7102cd7..1205d19 100644 --- a/vasl_templates/webapp/generate.py +++ b/vasl_templates/webapp/snippets.py @@ -88,9 +88,4 @@ def get_nationalities(): with open(fname,"r") as fp: nationalities = json.load( fp ) - # auto-generate ID's for those entries that don't already have one - for nat in nationalities: - if "id" not in nat: - nat["id"] = nat["display_name"].lower() - - return jsonify( { n["id"]: n for n in nationalities } ) + return jsonify( nationalities ) diff --git a/vasl_templates/webapp/static/main.js b/vasl_templates/webapp/static/main.js index d61774c..8f9e684 100644 --- a/vasl_templates/webapp/static/main.js +++ b/vasl_templates/webapp/static/main.js @@ -1,4 +1,5 @@ var gNationalities = {} ; +var gDefaultNationalities = {} ; var gDefaultTemplates = {} ; var gUserDefinedTemplates = {} ; @@ -106,11 +107,8 @@ $(document).ready( function () { // load the nationalities $.getJSON( gGetNationalitiesUrl, function(data) { gNationalities = data ; - var buf = [] ; - for ( var id in gNationalities ) - buf.push( "" ) ; - $("select[name='PLAYER_1']").html( buf ) ; - $("select[name='PLAYER_2']").html( buf ) ; + gDefaultNationalities = $.extend( true, {}, data ) ; + load_nationalities() ; on_new_scenario( false ) ; } ).fail( function( xhr, status, errorMsg ) { showErrorMsg( "Can't get the nationalities:
" + escapeHTML(errorMsg) + "
" ) ; @@ -213,6 +211,26 @@ $(document).ready( function () { // -------------------------------------------------------------------- +function load_nationalities() +{ + // update the player droplists + var curSel1 = $("select[name='PLAYER_1']").val() ; + var curSel2 = $("select[name='PLAYER_2']").val() ; + var buf = [] ; + for ( var id in gNationalities ) + buf.push( "" ) ; + $("select[name='PLAYER_1']").html( buf ).val( curSel1 ) ; + $("select[name='PLAYER_2']").html( buf ).val( curSel2 ) ; + + // update the OB tabs + if ( curSel1 ) + on_player_change( $("select[name='PLAYER_1']") ) ; + if ( curSel2 ) + on_player_change( $("select[name='PLAYER_2']") ) ; +} + +// -------------------------------------------------------------------- + function on_player_change( $select ) { // figure out which player was changed diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js index 695ad89..f84daac 100644 --- a/vasl_templates/webapp/static/snippets.js +++ b/vasl_templates/webapp/static/snippets.js @@ -407,10 +407,20 @@ function do_load_template_pack( fname, data ) var invalid_filename_extns = [] ; var unknown_template_ids = [] ; var new_templates = {} ; + var nationalities = null ; // initialize function on_new_template( fname, data ) { // make sure the filename is valid + if ( fname.toLowerCase() === "nationalities.json" ) { + try { + nationalities = JSON.parse( data ) ; + } catch( ex ) { + showWarningMsg( "Can't parse the nationalities JSON data:
" + escapeHTML(ex) + "
" ) ; + return ; + } + return ; + } if ( fname.substring(fname.length-3) != ".j2" ) { invalid_filename_extns.push( fname ) ; return ; @@ -466,6 +476,11 @@ function do_load_template_pack( fname, data ) for ( tid in new_templates ){ gUserDefinedTemplates[tid] = new_templates[tid] ; } + // install any user-defined nationality data + if ( nationalities !== null ) { + gNationalities = $.extend( true, {}, gDefaultNationalities, nationalities ) ; + load_nationalities() ; + } showInfoMsg( success_msg ) ; } diff --git a/vasl_templates/webapp/tests/fixtures/template-packs/with-nationality-data/NATIONALITIES.JSON b/vasl_templates/webapp/tests/fixtures/template-packs/with-nationality-data/NATIONALITIES.JSON new file mode 100644 index 0000000..74ccbe3 --- /dev/null +++ b/vasl_templates/webapp/tests/fixtures/template-packs/with-nationality-data/NATIONALITIES.JSON @@ -0,0 +1,12 @@ +{ + +"british": { + "display_name": "Poms!" +}, + +"korean": { + "display_name": "Korean", + "ob_colors": [ 111, 222 ] +} + +} diff --git a/vasl_templates/webapp/tests/test_generate.py b/vasl_templates/webapp/tests/test_snippets.py similarity index 100% rename from vasl_templates/webapp/tests/test_generate.py rename to vasl_templates/webapp/tests/test_snippets.py diff --git a/vasl_templates/webapp/tests/test_template_packs.py b/vasl_templates/webapp/tests/test_template_packs.py index 7a12bd6..3ed1e9d 100644 --- a/vasl_templates/webapp/tests/test_template_packs.py +++ b/vasl_templates/webapp/tests/test_template_packs.py @@ -5,6 +5,8 @@ import zipfile import tempfile import base64 +from selenium.webdriver.support.ui import Select + from vasl_templates.webapp.tests.utils import select_menu_option, get_clipboard from vasl_templates.webapp.tests.utils import get_stored_msg, set_stored_msg, dismiss_notifications, find_child from vasl_templates.webapp.tests.utils import for_each_template @@ -91,8 +93,8 @@ def test_autoload_template_pack( webapp, webdriver ): # configure the autoload template pack dname = os.path.join( os.path.split(__file__)[0], "fixtures/template-packs/autoload/" ) - from vasl_templates.webapp import generate - generate.autoload_template_pack = dname + from vasl_templates.webapp import snippets + snippets.autoload_template_pack = dname # initialize webdriver.get( webapp.url_for( "main" ) ) @@ -104,6 +106,42 @@ def test_autoload_template_pack( webapp, webdriver ): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +def test_nationality_data( webapp, webdriver ): + """Test a template pack with nationality data.""" + + # initialize + webdriver.get( webapp.url_for( "main", store_msgs=1, template_pack_persistence=1 ) ) + + # select the British as player 1 + sel = Select( + find_child( "select[name='PLAYER_1']" ) + ) + sel.select_by_value( "british" ) + elem = find_child( "a[href='#tabs-ob1']" ) + assert elem.text.strip() == "British OB" + sel = Select( find_child( "select[name='PLAYER_1']" ) ) + assert sel.first_selected_option.text == "British" + players = [ o.text for o in sel.options ] + + # upload a template pack that contains nationality data + zip_data = _make_zip_from_files( "with-nationality-data" ) + _upload_template_pack( zip_data ) + assert get_stored_msg("_last-error_") is None + + # check that the UI was updated correctly + elem = find_child( "a[href='#tabs-ob1']" ) + assert elem.text.strip() == "Poms! OB" + elem = find_child( "select[name='PLAYER_1']" ) + assert Select(elem).first_selected_option.text == "Poms!" + + # check that there is a new Korean player + players2 = [ o.text for o in sel.options ] + players2.remove( "Korean" ) + players2 = [ "British" if o == "Poms!" else o for o in players2 ] + assert players2 == players + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + def _check_snippets( func ): """Check that snippets are being generated as expected."""