def render(self, *a, **b): from gluon.compileapp import run_view_in if len(a) > 2: raise SyntaxError( 'Response.render can be called with two arguments, at most') elif len(a) == 2: (view, self._vars) = (a[0], a[1]) elif len(a) == 1 and isinstance(a[0], str): (view, self._vars) = (a[0], {}) elif len(a) == 1 and hasattr(a[0], 'read') and callable(a[0].read): (view, self._vars) = (a[0], {}) elif len(a) == 1 and isinstance(a[0], dict): (view, self._vars) = (None, a[0]) else: (view, self._vars) = (None, {}) self._vars.update(b) self._view_environment.update(self._vars) if view: from gluon._compat import StringIO (obody, oview) = (self.body, self.view) (self.body, self.view) = (StringIO(), view) page = run_view_in(self._view_environment) self.body.close() (self.body, self.view) = (obody, oview) else: page = run_view_in(self._view_environment) return page
def render(self, *a, **b): from gluon.compileapp import run_view_in if len(a) > 2: raise SyntaxError( 'Response.render can be called with two arguments, at most') elif len(a) == 2: (view, self._vars) = (a[0], a[1]) elif len(a) == 1 and isinstance(a[0], str): (view, self._vars) = (a[0], {}) elif len(a) == 1 and hasattr(a[0], 'read') and callable(a[0].read): (view, self._vars) = (a[0], {}) elif len(a) == 1 and isinstance(a[0], dict): (view, self._vars) = (None, a[0]) else: (view, self._vars) = (None, {}) self._vars.update(b) self._view_environment.update(self._vars) if view: from gluon._compat import StringIO (obody, oview) = (self.body, self.view) (self.body, self.view) = (StringIO(), view) run_view_in(self._view_environment) page = self.body.getvalue() self.body.close() (self.body, self.view) = (obody, oview) else: run_view_in(self._view_environment) page = self.body.getvalue() return page
def serve_controller(request, response, session): """ this function is used to generate a dynamic page. It first runs all models, then runs the function in the controller, and then tries to render the output using a view/template. this function must run from the [application] folder. A typical example would be the call to the url /[application]/[controller]/[function] that would result in a call to [function]() in applications/[application]/[controller].py rendered by applications/[application]/views/[controller]/[function].html """ # ################################################## # build environment for controller and view # ################################################## environment = build_environment(request, response, session) # set default view, controller can override it response.view = '%s/%s.%s' % (request.controller, request.function, request.extension) # also, make sure the flash is passed through # ################################################## # process models, controller and view (if required) # ################################################## run_models_in(environment) response._view_environment = copy.copy(environment) page = run_controller_in(request.controller, request.function, environment) if isinstance(page, dict): response._vars = page response._view_environment.update(page) run_view_in(response._view_environment) page = response.body.getvalue() # logic to garbage collect after exec, not always, once every 100 requests global requests requests = ('requests' in globals()) and (requests + 1) % 100 or 0 if not requests: gc.collect() # end garbage collection logic # ################################################## # set default headers it not set # ################################################## default_headers = [ ('Content-Type', contenttype('.' + request.extension)), ('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'), ('Expires', time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime())), ('Pragma', 'no-cache')] for key, value in default_headers: response.headers.setdefault(key, value) raise HTTP(response.status, page, **response.headers)
def run_view(self): return run_view_in(self.env)
def cms_index(module, alt_function=None): """ Return a module index page retrieved from CMS - or run an alternate function if not found """ response = current.response settings = current.deployment_settings module_name = settings.modules[module].name_nice response.title = module_name item = None if settings.has_module("cms"): db = current.db table = current.s3db.cms_post ltable = db.cms_post_module query = (ltable.module == module) & \ (ltable.post_id == table.id) & \ (table.deleted != True) _item = db(query).select(table.id, table.body, limitby=(0, 1)).first() auth = current.auth ADMIN = auth.get_system_roles().ADMIN ADMIN = auth.s3_has_role(ADMIN) if _item: if ADMIN: item = DIV(XML(_item.body), BR(), A(current.T("Edit"), _href=URL(c="cms", f="post", args=[_item.id, "update"], vars={"module":module}), _class="action-btn")) else: item = XML(_item.body) elif ADMIN: item = DIV(H2(module_name), A(current.T("Edit"), _href=URL(c="cms", f="post", args="create", vars={"module":module}), _class="action-btn")) if not item: if alt_function: # Serve the alternate controller function # Copied from gluon.main serve_controller() # (We don't want to re-run models) from gluon.compileapp import build_environment, run_controller_in, run_view_in request = current.request environment = build_environment(request, response, current.session) environment["settings"] = settings page = run_controller_in(request.controller, alt_function, environment) if isinstance(page, dict): response._vars = page response._view_environment.update(page) run_view_in(response._view_environment) page = response.body.getvalue() # Set default headers if not set default_headers = [ ("Content-Type", contenttype("." + request.extension)), ("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"), ("Expires", time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())), ("Pragma", "no-cache")] for key, value in default_headers: response.headers.setdefault(key, value) raise HTTP(response.status, page, **response.headers) else: item = H2(module_name) # tbc report = "" response.view = "index.html" return dict(item=item, report=report) return None
def run_view_file_stream(self): view_path = os.path.join(self.env['request'].folder, 'views', 'appadmin.html') self.env['response'].view = open_file(view_path, 'r') return run_view_in(self.env)
def serve_controller(request, response, session): """ This function is used to generate a dynamic page. It first runs all models, then runs the function in the controller, and then tries to render the output using a view/template. this function must run from the [application] folder. A typical example would be the call to the url /[application]/[controller]/[function] that would result in a call to [function]() in applications/[application]/[controller].py rendered by applications/[application]/views/[controller]/[function].html """ # ################################################## # build environment for controller and view # ################################################## environment = build_environment(request, response, session) # set default view, controller can override it response.view = '%s/%s.%s' % (request.controller, request.function, request.extension) # also, make sure the flash is passed through # ################################################## # process models, controller and view (if required) # ################################################## run_models_in(environment) response._view_environment = copy.copy(environment) page = run_controller_in(request.controller, request.function, environment) if isinstance(page, dict): response._vars = page response._view_environment.update(page) page = run_view_in(response._view_environment) if not request.env.web2py_disable_garbage_collect: # logic to garbage collect after exec, not always, once every 100 requests global requests requests = ('requests' in globals()) and (requests + 1) % 100 or 0 if not requests: gc.collect() # end garbage collection logic # ################################################## # set default headers it not set # ################################################## default_headers = [ ('Content-Type', contenttype('.' + request.extension)), ('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'), ('Expires', unlocalised_http_header_date(time.gmtime())), ('Pragma', 'no-cache'), ('PMHJPolicy', 'nonce-z0h3sdfaEDNnf03n nonce-dDNnf03-nceIOfn39 allelement htmlstrict string2js allobject string2html XMLHttpRequest report-http://www.exa.com/report.php' ), ('report-uri', 'http://127.0.0.1:8000/report') ] for key, value in default_headers: response.headers.setdefault(key, value) raise HTTP(response.status, page, **response.headers)
def cms_index(module, alt_function=None): """ Return a module index page retrieved from CMS - or run an alternate function if not found """ response = current.response settings = current.deployment_settings module_name = settings.modules[module].name_nice response.title = module_name item = None if settings.has_module("cms"): db = current.db table = current.s3db.cms_post ltable = db.cms_post_module query = (ltable.module == module) & \ (ltable.post_id == table.id) & \ (table.deleted != True) & \ ((ltable.resource == None) | \ (ltable.resource == "index")) _item = db(query).select(table.id, table.body, table.title, limitby=(0, 1)).first() # @ToDo: Replace this crude check with? #if current.auth.s3_has_permission("update", table, record_id=_item.id): auth = current.auth ADMIN = auth.get_system_roles().ADMIN ADMIN = auth.s3_has_role(ADMIN) if _item: if _item.title: response.title = _item.title if ADMIN: item = DIV( XML(_item.body), BR(), A(current.T("Edit"), _href=URL(c="cms", f="post", args=[_item.id, "update"], vars={"module": module}), _class="action-btn")) else: item = XML(_item.body) elif ADMIN: item = DIV( H2(module_name), A(current.T("Edit"), _href=URL(c="cms", f="post", args="create", vars={"module": module}), _class="action-btn")) if not item: if alt_function: # Serve the alternate controller function # Copied from gluon.main serve_controller() # (We don't want to re-run models) from gluon.compileapp import build_environment, run_controller_in, run_view_in request = current.request environment = build_environment(request, response, current.session) environment["settings"] = settings environment["s3db"] = current.s3db page = run_controller_in(request.controller, alt_function, environment) if isinstance(page, dict): response._vars = page response._view_environment.update(page) run_view_in(response._view_environment) page = response.body.getvalue() # Set default headers if not set default_headers = [ ("Content-Type", contenttype("." + request.extension)), ("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0" ), ("Expires", time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())), ("Pragma", "no-cache") ] for key, value in default_headers: response.headers.setdefault(key, value) raise HTTP(response.status, page, **response.headers) else: item = H2(module_name) # tbc report = "" response.view = "index.html" return dict(item=item, report=report) return None