Allow the date format to be changed.

master
Pacman Ghost 6 years ago
parent 1c4e333325
commit b9408d6842
  1. 6
      vasl_templates/main_window.py
  2. 2
      vasl_templates/webapp/static/main.js
  3. 26
      vasl_templates/webapp/static/user_settings.js
  4. 19
      vasl_templates/webapp/templates/user-settings-dialog.html
  5. 1
      vasl_templates/webapp/tests/test_capabilities.py
  6. 8
      vasl_templates/webapp/tests/test_template_packs.py
  7. 84
      vasl_templates/webapp/tests/test_user_settings.py

@ -204,7 +204,11 @@ class MainWindow( QWidget ):
buf.write( "{" )
for key in app_settings.allKeys():
if key.startswith( "UserSettings/" ):
buf.write( '"{}": {},'.format( key[13:], app_settings.value(key) ) )
val = app_settings.value(key)
if val in ("true","false") or val.isdigit():
buf.write( '"{}": {},'.format( key[13:], val ) )
else:
buf.write( '"{}": "{}",'.format( key[13:], val ) )
buf.write( '"_dummy_": null }' )
buf = buf.getvalue()
user_settings = {}

@ -105,7 +105,6 @@ $(document).ready( function () {
$("input[name='SCENARIO_DATE']").datepicker( {
showAnim: "slideDown",
changeMonth: true, changeYear: true,
defaultDate: "01/01/1940",
onClose: on_scenario_date_change,
} ) ;
@ -413,6 +412,7 @@ function update_page_load_status( id )
// check if the page has finished loading
if ( gPageLoadStatus.length === 0 ) {
// yup - update the UI
apply_user_settings() ;
$("#tabs").tabs({ disabled: [] }) ;
$("#loader").fadeOut( 500 ) ;
adjust_footer_vspacers() ;

@ -1,6 +1,7 @@
gUserSettings = Cookies.getJSON( "user-settings" ) || {} ;
USER_SETTINGS = {
"date-format": "droplist",
"include-vasl-images-in-snippets": "checkbox",
"include-flags-in-snippets": "checkbox",
} ;
@ -32,6 +33,8 @@ function user_settings()
var handlers = {
load_checkbox: function( $elem, val ) { $elem.prop( "checked", val?true:false ) ; },
unload_checkbox: function( $elem ) { return $elem.prop( "checked" ) ; },
load_droplist: function( $elem, val ) { if ( val ) $elem.val( val ) ; },
unload_droplist: function( $elem ) { return $elem.children(":selected").val() ; },
} ;
function update_ui() {
@ -50,7 +53,7 @@ function user_settings()
dialogClass: "user-settings",
modal: true,
width: 450,
height: 150,
height: 200,
resizable: false,
create: function() {
init_dialog( $(this), "OK", false ) ;
@ -68,6 +71,7 @@ function user_settings()
var settings = unload_settings() ;
gUserSettings = settings ;
Cookies.set( "user-settings", settings, { expires: 999 } ) ;
apply_user_settings() ;
if ( gWebChannelHandler )
gWebChannelHandler.on_user_settings_change( JSON.stringify( settings ) ) ;
$(this).dialog( "close" ) ;
@ -77,9 +81,29 @@ function user_settings()
} ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function apply_user_settings()
{
// set the date format
var date_format = gUserSettings["date-format"] || "mm/dd/yy" ;
var $scenario_date = $( "input[name='SCENARIO_DATE']" ) ;
var curr_date = $scenario_date.datepicker( "getDate" ) ;
$scenario_date.datepicker( "option", "dateFormat", date_format ) ;
$scenario_date.datepicker( "option", "defaultDate",
$.datepicker.formatDate( date_format, new Date(1940,0,1) )
) ;
if ( curr_date ) {
$scenario_date.val(
$.datepicker.formatDate( date_format, curr_date )
).trigger( "change" ) ;
}
}
// --------------------------------------------------------------------
function install_user_settings( user_settings ) // nb: this is called by the PyQT desktop application
{
gUserSettings = JSON.parse( user_settings ) ;
apply_user_settings() ;
}

@ -1,5 +1,18 @@
<div id="user-settings" style="display:none;">
<input type="checkbox" name="include-vasl-images-in-snippets">&nbsp;Include VASL images in snippets <br>
<input type="checkbox" name="include-flags-in-snippets">&nbsp;Include flags in snippets
<div class="note include-vasl-images-in-snippets-hint" style="margin-left:20px;">This program must be running before you load the scenario in VASL.</div>
<label for="">Date format:</label>
<select name="date-format">
<option value="mm/dd/yy">MM/DD/YYYY</option>
<option value="dd/mm/yy">DD/MM/YYYY</option>
<option value="yy-mm-dd">YYYY-MM-DD</option>
</select>
<br><br>
<div>
<input type="checkbox" name="include-vasl-images-in-snippets">&nbsp;Include VASL images in snippets <br>
<input type="checkbox" name="include-flags-in-snippets">&nbsp;Include flags in snippets
<div class="note include-vasl-images-in-snippets-hint" style="margin-left:20px;">This program must be running before you load the scenario in VASL.</div>
</div>
</div>

@ -305,7 +305,6 @@ def test_month_capabilities( webapp, webdriver ):
assert "H6" not in val
assert _get_capabilities( webdriver, webapp, *ordnance, "ETO", "07/1944" ) == val
val = _get_capabilities( webdriver, webapp, *ordnance, "ETO", "08/1944" )
print(vo_name)
assert "H6[!]" in val
assert _get_capabilities( webdriver, webapp, *ordnance, "ETO", "01/1945" ) == val

@ -25,18 +25,18 @@ def test_individual_files( webapp, webdriver ):
def test_template( template_id, orig_template_id ):
"""Test uploading a customized version of the template."""
# upload a new template
_ = _upload_template_pack_file( template_id+".j2", "UPLOADED TEMPLATE", False )
_ = upload_template_pack_file( template_id+".j2", "UPLOADED TEMPLATE", False )
# make sure generating a snippet returns the new version
_ = _generate_snippet( template_id, orig_template_id )
wait_for_clipboard( 2, "UPLOADED TEMPLATE" )
for_each_template( test_template )
# try uploading a template with an incorrect filename extension
_ = _upload_template_pack_file( "filename.xyz", "UPLOADED TEMPLATE", True )
_ = upload_template_pack_file( "filename.xyz", "UPLOADED TEMPLATE", True )
assert "Invalid template extension" in get_stored_msg( "_last-error_" )
# try uploading a template with an unknown filename
_ = _upload_template_pack_file( "unknown.j2", "UPLOADED TEMPLATE", True )
_ = upload_template_pack_file( "unknown.j2", "UPLOADED TEMPLATE", True )
assert "Invalid template filename" in get_stored_msg( "_last-error_" )
# ---------------------------------------------------------------------
@ -196,7 +196,7 @@ def _upload_template_pack_zip( zip_data, error_expected ):
error_expected
)
def _upload_template_pack_file( fname, data, error_expected ):
def upload_template_pack_file( fname, data, error_expected ):
"""Upload a template pack file."""
return _do_upload_template_pack(
"{} | {}".format( fname, data ),

@ -2,16 +2,21 @@
import json
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from vasl_templates.webapp.tests.utils import \
init_webapp, find_child, 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.tests.test_scenario_persistence import save_scenario, load_scenario
from vasl_templates.webapp.tests.test_template_packs import upload_template_pack_file
from vasl_templates.webapp.config.constants import DATA_DIR as REAL_DATA_DIR
# ---------------------------------------------------------------------
def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ):
"""Test the user settings."""
"""Test including VASL counter images in snippets."""
# initialize
monkeypatch.setitem( webapp.config, "DATA_DIR", REAL_DATA_DIR )
@ -45,10 +50,10 @@ def test_include_vasl_images_in_snippets( webapp, webdriver, monkeypatch ):
snippet_btn.click()
wait_for_clipboard( 2, "/counter/2524/front", contains=False )
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ---------------------------------------------------------------------
def test_include_flags_in_snippets( webapp, webdriver, monkeypatch ):
"""Test the user settings."""
"""Test including flags in snippets."""
# initialize
monkeypatch.setitem( webapp.config, "DATA_DIR", REAL_DATA_DIR )
@ -98,13 +103,82 @@ def test_include_flags_in_snippets( webapp, webdriver, monkeypatch ):
ob_ordnance_snippet_btn.click()
wait_for_clipboard( 2, "/flags/german", contains=False )
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ---------------------------------------------------------------------
def test_date_format( webapp, webdriver ):
"""Test changing the date format."""
# initialize
init_webapp( webapp, webdriver, template_pack_persistence=1, scenario_persistence=1 )
# customize the SCENARIO template
upload_template_pack_file( "scenario.j2",
"{{SCENARIO_YEAR}}-{{SCENARIO_MONTH}}-{{SCENARIO_DAY_OF_MONTH}}",
False
)
scenario_date = find_child( "input[name='SCENARIO_DATE']" )
snippet_btn = find_child( "button.generate[data-id='scenario']" )
def set_scenario_date( date_string ):
"""Set the scenario date."""
scenario_date.clear()
scenario_date.send_keys( date_string )
scenario_date.send_keys( Keys.TAB )
assert scenario_date.get_attribute( "value" ) == date_string
def check_scenario_date( expected ):
"""Check the scenario date is being interpreted correctly."""
assert isinstance(expected,tuple) and len(expected) == 3
assert 1 <= expected[0] <= 31 and 1 <= expected[1] <= 12 and 1940 <= expected[2] <= 1945
# check the snippet
snippet_btn.click()
wait_for_clipboard( 2, "{}-{}-{}".format( expected[2], expected[0], expected[1] ) )
# check the save file (should always be ISO-8601 format)
saved_scenario = save_scenario()
assert saved_scenario["SCENARIO_DATE"] == "{:04}-{:02}-{:02}".format( expected[2], expected[0], expected[1] )
# check the default format (MM/DD/YYYY)
set_scenario_date( "01/02/1940" )
check_scenario_date( (1,2,1940) )
saved_scenario = save_scenario()
# change the date format to YYYY-MM-DD
select_menu_option( "user_settings" )
date_format_sel = Select( find_child( ".ui-dialog.user-settings select[name='date-format']" ) )
date_format_sel.select_by_visible_text( "YYYY-MM-DD" )
click_dialog_button( "OK" )
_check_cookies( webdriver, "date-format", "yy-mm-dd" )
# make sure that it took effect
assert scenario_date.get_attribute( "value" ) == "1940-01-02"
check_scenario_date( (1,2,1940) )
# clear the scenario date, set the date format to DD-MM-YYY
set_scenario_date( "" )
select_menu_option( "user_settings" )
date_format_sel.select_by_visible_text( "DD/MM/YYYY" )
click_dialog_button( "OK" )
_check_cookies( webdriver, "date-format", "dd/mm/yy" )
# set the scenario date
set_scenario_date( "03/04/1945" ) # nb: this will be interpreted as DD/MM/YYYY
check_scenario_date( (4,3,1945) )
# load the scenario we saved before and check the date
load_scenario( saved_scenario )
check_scenario_date( (1,2,1940) )
assert scenario_date.get_attribute( "value" ) == "02/01/1940"
# restore the date format back to default (for the rest of the tests :-/)
select_menu_option( "user_settings" )
date_format_sel.select_by_visible_text( "MM/DD/YYYY" )
click_dialog_button( "OK" )
# ---------------------------------------------------------------------
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", '"' ).replace( "%2C", "," )
print( val )
user_settings = json.loads( val )
assert user_settings[name] == expected

Loading…
Cancel
Save