diff --git a/controllers/plugin_dbui.py b/controllers/plugin_dbui.py index 227eaee12ef0c357ba52299ac29357d2102ad97e..155b7e9d160237bdcacc5d7f60968929d6bb657a 100644 --- a/controllers/plugin_dbui.py +++ b/controllers/plugin_dbui.py @@ -1,13 +1,13 @@ """ plugin_dbui.py - Controllers expose by the plugin: - - index - database - configuration + Controllers expose by the plugin: + + index + database + configuration - Author: R. Le Gac - $Id$ + Author: R. Le Gac + $Id$ """ @@ -25,16 +25,16 @@ from pprint import pprint def index(): - """ Main Controller to run the plugin + """ Main Controller to run the plugin - SYNOPSIS - http://localhost:8000/application/plugin_dbui[?options] + SYNOPSIS + http://localhost:8000/application/plugin_dbui[?options] - DESCRIPTION - Main controller to launch the plugin. - Load the extjs librairy and the associated css files. - Load the javascript librairy for the plugin. - Define view. + DESCRIPTION + Main controller to launch the plugin. + Load the extjs librairy and the associated css files. + Load the javascript librairy for the plugin. + Define view. PARAMETERS WHICH CAN BE SET VIA THE PLUGINMANAGER @@ -45,221 +45,226 @@ def index(): script_path The relative path where scripts are located. By default static/plugin_dbui/scripts + + with_grid_filter + Add the javascript librairy for gridFilter - OPTIONS + OPTIONS - debug - activate the debug mode on the server side. + debug + activate the debug mode on the server side. - script foo - run the javascript foo in the framework defined by the plugin. - The scripts are stored in the directory application/static/script - by default the javascript main.js is launch. + script foo + run the javascript foo in the framework defined by the plugin. + The scripts are stored in the directory application/static/script + by default the javascript main.js is launch. - EXAMPLES + EXAMPLES - http://localhost:8000/application/plugin_dbui - http://localhost:8000/application/plugin_dbui?debug - http://localhost:8000/application/plugin_dbui?script=foo&debug + http://localhost:8000/application/plugin_dbui + http://localhost:8000/application/plugin_dbui?debug + http://localhost:8000/application/plugin_dbui?script=foo&debug - """ - # define plugin parameters and their default value - plugins = PluginManager('dbui', + """ + # define plugin parameters and their default value + plugins = PluginManager('dbui', page_view='plugin_dbui.html', - script_path='static/plugin_dbui/scripts') - - # paths - base = os.path.join(os.path.sep, request.application) - appjs = os.path.join(base, 'static', 'plugin_dbui', 'lib') - extjs = os.path.join(base, 'static', 'plugin_extjs') - server_path = os.path.join('applications', request.application) - script_path = os.path.join(server_path, plugins.dbui.script_path) - - # check the presence of the extjs plugin - path = os.path.join(server_path, 'static', 'plugin_extjs') - if not os.path.exists(path): - raise HTTP(500, 'The plugin extjs is not install !') - - # option debug - debug = False - if "debug" in request.get_vars: - debug = True - - # option script - script = "default" - if "script" in request.get_vars: - script = request.get_vars.script - - if not script.endswith(".js"): - script = '%s.js' % script - - if script not in os.listdir(script_path): - return 'Request script "%s" does not exist !!!' % script - - # css files - csslibs = [os.path.join(extjs, 'resources', 'css', 'ext-all.css'), - os.path.join(extjs, 'examples', 'shared', 'icons', 'silk.css'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'css', 'GridFilters.css'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'css', 'RangeMenu.css'), -# os.path.join(extjs, 'examples', 'shared', 'examples.css'), - ] - - # javascript libraries - if debug: - jslibs = [os.path.join(extjs, 'adapter', 'ext', 'ext-base-debug.js'), - os.path.join(extjs, 'ext-all-debug.js'), -# os.path.joinextjs, 'examples', 'ux', 'ux-all-debug.js'), - ] - else: - jslibs = [os.path.join(extjs, 'adapter', 'ext', 'ext-base.js'), - os.path.join(extjs, 'ext-all.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'menu', 'RangeMenu.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'menu', 'ListMenu.js'), - - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'GridFilters.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'filter', 'Filter.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'filter', 'BooleanFilter.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'filter', 'DateFilter.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'filter', 'ListFilter.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'filter', 'NumericFilter.js'), - os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'filter', 'StringFilter.js'), -# os.path.join(extjs, 'examples', 'shared', 'examples.js'), - ] - - # plugin javascript libraries - applibs = [] - for file in os.listdir(os.path.join(server_path, 'static', 'plugin_dbui', 'lib')): - if file.endswith(".js"): - applibs.append(os.path.join(appjs, file)) - - # main application script - applibs.sort() - applibs.append(os.path.join(base, plugins.dbui.script_path, script)) - - # page view - response.view = plugins.dbui.page_view - - return dict(clibs=csslibs,\ - jlibs=jslibs,\ - alibs= applibs,\ - appdebug=debug,\ - cfgSvcMethods=cfgSvc._getMethods()) + script_path='static/plugin_dbui/scripts', + with_grid_filter=False) + + # paths + base = os.path.join(os.path.sep, request.application) + appjs = os.path.join(base, 'static', 'plugin_dbui', 'lib') + extjs = os.path.join(base, 'static', 'plugin_extjs') + server_path = os.path.join('applications', request.application) + script_path = os.path.join(server_path, plugins.dbui.script_path) + + # check the presence of the extjs plugin + path = os.path.join(server_path, 'static', 'plugin_extjs') + if not os.path.exists(path): + raise HTTP(500, 'The plugin extjs is not install !') + + # option debug + debug = False + if "debug" in request.get_vars: + debug = True + + # option script + script = "default" + if "script" in request.get_vars: + script = request.get_vars.script + + if not script.endswith(".js"): + script = '%s.js' % script + + if script not in os.listdir(script_path): + return 'Request script "%s" does not exist !!!' % script + + # css files + csslibs = [os.path.join(extjs, 'resources', 'css', 'ext-all.css'), + os.path.join(extjs, 'examples', 'shared', 'icons', 'silk.css')] + + if plugins.dbui.with_grid_filter: + cpath = os.path.join(extjs, 'examples', 'ux', 'gridfilters', 'css') + csslibs.extend([os.path.join(cpath, 'GridFilters.css'), + os.path.join(cpath, 'RangeMenu.css')]) + + # javascript libraries + if debug: + jslibs = [os.path.join(extjs, 'adapter', 'ext', 'ext-base-debug.js'), + os.path.join(extjs, 'ext-all-debug.js')] + + else: + jslibs = [os.path.join(extjs, 'adapter', 'ext', 'ext-base.js'), + os.path.join(extjs, 'ext-all.js')] + + if plugins.dbui.with_grid_filter: + jpath = os.path.join(extjs, 'examples', 'ux', 'gridfilters') + li = [os.path.join(jpath, 'menu', 'RangeMenu.js'), + os.path.join(jpath, 'menu', 'ListMenu.js'), + os.path.join(jpath, 'GridFilters.js'), + os.path.join(jpath, 'filter', 'Filter.js'), + os.path.join(jpath, 'filter', 'BooleanFilter.js'), + os.path.join(jpath, 'filter', 'DateFilter.js'), + os.path.join(jpath, 'filter', 'ListFilter.js'), + os.path.join(jpath, 'filter', 'NumericFilter.js'), + os.path.join(jpath, 'filter', 'StringFilter.js')] + jslib.extend(li) + + # plugin libraries + applibs = [] + for file in os.listdir(os.path.join(server_path, 'static', 'plugin_dbui', 'lib')): + if file.endswith(".js"): + applibs.append(os.path.join(appjs, file)) + + # main application script + applibs.sort() + applibs.append(os.path.join(base, plugins.dbui.script_path, script)) + + # page view + response.view = plugins.dbui.page_view + + return dict(clibs=csslibs,\ + jlibs=jslibs,\ + alibs= applibs,\ + appdebug=debug,\ + cfgSvcMethods=cfgSvc._getMethods()) def database(): - """ Controllers to perform operation on the database. - Act as a bridge between the web browser and the database service. - - All information defining the operation are sent via the POST/GET methods. - It contains steering keyword defining the requested action. - - Return a JSON string when the action is successful. - Otherwise return and HTTP error - - """ - debug = "debug" in request.vars and (request.vars.debug == 'true') - - if debug: - print "\nSTART DATABASE CONTROLLER" - print "Method:", request.env.request_method - print "Arguments:", request.vars - - try: - - # decode the data dictionary - if "data" in request.vars and isinstance(request.vars.data, str): - request.vars.data = json.loads(request.vars.data) - - di = dbSvc.proceed(request.vars) - resp = json.dumps(di) - - except BaseException, e: - print "Exception in the database controller:" - print "-"*72 - traceback.print_exc(file=sys.stdout) - print "-"*72 - raise HTTP(500, e) - - if debug: - print "\nDATABASE CONTROLLER RESPONSE IS:" - pprint(di) - print "END OF DATABASE CONTROLLER\n" - - return resp + """ Controllers to perform operation on the database. + Act as a bridge between the web browser and the database service. + + All information defining the operation are sent via the POST/GET methods. + It contains steering keyword defining the requested action. + + Return a JSON string when the action is successful. + Otherwise return and HTTP error + + """ + debug = "debug" in request.vars and (request.vars.debug == 'true') + + if debug: + print "\nSTART DATABASE CONTROLLER" + print "Method:", request.env.request_method + print "Arguments:", request.vars + + try: + + # decode the data dictionary + if "data" in request.vars and isinstance(request.vars.data, str): + request.vars.data = json.loads(request.vars.data) + + di = dbSvc.proceed(request.vars) + resp = json.dumps(di) + + except BaseException, e: + print "Exception in the database controller:" + print "-"*72 + traceback.print_exc(file=sys.stdout) + print "-"*72 + raise HTTP(500, e) + + if debug: + print "\nDATABASE CONTROLLER RESPONSE IS:" + pprint(di) + print "END OF DATABASE CONTROLLER\n" + + return resp def configuration(): - """ Controllers/router for the web service cfgSVc - Act as a bridge between the web browser and the configuration service. - - Relies on Ext.Direct see specification: - http://www.extjs.com/products/extjs/direct.php - - Act as a router for the cfgSvc service (see Ext.Direct Extjs 3.1.1) - Decode the raw HTTP post request: - {action: "cfgSvc", method: x, data: y, tip: i, type: "rpc"} - - Return a JSON string with the result of the request: - {action: "cfgSvc", method: x, result: y, tip: i, type: "rpc"} - - Otherwise return and HTTP error. - - For more detail on Ext.Direct see specification: - http://www.extjs.com/products/extjs/direct.php - - - """ - debug = "debug" in request.vars and (request.vars.debug == 'true') - - # NOTE: the server cfgSvc is start at the beginning - # when building the model (db.py) - - # decode the raw HTTP post - # ExtJs.3.1.1 {action: "xx", method: "y", data: [], type:"rpc", tid:2} - - req = json.loads(request.body.read()) - - if debug: - print "\nSTART CONFIGURATION CONTROLLER:" - pprint(req) - - # receive a list of dictionary for a multi-request - li = [] - if isinstance(req, list): - li.extend(req) - else: - li.append(req) - - for di in li: - # construct the command to be executed - args = di["data"] - cmd = "%s.%s(*args)" % (di["action"], di["method"]) - - if args == None: - cmd = "%s.%s()" % (di["action"], di["method"]) - - # execute the command and return HTTP exception - # when an internal error occurred - try: - res = eval(cmd) - except BaseException, e: - print "Exception in the configuration controller:" - print "-"*72 - traceback.print_exc(file=sys.stdout) - print "-"*72 - raise HTTP(500, e) - - # NOTE: the client expect the following object: - # ExtJs.3.1.1 {action: "xx", method: "y", result: [], type:"rpc", tid:2} - - del di["data"] - di["result"] = res - - if debug: - print "\nCONFIGURATION CONTROLLER RESPONSE IS:" - pprint(li) - print "\nEND OF CONFIGURATION CONTROLLER\n" - - # encode the data as a json string - return json.dumps(li) \ No newline at end of file + """ Controllers/router for the web service cfgSVc + Act as a bridge between the web browser and the configuration service. + + Relies on Ext.Direct see specification: + http://www.extjs.com/products/extjs/direct.php + + Act as a router for the cfgSvc service (see Ext.Direct Extjs 3.1.1) + Decode the raw HTTP post request: + {action: "cfgSvc", method: x, data: y, tip: i, type: "rpc"} + + Return a JSON string with the result of the request: + {action: "cfgSvc", method: x, result: y, tip: i, type: "rpc"} + + Otherwise return and HTTP error. + + For more detail on Ext.Direct see specification: + http://www.extjs.com/products/extjs/direct.php + + + """ + debug = "debug" in request.vars and (request.vars.debug == 'true') + + # NOTE: the server cfgSvc is start at the beginning + # when building the model (db.py) + + # decode the raw HTTP post + # ExtJs.3.1.1 {action: "xx", method: "y", data: [], type:"rpc", tid:2} + + req = json.loads(request.body.read()) + + if debug: + print "\nSTART CONFIGURATION CONTROLLER:" + pprint(req) + + # receive a list of dictionary for a multi-request + li = [] + if isinstance(req, list): + li.extend(req) + else: + li.append(req) + + for di in li: + # construct the command to be executed + args = di["data"] + cmd = "%s.%s(*args)" % (di["action"], di["method"]) + + if args == None: + cmd = "%s.%s()" % (di["action"], di["method"]) + + # execute the command and return HTTP exception + # when an internal error occurred + try: + res = eval(cmd) + except BaseException, e: + print "Exception in the configuration controller:" + print "-"*72 + traceback.print_exc(file=sys.stdout) + print "-"*72 + raise HTTP(500, e) + + # NOTE: the client expect the following object: + # ExtJs.3.1.1 {action: "xx", method: "y", result: [], type:"rpc", tid:2} + + del di["data"] + di["result"] = res + + if debug: + print "\nCONFIGURATION CONTROLLER RESPONSE IS:" + pprint(li) + print "\nEND OF CONFIGURATION CONTROLLER\n" + + # encode the data as a json string + return json.dumps(li) \ No newline at end of file