def bootstrap(paths, global_env=None, filters=None): """Resets cached Jinja2 env to pick up new template paths. This is purely additive and idempotent. So consecutive calls to this functions with different arguments is fine. Args: paths: dict {prefix -> template_dir}, templates under template_dir would be accessible as <prefix>/<path relative to template_dir>. global_env: dict with variables to add to global template environment. filters: dict with filters to add to global filter list. """ assert isinstance(paths, dict), paths assert all( _TEMPLATE_PATHS.get(k, v) == v for k, v in paths.items()), paths assert all(os.path.isabs(p) for p in paths.values()), paths assert all(os.path.isdir(p) for p in paths.values()), paths if global_env is not None: assert isinstance(global_env, dict), global_env assert all(isinstance(k, str) for k in global_env), global_env assert all( _GLOBAL_ENV.get(k, v) == v for k, v in global_env.items()), global_env if filters is not None: assert isinstance(filters, dict), filters assert all( isinstance(k, str) and callable(v) for k, v in filters.items()), filters assert all( _GLOBAL_FILTERS.get(k, v) == v for k, v in filters.items()), filters _TEMPLATE_PATHS.update(paths) if global_env: _GLOBAL_ENV.update(global_env) # These are immutable. _GLOBAL_ENV.setdefault('app_id', app_identity.get_application_id()) _GLOBAL_ENV.setdefault('app_version', utils.get_app_version()) _GLOBAL_ENV.setdefault('app_revision_url', utils.get_app_revision_url()) if filters: _GLOBAL_FILTERS.update(filters) utils.clear_cache(get_jinja_env)
def bootstrap(paths, global_env=None, filters=None): """Resets cached Jinja2 env to pick up new template paths. This is purely additive and idempotent. So consecutive calls to this functions with different arguments is fine. Args: paths: dict {prefix -> template_dir}, templates under template_dir would be accessible as <prefix>/<path relative to template_dir>. global_env: dict with variables to add to global template environment. filters: dict with filters to add to global filter list. """ assert isinstance(paths, dict), paths assert all( _TEMPLATE_PATHS.get(k, v) == v for k, v in paths.iteritems()), paths assert all(os.path.isabs(p) for p in paths.itervalues()), paths assert all(os.path.isdir(p) for p in paths.itervalues()), paths if global_env is not None: assert isinstance(global_env, dict), global_env assert all(isinstance(k, str) for k in global_env), global_env assert all( _GLOBAL_ENV.get(k, v) == v for k, v in global_env.iteritems()), global_env if filters is not None: assert isinstance(filters, dict), filters assert all( isinstance(k, str) and callable(v) for k, v in filters.iteritems()), filters assert all( _GLOBAL_FILTERS.get(k, v) == v for k, v in filters.iteritems()), filters _TEMPLATE_PATHS.update(paths) if global_env: _GLOBAL_ENV.update(global_env) _GLOBAL_ENV.setdefault('app_version', utils.get_app_version()) _GLOBAL_ENV.setdefault('app_revision_url', utils.get_app_revision_url()) if filters: _GLOBAL_FILTERS.update(filters) utils.clear_cache(get_jinja_env)
def reply(self, path, env=None, status=200): """Renders template |path| to the HTTP response using given environment. Optional keys from |env| that base.html uses: css_file: URL to a file with page specific styles, relative to site root. js_file: URL to a file with page specific Javascript code, relative to site root. File should define global object named same as a filename, i.e. '/auth/static/js/api.js' should define global object 'api' that incapsulates functionality implemented in the module. navbar_tab_id: id of a navbar tab to highlight. page_title: title of an HTML page. Args: path: path to a template, relative to templates/. env: additional environment dict to use when rendering the template. status: HTTP status code to return. """ env = (env or {}).copy() env.setdefault('css_file', None) env.setdefault('js_file', None) env.setdefault('navbar_tab_id', None) env.setdefault('page_title', 'Untitled') # This goes to both Jinja2 env and Javascript config object. user = self.get_current_user() common = { 'account_picture': user.picture() if user else None, 'auth_service_config_locked': False, # overridden in auth_service 'is_admin': api.is_admin(), 'login_url': self.create_login_url(self.request.url), 'logout_url': self.create_logout_url('/'), 'using_gae_auth': self.auth_method == handler.gae_cookie_authentication, 'xsrf_token': self.generate_xsrf_token(), } if _ui_data_callback: common.update(_ui_data_callback()) # Name of Javascript module with page code. js_module_name = None if env['js_file']: assert env['js_file'].endswith('.js') js_module_name = os.path.basename(env['js_file'])[:-3] # This will be accessible from Javascript as global 'config' variable. js_config = { 'identity': api.get_current_identity().to_bytes(), } js_config.update(common) # Jinja2 environment to use to render a template. full_env = { 'app_name': _ui_app_name, 'app_revision_url': utils.get_app_revision_url(), 'app_version': utils.get_app_version(), 'config': json.dumps(js_config), 'csp_nonce': self.csp_nonce, 'identity': api.get_current_identity(), 'js_module_name': js_module_name, 'navbar': [(cls.navbar_tab_id, cls.navbar_tab_title, cls.navbar_tab_url) for cls in _ui_navbar_tabs if cls.is_visible()], } full_env.update(common) full_env.update(env) # Render it. self.response.set_status(status) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' self.response.write(template.render(path, full_env))
def reply(self, path, env=None, status=200): """Render template |path| to response using given environment. Optional keys from |env| that base.html uses: css_file: URL to a file with page specific styles, relative to site root. js_file: URL to a file with page specific Javascript code, relative to site root. File should define global object named same as a filename, i.e. '/auth/static/js/api.js' should define global object 'api' that incapsulates functionality implemented in the module. navbar_tab_id: id a navbar tab to highlight. page_title: title of an HTML page. Args: path: path to a template, relative to templates/. env: additional environment dict to use when rendering the template. status: HTTP status code to return. """ env = (env or {}).copy() env.setdefault('css_file', None) env.setdefault('js_file', None) env.setdefault('navbar_tab_id', None) env.setdefault('page_title', 'Untitled') # This goes to both Jinja2 env and Javascript config object. user = self.get_current_user() common = { 'account_picture': user.picture() if user else None, 'auth_service_config_locked': False, # overridden in auth_service 'is_admin': api.is_admin(), 'login_url': self.create_login_url(self.request.url), 'logout_url': self.create_logout_url('/'), 'using_gae_auth': self.auth_method == handler.gae_cookie_authentication, 'xsrf_token': self.generate_xsrf_token(), } if _ui_env_callback: common.update(_ui_env_callback(self)) # Name of Javascript module with page code. js_module_name = None if env['js_file']: assert env['js_file'].endswith('.js') js_module_name = os.path.basename(env['js_file'])[:-3] # This will be accessible from Javascript as global 'config' variable. js_config = { 'identity': api.get_current_identity().to_bytes(), } js_config.update(common) # Prepare URL to explore app API. schema, netloc = urlparse.urlparse(self.request.url)[:2] api_url = ( 'https://apis-explorer.appspot.com/apis-explorer/?' 'base=%s://%s/_ah/api' % (schema, netloc)) # Jinja2 environment to use to render a template. full_env = { 'app_name': _ui_app_name, 'app_revision_url': utils.get_app_revision_url(), 'app_version': utils.get_app_version(), 'config': json.dumps(js_config), 'identity': api.get_current_identity(), 'js_module_name': js_module_name, 'api_url': api_url, 'navbar': [ (cls.navbar_tab_id, cls.navbar_tab_title, cls.navbar_tab_url) for cls in _ui_navbar_tabs if cls.is_visible() ], } full_env.update(common) full_env.update(env) # Render it. self.response.set_status(status) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' self.response.write(template.render(path, full_env))
def reply(self, path, env=None, status=200): """Render template |path| to response using given environment. Optional keys from |env| that base.html uses: css_file: URL to a file with page specific styles, relative to site root. js_file: URL to a file with page specific Javascript code, relative to site root. File should define global object named same as a filename, i.e. '/auth/static/js/api.js' should define global object 'api' that incapsulates functionality implemented in the module. navbar_tab_id: id a navbar tab to highlight. page_title: title of an HTML page. Args: path: path to a template, relative to templates/. env: additional environment dict to use when rendering the template. status: HTTP status code to return. """ env = (env or {}).copy() env.setdefault('css_file', None) env.setdefault('js_file', None) env.setdefault('navbar_tab_id', None) env.setdefault('page_title', 'Untitled') # This goes to both Jinja2 env and Javascript config object. common = { 'login_url': users.create_login_url(self.request.path), 'logout_url': users.create_logout_url('/'), 'xsrf_token': self.generate_xsrf_token(), } # Name of Javascript module with page code. js_module_name = None if env['js_file']: assert env['js_file'].endswith('.js') js_module_name = os.path.basename(env['js_file'])[:-3] # This will be accessible from Javascript as global 'config' variable. js_config = { 'identity': api.get_current_identity().to_bytes(), } js_config.update(common) # Jinja2 environment to use to render a template. full_env = { 'app_name': _ui_app_name, 'app_revision_url': utils.get_app_revision_url(), 'app_version': utils.get_app_version(), 'config': json.dumps(js_config), 'identity': api.get_current_identity(), 'js_module_name': js_module_name, 'navbar': [ (cls.navbar_tab_id, cls.navbar_tab_title, cls.navbar_tab_url) for cls in _ui_navbar_tabs ], } full_env.update(common) full_env.update(env) # Render it. self.response.set_status(status) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' self.response.write(template.render(path, full_env))
def reply(self, path, env=None, status=200): """Render template |path| to response using given environment. Optional keys from |env| that base.html uses: css_file: URL to a file with page specific styles, relative to site root. js_file: URL to a file with page specific Javascript code, relative to site root. File should define global object named same as a filename, i.e. '/auth/static/js/api.js' should define global object 'api' that incapsulates functionality implemented in the module. navbar_tab_id: id a navbar tab to highlight. page_title: title of an HTML page. Args: path: path to a template, relative to templates/. env: additional environment dict to use when rendering the template. status: HTTP status code to return. """ env = (env or {}).copy() env.setdefault("css_file", None) env.setdefault("js_file", None) env.setdefault("navbar_tab_id", None) env.setdefault("page_title", "Untitled") # This goes to both Jinja2 env and Javascript config object. common = { "auth_service_config_locked": False, # overridden in auth_service "login_url": users.create_login_url(self.request.path), "logout_url": users.create_logout_url("/"), "xsrf_token": self.generate_xsrf_token(), } if _ui_env_callback: common.update(_ui_env_callback(self)) # Name of Javascript module with page code. js_module_name = None if env["js_file"]: assert env["js_file"].endswith(".js") js_module_name = os.path.basename(env["js_file"])[:-3] # This will be accessible from Javascript as global 'config' variable. js_config = {"identity": api.get_current_identity().to_bytes()} js_config.update(common) # Prepare URL to explore app API. schema, netloc, _, _, _, _ = urlparse.urlparse(self.request.url) api_url = "https://apis-explorer.appspot.com/apis-explorer/?" "base=%s://%s/_ah/api" % (schema, netloc) # Jinja2 environment to use to render a template. full_env = { "app_name": _ui_app_name, "app_revision_url": utils.get_app_revision_url(), "app_version": utils.get_app_version(), "config": json.dumps(js_config), "identity": api.get_current_identity(), "js_module_name": js_module_name, "api_url": api_url, "navbar": [(cls.navbar_tab_id, cls.navbar_tab_title, cls.navbar_tab_url) for cls in _ui_navbar_tabs], } full_env.update(common) full_env.update(env) # Render it. self.response.set_status(status) self.response.headers["Content-Type"] = "text/html; charset=utf-8" self.response.write(template.render(path, full_env))