From c074209dcf66a92f93a40cb22ce766b9e17a067e Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Fri, 25 Nov 2011 23:56:13 -0600 Subject: [PATCH] from gluon import LOAD, thanks Anthony --- VERSION | 2 +- gluon/__init__.py | 4 +-- gluon/compileapp.py | 84 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/VERSION b/VERSION index 1c3ebcf1..8c9ccc2e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 1.99.3 (2011-11-25 22:48:55) dev +Version 1.99.3 (2011-11-25 23:56:06) dev diff --git a/gluon/__init__.py b/gluon/__init__.py index b4ca829b..49e5a4d0 100644 --- a/gluon/__init__.py +++ b/gluon/__init__.py @@ -10,7 +10,7 @@ Web2Py framework modules ======================== """ -__all__ = ['A', 'B', 'BEAUTIFY', 'BODY', 'BR', 'CAT', 'CENTER', 'CLEANUP', 'CODE', 'CRYPT', 'DAL', 'DIV', 'EM', 'EMBED', 'FIELDSET', 'FORM', 'Field', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEAD', 'HR', 'HTML', 'HTTP', 'I', 'IFRAME', 'IMG', 'INPUT', 'IS_ALPHANUMERIC', 'IS_DATE', 'IS_DATETIME', 'IS_DATETIME_IN_RANGE', 'IS_DATE_IN_RANGE', 'IS_DECIMAL_IN_RANGE', 'IS_EMAIL', 'IS_EMPTY_OR', 'IS_EQUAL_TO', 'IS_EXPR', 'IS_FLOAT_IN_RANGE', 'IS_IMAGE', 'IS_INT_IN_RANGE', 'IS_IN_DB', 'IS_IN_SET', 'IS_IPV4', 'IS_LENGTH', 'IS_LIST_OF', 'IS_LOWER', 'IS_MATCH', 'IS_NOT_EMPTY', 'IS_NOT_IN_DB', 'IS_NULL_OR', 'IS_SLUG', 'IS_STRONG', 'IS_TIME', 'IS_UPLOAD_FILENAME', 'IS_UPPER', 'IS_URL', 'LABEL', 'LEGEND', 'LI', 'LINK', 'MARKMIN', 'MENU', 'META', 'OBJECT', 'OL', 'ON', 'OPTGROUP', 'OPTION', 'P', 'PRE', 'SCRIPT', 'SELECT', 'SPAN', 'SQLFORM', 'SQLTABLE', 'STYLE', 'TABLE', 'TAG', 'TBODY', 'TD', 'TEXTAREA', 'TFOOT', 'TH', 'THEAD', 'TITLE', 'TR', 'TT', 'UL', 'URL', 'XHTML', 'XML','redirect','current','embed64'] +__all__ = ['A', 'B', 'BEAUTIFY', 'BODY', 'BR', 'CAT', 'CENTER', 'CLEANUP', 'CODE', 'CRYPT', 'DAL', 'DIV', 'EM', 'EMBED', 'FIELDSET', 'FORM', 'Field', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEAD', 'HR', 'HTML', 'HTTP', 'I', 'IFRAME', 'IMG', 'INPUT', 'IS_ALPHANUMERIC', 'IS_DATE', 'IS_DATETIME', 'IS_DATETIME_IN_RANGE', 'IS_DATE_IN_RANGE', 'IS_DECIMAL_IN_RANGE', 'IS_EMAIL', 'IS_EMPTY_OR', 'IS_EQUAL_TO', 'IS_EXPR', 'IS_FLOAT_IN_RANGE', 'IS_IMAGE', 'IS_INT_IN_RANGE', 'IS_IN_DB', 'IS_IN_SET', 'IS_IPV4', 'IS_LENGTH', 'IS_LIST_OF', 'IS_LOWER', 'IS_MATCH', 'IS_NOT_EMPTY', 'IS_NOT_IN_DB', 'IS_NULL_OR', 'IS_SLUG', 'IS_STRONG', 'IS_TIME', 'IS_UPLOAD_FILENAME', 'IS_UPPER', 'IS_URL', 'LABEL', 'LEGEND', 'LI', 'LINK', 'LOAD', 'MARKMIN', 'MENU', 'META', 'OBJECT', 'OL', 'ON', 'OPTGROUP', 'OPTION', 'P', 'PRE', 'SCRIPT', 'SELECT', 'SPAN', 'SQLFORM', 'SQLTABLE', 'STYLE', 'TABLE', 'TAG', 'TBODY', 'TD', 'TEXTAREA', 'TFOOT', 'TH', 'THEAD', 'TITLE', 'TR', 'TT', 'UL', 'URL', 'XHTML', 'XML','redirect','current','embed64'] from globals import current from html import * @@ -18,7 +18,7 @@ from validators import * from http import redirect, HTTP from dal import DAL, Field from sqlhtml import SQLFORM, SQLTABLE - +from compileapp import LOAD diff --git a/gluon/compileapp.py b/gluon/compileapp.py index 4d75b18f..86123b25 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -28,7 +28,7 @@ from languages import translator from dal import BaseAdapter, SQLDB, SQLField, DAL, Field from sqlhtml import SQLFORM, SQLTABLE from cache import Cache -from globals import current +from globals import current, Response import settings from cfs import getcfs import html @@ -102,6 +102,85 @@ class mybuiltin(object): def __setitem__(self, key, value): setattr(self, key, value) +def LOAD(c=None, f='index', args=None, vars=None, + extension=None, target=None,ajax=False,ajax_trap=False, + url=None,user_signature=False, content='loading...',**attr): + from html import TAG, DIV, URL, SCRIPT, XML + from gluon import current + if args is None: args = [] + vars = Storage(vars or {}) + target = target or 'c'+str(random.random())[2:] + attr['_id']=target + request = current.request + if '.' in f: + f, extension = f.split('.',1) + if url or ajax: + url = url or URL(request.application, c, f, r=request, + args=args, vars=vars, extension=extension, + user_signature=user_signature) + script = SCRIPT('web2py_component("%s","%s")' % (url, target), + _type="text/javascript") + return TAG[''](script, DIV(content,**attr)) + else: + if not isinstance(args,(list,tuple)): + args = [args] + c = c or request.controller + other_request = Storage() + for key, value in request.items(): + other_request[key] = value + other_request['env'] = Storage() + for key, value in request.env.items(): + other_request.env['key'] = value + other_request.controller = c + other_request.function = f + other_request.extension = extension or request.extension + other_request.args = List(args) + other_request.vars = vars + other_request.get_vars = vars + other_request.post_vars = Storage() + other_response = Response() + other_request.env.path_info = '/' + \ + '/'.join([request.application,c,f] + \ + map(str, other_request.args)) + other_request.env.query_string = \ + vars and URL(vars=vars).split('?')[1] or '' + other_request.env.http_web2py_component_location = \ + request.env.path_info + other_request.cid = target + other_request.env.http_web2py_component_element = target + other_response.view = '%s/%s.%s' % (c,f, other_request.extension) + + other_environment = copy.copy(current.globalenv) ### NASTY + + other_response._view_environment = other_environment + other_response.generic_patterns = \ + copy.copy(current.response.generic_patterns) + other_environment['request'] = other_request + other_environment['response'] = other_response + + ## some magic here because current are thread-locals + + original_request, current.request = current.request, other_request + original_response, current.response = current.response, other_response + page = run_controller_in(c, f, other_environment) + if isinstance(page, dict): + other_response._vars = page + for key in page: + other_response._view_environment[key] = page[key] + run_view_in(other_response._view_environment) + page = other_response.body.getvalue() + current.request, current.response = original_request, original_response + js = None + if ajax_trap: + link = URL(request.application, c, f, r=request, + args=args, vars=vars, extension=extension, + user_signature=user_signature) + js = "web2py_trap_form('%s','%s');" % (link, target) + script = js and SCRIPT(js,_type="text/javascript") or '' + return TAG[''](DIV(XML(page),**attr),script) + + + class LoadFactory(object): """ Attention: this helper is new and experimental @@ -256,6 +335,7 @@ def build_environment(request, response, session, store_current=True): t = environment['T'] = translator(request) c = environment['cache'] = Cache(request) if store_current: + current.globalenv = environment current.request = request current.response = response current.session = session @@ -279,7 +359,7 @@ def build_environment(request, response, session, store_current=True): environment['SQLField'] = SQLField # for backward compatibility environment['SQLFORM'] = SQLFORM environment['SQLTABLE'] = SQLTABLE - environment['LOAD'] = LoadFactory(environment) + environment['LOAD'] = LOAD environment['local_import'] = \ lambda name, reload=False, app=request.application:\ local_import_aux(name,reload,app)