diff --git a/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 b/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 index 6fd5fde..e5877b1 100644 --- a/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 +++ b/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 @@ -19,7 +19,7 @@ sup { font-size: 75% ; } padding: 2px 5px ; font-weight: bold ; "> - {{PLAYER_NAME}} Ordnance + {%if PLAYER_FLAG%} {%endif%}{{PLAYER_NAME}} Ordnance {%for ord in OB_ORDNANCE%} diff --git a/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 b/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 index 0d229e1..4041caf 100644 --- a/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 +++ b/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 @@ -10,7 +10,7 @@ font-weight: bold ; {%if OB_SETUP_WIDTH%} width: {{OB_SETUP_WIDTH}} ; {%endif%} "> - {{OB_SETUP}} + {%if PLAYER_FLAG%} {%endif%}{{OB_SETUP}} diff --git a/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 b/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 index f7547ed..380d49c 100644 --- a/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 +++ b/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 @@ -19,7 +19,7 @@ sup { font-size: 75% ; } padding: 2px 5px ; font-weight: bold ; "> - {{PLAYER_NAME}} Vehicles + {%if PLAYER_FLAG%} {%endif%}{{PLAYER_NAME}} Vehicles {%for veh in OB_VEHICLES%} diff --git a/vasl_templates/webapp/snippets.py b/vasl_templates/webapp/snippets.py index 8cd5a17..a357b9e 100644 --- a/vasl_templates/webapp/snippets.py +++ b/vasl_templates/webapp/snippets.py @@ -2,6 +2,7 @@ import os import json +import re import zipfile from flask import jsonify, abort @@ -85,3 +86,12 @@ def _do_get_template_pack( dname ): if words[1] == ".j2": templates[words[0]] = fp.read() return nationalities, templates + +# --------------------------------------------------------------------- + +@app.route( "/flags/" ) +def get_flag( nat ): + """Get a flag image.""" + if not re.search( "^[-a-z]+$", nat ): + abort( 404 ) + return app.send_static_file( "images/flags/{}.png".format( nat ) ) diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js index cf0b9de..6cdc536 100644 --- a/vasl_templates/webapp/static/snippets.js +++ b/vasl_templates/webapp/static/snippets.js @@ -27,6 +27,10 @@ function generate_snippet( $btn, extra_params ) var params = unload_snippet_params( true, template_id ) ; // set player-specific parameters + function make_player_flag_url( player_no ) { + var player_nat = get_player_nat( player_no ) ; + return APP_URL_BASE + "/flags/" + player_nat ; + } var curr_tab = $("#tabs .ui-tabs-active a").attr( "href" ) ; var colors ; if ( curr_tab === "#tabs-ob1" ) { @@ -34,11 +38,15 @@ function generate_snippet( $btn, extra_params ) colors = get_player_colors( 1 ) ; params.OB_COLOR = colors[0] ; params.OB_COLOR_2 = colors[2] ; + if ( gUserSettings["include-flags-in-snippets"] ) + params.PLAYER_FLAG = make_player_flag_url( 1 ) ; } else if ( curr_tab === "#tabs-ob2" ) { params.PLAYER_NAME = get_nationality_display_name( params.PLAYER_2 ) ; colors = get_player_colors( 2 ) ; params.OB_COLOR = colors[0] ; params.OB_COLOR_2 = colors[2] ; + if ( gUserSettings["include-flags-in-snippets"] ) + params.PLAYER_FLAG = make_player_flag_url( 2 ) ; } // set player-specific parameters diff --git a/vasl_templates/webapp/static/user_settings.js b/vasl_templates/webapp/static/user_settings.js index 9e68763..9bb5ad9 100644 --- a/vasl_templates/webapp/static/user_settings.js +++ b/vasl_templates/webapp/static/user_settings.js @@ -2,6 +2,7 @@ gUserSettings = Cookies.getJSON( "user-settings" ) || {} ; USER_SETTINGS = { "include-vasl-images-in-snippets": "checkbox", + "include-flags-in-snippets": "checkbox", } ; // -------------------------------------------------------------------- @@ -36,9 +37,10 @@ function user_settings() function update_ui() { // update the UI var $dlg = $( ".ui-dialog.user-settings" ) ; - var is_checked = $dlg.find( "input[name='include-vasl-images-in-snippets']" ).prop( "checked" ) ; + var is_server = $dlg.find( "input[name='include-vasl-images-in-snippets']" ).prop( "checked" ) || + $dlg.find( "input[name='include-flags-in-snippets']" ).prop( "checked" ) ; $dlg.find( ".include-vasl-images-in-snippets-hint" ).css( - "color", is_checked ? "#444" : "#aaa" + "color", is_server ? "#444" : "#aaa" ) ; } @@ -53,6 +55,7 @@ function user_settings() create: function() { init_dialog( $(this), "OK", false ) ; $(this).find( "input[name='include-vasl-images-in-snippets']" ).change( update_ui ) ; + $(this).find( "input[name='include-flags-in-snippets']" ).change( update_ui ) ; }, open: function() { // load the current user settings diff --git a/vasl_templates/webapp/templates/user-settings-dialog.html b/vasl_templates/webapp/templates/user-settings-dialog.html index f6decaa..8ae24fd 100644 --- a/vasl_templates/webapp/templates/user-settings-dialog.html +++ b/vasl_templates/webapp/templates/user-settings-dialog.html @@ -1,4 +1,5 @@ diff --git a/vasl_templates/webapp/tests/test_user_settings.py b/vasl_templates/webapp/tests/test_user_settings.py index 7b9352f..a34e882 100644 --- a/vasl_templates/webapp/tests/test_user_settings.py +++ b/vasl_templates/webapp/tests/test_user_settings.py @@ -4,7 +4,8 @@ import json from vasl_templates.webapp.tests.utils import \ init_webapp, find_child, _get_clipboard, \ - wait_for, select_menu_option, click_dialog_button + wait_for, wait_for_clipboard, select_tab, select_menu_option, click_dialog_button, \ + add_simple_note from vasl_templates.webapp.tests.test_vehicles_ordnance import add_vo from vasl_templates.webapp.config.constants import DATA_DIR as REAL_DATA_DIR @@ -31,7 +32,7 @@ def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ): # make sure that it took effect snippet_btn = find_child( "button[data-id='ob_vehicles_1']" ) snippet_btn.click() - wait_for( 2, lambda: "/counter/2524/front" in _get_clipboard() ) + wait_for_clipboard( 2, "/counter/2524/front", contains=True ) # disable "show VASL images in snippets" select_menu_option( "user_settings" ) @@ -47,10 +48,64 @@ def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +def test_include_flags_in_snippets( webapp, webdriver, monkeypatch ): + """Test the user settings.""" + + # initialize + monkeypatch.setitem( webapp.config, "DATA_DIR", REAL_DATA_DIR ) + init_webapp( webapp, webdriver ) + + # prepare the scenario + select_tab( "ob1" ) + sortable = find_child( "#ob_setups-sortable_1" ) + add_simple_note( sortable, "OB setup note", None ) + + # enable "show flags in snippets" + select_menu_option( "user_settings" ) + elem = find_child( ".ui-dialog.user-settings input[name='include-flags-in-snippets']" ) + assert not elem.is_selected() + elem.click() + click_dialog_button( "OK" ) + _check_cookies( webdriver, "include-flags-in-snippets", True ) + + # make sure that it took effect + ob_setup_snippet_btn = find_child( "li img.snippet", sortable ) + ob_setup_snippet_btn.click() + wait_for_clipboard( 2, "/flags/german", contains=True ) + + # make sure it also affects vehicle/ordnance snippets + ob_vehicles_snippet_btn = find_child( "button.generate[data-id='ob_vehicles_1']" ) + ob_vehicles_snippet_btn.click() + wait_for_clipboard( 2, "/flags/german", contains=True ) + ob_ordnance_snippet_btn = find_child( "button.generate[data-id='ob_ordnance_1']" ) + ob_ordnance_snippet_btn.click() + wait_for_clipboard( 2, "/flags/german", contains=True ) + + # disable "show flags in snippets" + select_menu_option( "user_settings" ) + elem = find_child( ".ui-dialog.user-settings input[name='include-flags-in-snippets']" ) + assert elem.is_selected() + elem.click() + click_dialog_button( "OK" ) + _check_cookies( webdriver, "include-flags-in-snippets", False ) + + # make sure that it took effect + ob_setup_snippet_btn.click() + wait_for( 2, lambda: "/flags/german" not in _get_clipboard() ) + + # make sure it also affects vehicle/ordnance snippets + ob_vehicles_snippet_btn.click() + wait_for( 2, lambda: "/flags/german" not in _get_clipboard() ) + ob_ordnance_snippet_btn.click() + wait_for( 2, lambda: "/flags/german" not in _get_clipboard() ) + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + def _check_cookies( webdriver, name, expected ): """Check that a user setting was stored in the cookies correctly.""" cookies = [ c for c in webdriver.get_cookies() if c["name"] == "user-settings" ] assert len(cookies) == 1 - val = cookies[0]["value"].replace( "%22", '"' ) + val = cookies[0]["value"].replace( "%22", '"' ).replace( "%2C", "," ) + print( val ) user_settings = json.loads( val ) assert user_settings[name] == expected