From 2b0e11e843e739368c912edf11626a33fe41a1a0 Mon Sep 17 00:00:00 2001 From: Renaud Le Gac <renaud.legac@free.fr> Date: Wed, 15 Feb 2012 21:31:18 +0100 Subject: [PATCH] Redesign the configuration parameters of the plugins. Introduce a new helper function get_file_paths. Redesign index and debug controller as well as the associated view. --- models/db.py | 9 ++++-- models/plugin_dbui.py | 33 ++++++++++++++++++---- modules/plugin_dbui/__init__.py | 2 +- modules/plugin_dbui/helper.py | 50 +++++++++++++++++++-------------- views/plugin_dbui/index.html | 21 +++++++------- 5 files changed, 74 insertions(+), 41 deletions(-) diff --git a/models/db.py b/models/db.py index 8cb3beb9..d40abff6 100644 --- a/models/db.py +++ b/models/db.py @@ -11,12 +11,15 @@ from gluon.tools import PluginManager #------------------------------------------------------------------------------- # -# web2py-dbui plugin parameters +# plugin configuration parameters # #------------------------------------------------------------------------------- #plugins = PluginManager() -#plugins.dbui.script_path = 'static/scripts' -#plugins.dbui.page_view = 'plugin_dbui.html' +#plugins.dbui.app_css = None # relative path, i.e static/myapp.css or dir +#plugins.dbui.app_js_dir = None # relative directory static/src +#plugins.dbui.app_libmin = None # relative path static/myapp-min.js or dir +#plugins.dbui.app_script = None # relative path static/myapp.js +#plugins.dbui.app_script_dir = None # relative directory static/scripts #------------------------------------------------------------------------------- # diff --git a/models/plugin_dbui.py b/models/plugin_dbui.py index d2705a60..3c59c15e 100644 --- a/models/plugin_dbui.py +++ b/models/plugin_dbui.py @@ -10,16 +10,37 @@ from gluon.storage import Storage from gluon.tools import PluginManager dbui = local_import('plugin_dbui') - -# define persistent data for the plugin and their default value +# NOTE: +# define default configuration parameters for the plugin, +# the application using it and the persistent storage used to tune the widgets. +# In most of the case configuration parameters are either a relative path +# to a file (static/myapp.css) or to a directory (static/css) except app_script +# and base_script which should point to a file. +# +# All these parameters should be defined in the applications model. +# plugins = PluginManager('dbui', + + app_css=None, # relative path static/myapp.css or dir + app_js_dir=None, # relative directroy static/src + app_libmin=None, # relative path static/myapp-min.js or dir + app_script=None, # relative path static/myapp.js + app_script_dir=None, # relative directory static/scripts + + base_css='static/plugin_dbui/resources/css/kde-oxygen.css', + base_js_dir='static/plugin_dbui/src', + base_lg=dbui.get_language(), + base_libmin='static/plugin_dbui/dbui-min.js', + base_pdbui=dbui.get_plugin_path('plugin_dbui'), + base_pextjs=dbui.get_plugin_path('plugin_extjs'), + base_pmathjax=dbui.get_plugin_path('plugin_mathjax'), + base_script='static/plugin_dbui/main.js', + field_modifiers={}, form_modifiers={}, grid_modifiers={}, - viewport_modifiers=Storage(extjs={}), - script_path='static/scripts', - user_src_path='static/src', - user_libmin=None) + viewport_modifiers=Storage(extjs={})) + # Start common services dbSvc = dbui.DbSvc(globals()) diff --git a/modules/plugin_dbui/__init__.py b/modules/plugin_dbui/__init__.py index bca7465d..296d49b4 100755 --- a/modules/plugin_dbui/__init__.py +++ b/modules/plugin_dbui/__init__.py @@ -12,7 +12,7 @@ from helper import (as_list, decode_field, encode_field, get_field_validators, - get_files, + get_file_paths, get_foreign_field, get_language, get_plugin_path, diff --git a/modules/plugin_dbui/helper.py b/modules/plugin_dbui/helper.py index 2f98b8bd..5f0eb411 100644 --- a/modules/plugin_dbui/helper.py +++ b/modules/plugin_dbui/helper.py @@ -105,9 +105,9 @@ def get_field_validators(field): return cfg -def get_files(path, ext=None, alpha=False): - """Return a list of files according to path: - Path for a file or a directory is a local path relative to the application. +def get_file_paths(path, ext=None, alpha=True): + """Return a list of files paths according to the relative path: + The relative path for a file or a directory is relative to the application. A path can be: None. @@ -126,6 +126,8 @@ def get_files(path, ext=None, alpha=False): The method return a list with local paths of the selected files and those located in the selected directory. + The return path is the client path which can be used in view. + The keyword argument ext allows to filter files located in directory by their extension: '.js', '.css'. @@ -200,8 +202,7 @@ def get_plugin_path(plugin_name): The local path defined with respect to the application directory. The function return the path of the first directory starting with - plugin_name. It is useful to handle plugin with a version number - like plugin_extjs_3.3.2. + plugin_name. Return None if the plugin is not found. @@ -243,37 +244,44 @@ def get_set_field(field): return [] -def get_script_path(script_path): +def get_script_path(plugin): """Helper method returning the local path of the main script. the local path is defined with respect to the application directory. - The default script is static/plugin_dbui/main.js + There is many way to define the main script via the url and/or via the + configuration parameters of the plugin (application, base). - The script name can be set via the url using the argument script - It is search in script_path, i.e static/scripts + If the script argument is found in the url, the main script is search in + script_dir, i.e static/scripts. the script name is given by the argument + value. - """ - # default path - base = os.path.join(os.path.sep, current.request.application) - script = os.path.join(base, 'static', 'plugin_dbui', 'main.js') + Next in the list is the application script which might be defined in the + plugin configuration parameters. If no found the script associate to the + plugin is used. - # if the user specified a script in the URL + """ + server_path, client_path = get_reference_paths() + + # script name specifies in the URL if "script" in current.request.vars: script = current.request.vars.script if not script.endswith(".js"): script = '%s.js' % script - server_path = os.path.join('applications', current.request.application) - script_dir = os.path.join(server_path, script_path) + pdir = os.path.join(server_path, plugin.app_script_dir) - if script not in os.listdir(script_dir): + if script not in os.listdir(pdir): raise HTTP(500, 'Request script "%s" does not exist !!!' % script) -# return 'Request script "%s" does not exist !!!' % script - - script = os.path.join(base, script_path, script) + + return os.path.join(client_path, plugin.app_script_dir, script) - return script + # default script for the application + if plugin.app_script: + return os.path.join(client_path, plugin.app_script) + + # default script for the plugin + return os.path.join(client_path, plugin.base_script) def get_where_query(table): diff --git a/views/plugin_dbui/index.html b/views/plugin_dbui/index.html index da0645a6..cbdd9ede 100644 --- a/views/plugin_dbui/index.html +++ b/views/plugin_dbui/index.html @@ -23,22 +23,23 @@ "HTML-CSS": { availableFonts: ["TeX"] } }); </script> - <script type="text/javascript" src="{{=pmathjax}}/MathJax.js"></script> + <script type="text/javascript" src="{{=plugin.base_pmathjax}}/MathJax.js"></script> - <!-- extjs css --> - <link rel="stylesheet" type="text/css" href="{{=pextjs}}/resources/css/ext-all.css"/> - <link rel="stylesheet" type="text/css" href="/{{=request.application}}/static/plugin_dbui/resources/css/kde-oxygen.css"/> + <!-- extjs + dbui + application css --> + <link rel="stylesheet" type="text/css" href="{{=plugin.base_pextjs}}/resources/css/ext-all.css"/> + {{for el in response.files:}}{{if el.endswith('.css'):}}<link rel="stylesheet" type="text/css" href="{{=el}}"/>{{pass}} + {{pass}} <!-- extjs javascript library --> - <script type="text/javascript" src="{{=pextjs}}/adapter/ext/ext-base.js"></script> - <script type="text/javascript" src="{{=pextjs}}/ext-all.js"></script> - <script type="text/javascript" src="{{=pextjs}}/src/locale/ext-lang-{{=lg}}.js"></script> + <script type="text/javascript" src="{{=plugin.base_pextjs}}/adapter/ext/ext-base.js"></script> + <script type="text/javascript" src="{{=plugin.base_pextjs}}/ext-all.js"></script> + <script type="text/javascript" src="{{=plugin.base_pextjs}}/src/locale/ext-lang-{{=plugin.base_lg}}.js"></script> <!-- dbui + user javascript library and main script --> <script type="text/javascript" src="/{{=request.application}}/plugin_dbui/get_api"></script> - <script type="text/javascript" src="/{{=request.application}}/static/plugin_dbui/dbui-min.js"></script> - <script type="text/javascript" src="/{{=request.application}}/static/plugin_dbui/locale/app-lang-{{=lg}}.js"></script> - {{for el in response.files:}}<script type="text/javascript" src="{{=el}}"></script>{{pass}} + <script type="text/javascript" src="/{{=plugin.base_pdbui}}/locale/app-lang-{{=plugin.base_lg}}.js"></script> + {{for el in response.files:}}{{if el.endswith('.js'):}}<script type="text/javascript" src="{{=el}}"></script>{{pass}} + {{pass}} </head> <body> -- GitLab