diff --git a/vasl_templates/main.py b/vasl_templates/main.py
index cf31de4..f558a0b 100755
--- a/vasl_templates/main.py
+++ b/vasl_templates/main.py
@@ -44,16 +44,16 @@ class LoggerProxy:
def main( template_pack, debug ):
"""Main entry point for the application."""
- # configure the autoload template pack
+ # configure the default template pack
if template_pack:
if template_pack.lower().endswith( ".zip" ):
rc = os.path.isfile( template_pack )
else:
rc = os.path.isdir( template_pack )
if not rc:
- click.echo( "ERROR: The template pack must be a ZIP file or a directory containing the template files." )
+ click.echo( "ERROR: The template pack must be a ZIP file, or a directory containing the template files." )
return 1
- snippets.autoload_template_pack = template_pack
+ snippets.default_template_pack = template_pack
# install the debug config file
if debug:
diff --git a/vasl_templates/webapp/data/default-templates/atmm.j2 b/vasl_templates/webapp/data/default-template-pack/atmm.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/atmm.j2
rename to vasl_templates/webapp/data/default-template-pack/atmm.j2
diff --git a/vasl_templates/webapp/data/default-templates/baz.j2 b/vasl_templates/webapp/data/default-template-pack/baz.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/baz.j2
rename to vasl_templates/webapp/data/default-template-pack/baz.j2
diff --git a/vasl_templates/webapp/data/default-templates/mol-p.j2 b/vasl_templates/webapp/data/default-template-pack/mol-p.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/mol-p.j2
rename to vasl_templates/webapp/data/default-template-pack/mol-p.j2
diff --git a/vasl_templates/webapp/data/default-templates/mol.j2 b/vasl_templates/webapp/data/default-template-pack/mol.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/mol.j2
rename to vasl_templates/webapp/data/default-template-pack/mol.j2
diff --git a/vasl_templates/webapp/data/nationalities.json b/vasl_templates/webapp/data/default-template-pack/nationalities.json
similarity index 100%
rename from vasl_templates/webapp/data/nationalities.json
rename to vasl_templates/webapp/data/default-template-pack/nationalities.json
diff --git a/vasl_templates/webapp/data/default-templates/ob_setup.j2 b/vasl_templates/webapp/data/default-template-pack/ob_setup.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/ob_setup.j2
rename to vasl_templates/webapp/data/default-template-pack/ob_setup.j2
diff --git a/vasl_templates/webapp/data/default-templates/pf.j2 b/vasl_templates/webapp/data/default-template-pack/pf.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/pf.j2
rename to vasl_templates/webapp/data/default-template-pack/pf.j2
diff --git a/vasl_templates/webapp/data/default-templates/piat.j2 b/vasl_templates/webapp/data/default-template-pack/piat.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/piat.j2
rename to vasl_templates/webapp/data/default-template-pack/piat.j2
diff --git a/vasl_templates/webapp/data/default-templates/players.j2 b/vasl_templates/webapp/data/default-template-pack/players.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/players.j2
rename to vasl_templates/webapp/data/default-template-pack/players.j2
diff --git a/vasl_templates/webapp/data/default-templates/psk.j2 b/vasl_templates/webapp/data/default-template-pack/psk.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/psk.j2
rename to vasl_templates/webapp/data/default-template-pack/psk.j2
diff --git a/vasl_templates/webapp/data/default-templates/scenario.j2 b/vasl_templates/webapp/data/default-template-pack/scenario.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/scenario.j2
rename to vasl_templates/webapp/data/default-template-pack/scenario.j2
diff --git a/vasl_templates/webapp/data/default-templates/ssr.j2 b/vasl_templates/webapp/data/default-template-pack/ssr.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/ssr.j2
rename to vasl_templates/webapp/data/default-template-pack/ssr.j2
diff --git a/vasl_templates/webapp/data/default-templates/victory_conditions.j2 b/vasl_templates/webapp/data/default-template-pack/victory_conditions.j2
similarity index 100%
rename from vasl_templates/webapp/data/default-templates/victory_conditions.j2
rename to vasl_templates/webapp/data/default-template-pack/victory_conditions.j2
diff --git a/vasl_templates/webapp/snippets.py b/vasl_templates/webapp/snippets.py
index 1205d19..7b4f09d 100644
--- a/vasl_templates/webapp/snippets.py
+++ b/vasl_templates/webapp/snippets.py
@@ -9,83 +9,73 @@ from flask import jsonify, abort
from vasl_templates.webapp import app
from vasl_templates.webapp.config.constants import DATA_DIR
-autoload_template_pack = None
+default_template_pack = None
# ---------------------------------------------------------------------
-@app.route( "/templates/default" )
-def get_default_templates():
- """Get the default templates."""
+@app.route( "/template-pack" )
+def get_template_pack():
+ """Return a template pack.
- # return the default templates
- dname = os.path.join( DATA_DIR, "default-templates" )
- return jsonify( _do_get_templates( dname ) )
-
-# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-@app.route( "/templates/autoload" )
-def get_autoload_templates():
- """Get the templates to auto-load at startup.
-
- We would like the user to be able to specify a template pack to auto-load
- when starting the desktop app, but it's a little tricky to programatically
- get the frontend Javascript to accept an upload. We could possibly do it
- by using QWebChannel, but this would only work if the webapp was running
- inside PyQt. Instead, we get the frontend to call this endpoint when it
- starts up, to get the (optional) autoload templates.
+ Loading template packs is currently handled in the front-end, but we need
+ this entry point for the webapp to get the *default* template pack.
+ If, in the future, we support loading other template packs from the backend,
+ we can add a parameter here to specify which one to return.
"""
- # check if an autoload template pack has been configured
- if not autoload_template_pack:
- # nope - return an empty response
- return jsonify( {} )
-
- # check if the template pack is a directory
- if os.path.isdir( autoload_template_pack ):
- # yup - return the template files in it
- templates = _do_get_templates( autoload_template_pack )
- templates["_path_"] = autoload_template_pack
- return jsonify( templates )
+ # initialize
+ # NOTE: We always start with the default nationalities data. Unlike template files,
+ # user-defined template packs can add to it, or modify existing entries, but not replace it.
+ base_dir = os.path.join( DATA_DIR, "default-template-pack/" )
+ data = { "templates": {} }
+ fname = os.path.join( base_dir, "nationalities.json" )
+ with open(fname,"r") as fp:
+ data["nationalities"] = json.load( fp )
+
+ # check if a default template pack has been configured
+ if default_template_pack:
+ dname = default_template_pack
+ data["_path_"] = dname
+ else:
+ # nope - use our default template pack
+ dname = base_dir
+
+ # check if we're loading the template pack from a directory
+ if os.path.isdir( dname ):
+ # yup - return the files in it
+ nat, templates =_do_get_template_pack( dname )
+ data["nationalities"].update( nat )
+ data["templates"] = templates
+ else:
+ # extract the template pack files from the specified ZIP file
+ if not os.path.isfile( dname ):
+ return jsonify( { "error": "Can't find template pack: {}".format(dname) } )
+ with zipfile.ZipFile( dname, "r" ) as zip_file:
+ for fname in zip_file.namelist():
+ if fname.endswith( "/" ):
+ continue
+ fdata = zip_file.read( fname ).decode( "utf-8" )
+ fname = os.path.split(fname)[1]
+ if fname.lower() == "nationalities.json":
+ data["nationalities"].update( json.loads( fdata ) )
+ else:
+ data[os.path.splitext(fname)[0]] = fdata
- # return the template files in the specified ZIP file
- if not os.path.isfile( autoload_template_pack ):
- return jsonify( { "error": "Can't find template pack: {}".format(autoload_template_pack) } )
- templates = {}
- with zipfile.ZipFile( autoload_template_pack, "r" ) as zip_file:
- for fname in zip_file.namelist():
- if fname.endswith( "/" ):
- continue
- fname2 = os.path.split(fname)[1]
- templates[os.path.splitext(fname2)[0]] = zip_file.read( fname ).decode( "utf-8" )
- templates["_path_"] = autoload_template_pack
- return jsonify( templates )
+ return jsonify( data )
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-def _do_get_templates( dname ):
- """Get the specified templates."""
+def _do_get_template_pack( dname ):
+ """Get the specified template pack."""
if not os.path.isdir( dname ):
abort( 404 )
- templates = {}
+ nationalities, templates = {}, {}
for root,_,fnames in os.walk(dname):
for fname in fnames:
- fname = os.path.join( root, fname )
- if os.path.splitext(fname)[1] != ".j2":
- continue
- with open(fname,"r") as fp:
- fname = os.path.split(fname)[1]
- templates[os.path.splitext(fname)[0]] = fp.read()
- return templates
-
-# ---------------------------------------------------------------------
-
-@app.route( "/nationalities" )
-def get_nationalities():
- """Get the nationalities table."""
-
- # load the nationalities table
- fname = os.path.join( DATA_DIR, "nationalities.json" )
- with open(fname,"r") as fp:
- nationalities = json.load( fp )
-
- return jsonify( nationalities )
+ # add the next file to the results
+ with open( os.path.join(root,fname), "r" ) as fp:
+ if fname.lower() == "nationalities.json":
+ nationalities = json.load( fp )
+ elif os.path.splitext(fname)[1] == ".j2":
+ templates[os.path.splitext(fname)[0]] = fp.read()
+ return nationalities, templates
diff --git a/vasl_templates/webapp/static/main.js b/vasl_templates/webapp/static/main.js
index 8f9e684..65ff8d9 100644
--- a/vasl_templates/webapp/static/main.js
+++ b/vasl_templates/webapp/static/main.js
@@ -1,7 +1,6 @@
-var gNationalities = {} ;
+var gTemplatePack = {} ;
var gDefaultNationalities = {} ;
-var gDefaultTemplates = {} ;
-var gUserDefinedTemplates = {} ;
+var gValidTemplateIds = [] ;
var _NATIONALITY_SPECIFIC_BUTTONS = {
"russian": [ "mol", "mol-p" ],
@@ -104,38 +103,28 @@ $(document).ready( function () {
$("select[name='PLAYER_1_SAN']").html( buf ) ;
$("select[name='PLAYER_2_SAN']").html( buf ) ;
- // load the nationalities
- $.getJSON( gGetNationalitiesUrl, function(data) {
- gNationalities = data ;
- gDefaultNationalities = $.extend( true, {}, data ) ;
- load_nationalities() ;
- on_new_scenario( false ) ;
- } ).fail( function( xhr, status, errorMsg ) {
- showErrorMsg( "Can't get the nationalities:
" + escapeHTML(errorMsg) + "
" ) ;
- } ) ;
-
// add handlers for player changes
$("select[name='PLAYER_1']").change( function() { on_player_change($(this)) ; } ) ;
$("select[name='PLAYER_2']").change( function() { on_player_change($(this)) ; } ) ;
- // get the templates
- $.getJSON( gGetDefaultTemplatesUrl, function(data) {
- gDefaultTemplates = data ;
- } ).fail( function( xhr, status, errorMsg ) {
- showErrorMsg( "Can't get the default templates:
" + escapeHTML(errorMsg) + "
" ) ;
- } ) ;
- $.getJSON( gGetAutoloadTemplatesUrl, function(data) {
+ // get the template pack
+ $.getJSON( gGetTemplatePackUrl, function(data) {
if ( "error" in data )
- showErrorMsg( "Can't get the autoload templates:
" + escapeHTML(data.error) + "
" ) ;
+ showErrorMsg( "Can't get the template pack:
" + escapeHTML(data.error) + "
" ) ;
else {
if ( "_path_" in data ) {
showInfoMsg( "Auto-loaded template pack:
" + escapeHTML(data._path_) + "
" ) ;
delete data._path_ ;
}
- gUserDefinedTemplates = data ;
}
+ install_template_pack( data ) ;
+ on_new_scenario( false ) ;
+ gDefaultNationalities = $.extend( true, {}, data.nationalities ) ;
+ // NOTE: If we are loading a user-defined template pack, then what we think
+ // is the set of valid template ID's will depend on what's in it :-/
+ gValidTemplateIds = Object.keys( data.templates ) ;
} ).fail( function( xhr, status, errorMsg ) {
- showErrorMsg( "Can't get the autoload templates:
" + escapeHTML(errorMsg) + "
" ) ;
+ showErrorMsg( "Can't get the template pack:
" + escapeHTML(errorMsg) + "
" ) ;
} ) ;
var prevHeight = [] ;
@@ -211,14 +200,19 @@ $(document).ready( function () {
// --------------------------------------------------------------------
-function load_nationalities()
+function install_template_pack( data )
{
+ // install the template pack
+ gTemplatePack = data ;
+
// 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( "" ) ;
+ var nationalities = gTemplatePack.nationalities ;
+ for ( var id in nationalities )
+ buf.push( "" ) ;
+ buf = buf.join( "" ) ;
$("select[name='PLAYER_1']").html( buf ).val( curSel1 ) ;
$("select[name='PLAYER_2']").html( buf ).val( curSel2 ) ;
@@ -241,9 +235,10 @@ function on_player_change( $select )
var player_nat = $select.find( "option:selected" ).val() ;
var $elem = $("#tabs .ui-tabs-nav a[href='#tabs-ob" + player_id + "']") ;
var image_url = gImagesBaseUrl + "/flags/" + player_nat + ".png" ;
+ var nationalities = gTemplatePack.nationalities ;
$elem.html(
" " +
- "" + escapeHTML(gNationalities[player_nat].display_name) + " OB"
+ "" + escapeHTML(nationalities[player_nat].display_name) + " OB"
) ;
// show/hide the nationality-specific buttons
diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js
index f84daac..108e379 100644
--- a/vasl_templates/webapp/static/snippets.js
+++ b/vasl_templates/webapp/static/snippets.js
@@ -19,8 +19,10 @@ function generate_snippet( $btn )
storeMsgForTestSuite( "_last-error_", "" ) ;
// unload the template parameters
- var params = unload_params() ;
var template_id = $btn.data( "id" ) ;
+ var params = unload_params() ;
+
+ // set player-specific parameters
if ( template_id === "ob_setup_1" ) {
template_id = "ob_setup" ;
params.OB_SETUP = params.OB_SETUP_1 ;
@@ -35,18 +37,19 @@ function generate_snippet( $btn )
params.OB_SETUP_WIDTH = params.OB_SETUP_WIDTH_2 ;
delete params.OB_SETUP_WIDTH_2 ;
}
+ var nationalities = gTemplatePack.nationalities ;
var curr_tab = $("#tabs .ui-tabs-active a").attr( "href" ) ;
if ( curr_tab === "#tabs-ob1" ) {
- params.OB_COLOR = gNationalities[params.PLAYER_1].ob_colors[0] ;
- params.OB_COLOR_2 = gNationalities[params.PLAYER_1].ob_colors[1] ;
+ params.OB_COLOR = nationalities[params.PLAYER_1].ob_colors[0] ;
+ params.OB_COLOR_2 = nationalities[params.PLAYER_1].ob_colors[1] ;
} if ( curr_tab === "#tabs-ob2" ) {
- params.OB_COLOR = gNationalities[params.PLAYER_2].ob_colors[0] ;
- params.OB_COLOR_2 = gNationalities[params.PLAYER_2].ob_colors[1] ;
+ params.OB_COLOR = nationalities[params.PLAYER_2].ob_colors[0] ;
+ params.OB_COLOR_2 = nationalities[params.PLAYER_2].ob_colors[1] ;
}
// include the player display names
- params.PLAYER_1_NAME = gNationalities[params.PLAYER_1].display_name ;
- params.PLAYER_2_NAME = gNationalities[params.PLAYER_2].display_name ;
+ params.PLAYER_1_NAME = nationalities[params.PLAYER_1].display_name ;
+ params.PLAYER_2_NAME = nationalities[params.PLAYER_2].display_name ;
// extract the scenario date components
var scenario_date = $("input[name='SCENARIO_DATE']").datepicker( "getDate" ) ;
@@ -117,7 +120,11 @@ function generate_snippet( $btn )
}
if ( template_id === "baz" ) {
if ( params.SCENARIO_DATE === "" || params.SCENARIO_YEAR <= 1941 || (params.SCENARIO_YEAR == 1942 && params.SCENARIO_MONTH < 11) )
- showWarningMsg( "BAZ are only available from November 1942." ) ;
+ showWarningMsg( "BAZ are only available from November 1942." ) ;
+ }
+ if ( template_id === "atmm" ) {
+ if ( params.SCENARIO_DATE === "" || params.SCENARIO_YEAR < 1944 )
+ showWarningMsg( "ATMM are only available from 1944." ) ;
}
// check that the players have different nationalities
@@ -187,10 +194,8 @@ function unload_params()
function get_template( template_id )
{
// get the specified template
- if ( template_id in gUserDefinedTemplates )
- return gUserDefinedTemplates[template_id] ;
- else if ( template_id in gDefaultTemplates )
- return gDefaultTemplates[template_id] ;
+ if ( template_id in gTemplatePack.templates )
+ return gTemplatePack.templates[template_id] ;
showErrorMsg( "Unknown template: " + escapeHTML(template_id) + "" ) ;
return null ;
}
@@ -206,7 +211,7 @@ function edit_template( template_id )
function on_template_change() {
// install the new template
- gUserDefinedTemplates[template_id] = $("#edit-template textarea").val() ;
+ gTemplatePack.templates[template_id] = $("#edit-template textarea").val() ;
}
// let the user edit the template
@@ -406,19 +411,23 @@ function do_load_template_pack( fname, data )
// initialize
var invalid_filename_extns = [] ;
var unknown_template_ids = [] ;
- var new_templates = {} ;
- var nationalities = null ;
+ var template_pack = {
+ nationalities: $.extend( true, {}, gDefaultNationalities ),
+ templates: {},
+ } ;
// initialize
- function on_new_template( fname, data ) {
+ function on_template_pack_file( fname, data ) {
// make sure the filename is valid
if ( fname.toLowerCase() === "nationalities.json" ) {
+ var nationalities = null ;
try {
nationalities = JSON.parse( data ) ;
} catch( ex ) {
showWarningMsg( "Can't parse the nationalities JSON data:
" + escapeHTML(ex) + "
" ) ;
return ;
}
+ $.extend( true, template_pack.nationalities, nationalities ) ;
return ;
}
if ( fname.substring(fname.length-3) != ".j2" ) {
@@ -426,16 +435,16 @@ function do_load_template_pack( fname, data )
return ;
}
var template_id = fname.substring( 0, fname.length-3 ).toLowerCase() ;
- if ( ! (template_id in gDefaultTemplates) ) {
+ if ( gValidTemplateIds.indexOf( template_id ) === -1 ) {
unknown_template_ids.push( fname ) ;
return ;
}
- // save the new template file
- new_templates[template_id] = data ;
+ // save the template pack file
+ template_pack.templates[template_id] = data ;
}
// initialize
- function install_new_templates( success_msg ) {
+ function install_new_template_pack( success_msg ) {
// check if there were any errors
var ok = true ;
var buf, tid, i ;
@@ -464,23 +473,16 @@ function do_load_template_pack( fname, data )
buf.push( escapeHTML(unknown_template_ids[i]) + " " ) ;
buf.push( "" ) ;
buf.push( "Must be one of:
" ) ;
- for ( tid in gDefaultTemplates )
- buf.push( "