def serve(self): request, response, env = self.request, self.response, self.env # If the Person Finder instance has not been initialized yet, # prepend to any served page a warning and a link to the admin # page where the datastore can be initialized. if not env.config.get('initialized'): if request.get('operation') == 'setup_datastore': setup_pf.setup_datastore() self.redirect(env.global_url + '/') return else: get_vars = lambda: {'env': env} content = resources.get_rendered('setup_datastore.html', env.lang, (env.repo, env.charset), get_vars) response.out.write(content) if env.config.get('enable_react_ui'): # TODO(nworden): serve static files from /global/static if env.repo == 'static': self.serve_static_content(self.env.action) elif self.should_serve_react_ui(): csp_nonce = self.set_content_security_policy() react_env = { 'maps_api_key': env.config.get('maps_api_key'), } json_encoder = simplejson.encoder.JSONEncoder() response.out.write( resources.get_rendered( 'react_index.html', env.lang, get_vars=lambda: { 'env': env, 'csp_nonce': csp_nonce, 'env_json': json_encoder.encode(react_env), })) return if not env.action and not env.repo: # A request for the root path ('/'). Renders the home page. self.serve_static_content(HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)(request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') else: self.serve_static_content(self.env.action)
def test_set_active_bundle_name(self): # Verifies that get_localized and get_rendered are properly affected # by set_active_bundle_name. self.put_resource('1', 'xyz', 0, 'one') self.put_resource('2', 'xyz', 0, 'two') assert resources.get_localized('xyz', 'en').content == 'one' assert resources.get_rendered('xyz', 'en') == 'one' resources.set_active_bundle_name('2') assert resources.get_localized('xyz', 'en').content == 'two' assert resources.get_rendered('xyz', 'en') == 'two' resources.set_active_bundle_name('1') assert resources.get_localized('xyz', 'en').content == 'one' assert resources.get_rendered('xyz', 'en') == 'one'
def test_set_active_bundle_name(self): # Verifies that get_localized and get_rendered are properly affected # by set_active_bundle_name. self.put_resource("1", "xyz", 0, "one") self.put_resource("2", "xyz", 0, "two") assert resources.get_localized("xyz", "en").content == "one" assert resources.get_rendered("xyz", "en") == "one" resources.set_active_bundle_name("2") assert resources.get_localized("xyz", "en").content == "two" assert resources.get_rendered("xyz", "en") == "two" resources.set_active_bundle_name("1") assert resources.get_localized("xyz", "en").content == "one" assert resources.get_rendered("xyz", "en") == "one"
def serve(self): request, response, env = self.request, self.response, self.env # If the Person Finder instance has not been initialized yet, # prepend to any served page a warning and a link to the admin # page where the datastore can be initialized. if not config.get('initialized'): if request.get('operation') == 'setup_datastore': setup_pf.setup_datastore() self.redirect(env.global_url + '/') return else: get_vars = lambda: {'env': env} content = resources.get_rendered('setup_datastore.html', env.lang, (env.repo, env.charset), get_vars) response.out.write(content) if not env.action and not env.repo: # Redirect to the default home page. self.redirect(env.global_url + '/' + HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)(request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') elif env.action in BLACKLISTED_ACTIONS_FOR_STATIC_SERVING: response.set_status(404) response.out.write('Not found') else: # Serve a static page or file. env.robots_ok = True get_vars = lambda: {'env': env, 'config': env.config} content = resources.get_rendered(env.action, env.lang, (env.repo, env.charset), get_vars) if content is None: response.set_status(404) response.out.write('Not found') else: content_type, encoding = mimetypes.guess_type(env.action) response.headers['Content-Type'] = ( (content_type or 'text/plain') + ('; charset=%s' % encoding if encoding else '')) response.out.write(content)
def render(self, template_name, status_code=200, **template_vars): """Renders a template with the given variables. Args: template_name (str): The filename of the template in app/resources. **template_vars: Named variables to pass to the template. Returns: HttpResponse: An HttpResponse with the rendered template. """ def get_vars(): """A function returning vars, for use by the resources module.""" template_vars['env'] = self.env # TODO(nworden): change templates to access config through env, # which already has the config anyway template_vars['config'] = self.env.config template_vars['params'] = self.params template_vars['csp_nonce'] = self.request.csp_nonce return template_vars query_str = self.request.META.get('QUERY_STRING', '') extra_key = (self.env.repo, self.env.charset, query_str) return django.http.HttpResponse(resources.get_rendered( template_name, self.env.lang, extra_key, get_vars, 0), status=status_code)
def serve(self): request, response, env = self.request, self.response, self.env # If the Person Finder instance has not been initialized yet, # prepend to any served page a warning and a link to the admin # page where the datastore can be initialized. if not config.get('initialized'): if request.get('operation') == 'setup_datastore': setup_pf.setup_datastore() self.redirect(env.global_url + '/') return else: get_vars = lambda: {'env': env} content = resources.get_rendered('setup_datastore.html', env.lang, (env.repo, env.charset), get_vars) response.out.write(content) if not env.action and not env.repo: # A request for the root path ('/'). Renders the home page. self.serve_static_content(HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)(request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') else: self.serve_static_content(self.env.action)
def serve(self): request, response, env = self.request, self.response, self.env if not env.action and not env.repo: # Redirect to the default home page. self.redirect(env.global_url + '/' + HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)(request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') else: # Serve a static page or file. env.robots_ok = True get_vars = lambda: {'env': env, 'config': env.config} content = resources.get_rendered(env.action, env.lang, (env.repo, env.charset), get_vars) if content is None: response.set_status(404) response.out.write('Not found') else: content_type, encoding = mimetypes.guess_type(env.action) response.headers['Content-Type'] = content_type or 'text/plain' response.out.write(content)
def render_to_string(self, name, language_override=None, cache_seconds=0, get_vars=lambda: {}, **vars): """Renders a template to a string, passing in the variables specified in **vars as well as any additional variables returned by get_vars(). Since this is intended for use by a dynamic page handler, caching is off by default; if cache_seconds is positive, then get_vars() will be called only when cached content is unavailable.""" # TODO(kpy): Make the contents of extra_key overridable by callers? lang = language_override or self.env.lang extra_key = (self.env.repo, self.env.charset, self.request.query_string) def get_all_vars(): vars.update(get_vars()) for key in ('env', 'config', 'params'): if key in vars: raise Exception( 'Cannot use "%s" as a key in vars. It is reserved.' % key) vars['env'] = self.env # pass along application-wide context vars['config'] = self.config # pass along the configuration vars['params'] = self.params # pass along the query parameters return vars return resources.get_rendered(name, lang, extra_key, get_all_vars, cache_seconds)
def serve(self): request, response, env = self.request, self.response, self.env if not env.action and not env.repo: # Redirect to the default home page. self.redirect(env.global_url + '/' + HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)( request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') else: # Serve a static page or file. env.robots_ok = True get_vars = lambda: {'env': env, 'config': env.config} content = resources.get_rendered( env.action, env.lang, (env.repo, env.charset), get_vars) if content is None: response.set_status(404) response.out.write('Not found') else: content_type, encoding = mimetypes.guess_type(env.action) response.headers['Content-Type'] = content_type or 'text/plain' response.out.write(content)
def render(self, template_name, status_code=200, **template_vars): """Renders a template with the given variables. Args: template_name (str): The filename of the template in app/resources. **template_vars: Named variables to pass to the template. Returns: HttpResponse: An HttpResponse with the rendered template. """ def get_vars(): """A function returning vars, for use by the resources module.""" template_vars['env'] = self.env # TODO(nworden): change templates to access config through env, # which already has the config anyway template_vars['config'] = self.env.config template_vars['params'] = self.params template_vars['csp_nonce'] = self.request.csp_nonce return template_vars query_str = self.request.META.get('QUERY_STRING', '') extra_key = (self.env.repo, self.env.charset, query_str) return django.http.HttpResponse( resources.get_rendered(template_name, self.env.lang, extra_key, get_vars, 0), status=status_code)
def serve(self): request, response, env = self.request, self.response, self.env # If the Person Finder instance has not been initialized yet, # prepend to any served page a warning and a link to the admin # page where the datastore can be initialized. if not config.get('initialized'): if request.get('operation') == 'setup_datastore': setup_pf.setup_datastore() self.redirect(env.global_url + '/') return else: get_vars = lambda: {'env': env} content = resources.get_rendered('setup_datastore.html', env.lang, (env.repo, env.charset), get_vars) response.out.write(content) if not env.action and not env.repo: # Redirect to the default home page. self.redirect(env.global_url + '/' + HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)( request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') else: # Serve a static page or file. env.robots_ok = True get_vars = lambda: {'env': env, 'config': env.config} content = resources.get_rendered( env.action, env.lang, (env.repo, env.charset), get_vars) if content is None: response.set_status(404) response.out.write('Not found') else: content_type, encoding = mimetypes.guess_type(env.action) response.headers['Content-Type'] = ( (content_type or 'text/plain') + ('; charset=%s' % encoding if encoding else '')) response.out.write(content)
def serve(self): request, response, env = self.request, self.response, self.env # If the Person Finder instance has not been initialized yet, # prepend to any served page a warning and a link to the admin # page where the datastore can be initialized. if not env.config.get('initialized'): if request.get('operation') == 'setup_datastore': setup_pf.setup_datastore() self.redirect(env.global_url + '/') return else: get_vars = lambda: {'env': env} content = resources.get_rendered('setup_datastore.html', env.lang, (env.repo, env.charset), get_vars) response.out.write(content) if env.config.get('enable_react_ui'): # TODO(nworden): serve static files from /global/static if env.repo == 'static': self.serve_static_content(self.env.action) elif self.should_serve_react_ui(): csp_nonce = self.set_content_security_policy() response.out.write( resources.get_rendered( 'react_index.html', env.lang, get_vars=lambda: {'env': env, 'csp_nonce': csp_nonce})) return if not env.action and not env.repo: # A request for the root path ('/'). Renders the home page. self.serve_static_content(HOME_ACTION) elif env.action in HANDLER_CLASSES: # Dispatch to the handler for the specified action. module_name, class_name = HANDLER_CLASSES[env.action].split('.') handler = getattr(__import__(module_name), class_name)( request, response, env) getattr(handler, request.method.lower())() # get() or post() elif env.action.endswith('.template'): # Don't serve template source code. response.set_status(404) response.out.write('Not found') else: self.serve_static_content(self.env.action)
def get(self, request, *args, **kwargs): """Serves get requests with configurable static files.""" del request, args # unused filename = kwargs['filename'] if filename not in CONFIGURABLE_STATIC_FILES: return self.error(404) fileinfo = CONFIGURABLE_STATIC_FILES[filename] return django.http.HttpResponse(resources.get_rendered( 'static/configurable/%s' % filename, self.env.lang), content_type=fileinfo.content_type)
def serve_static_content(self, resource_name): """Serve a static page or file in app/resources/static directory.""" response, env = self.response, self.env env.robots_ok = True get_vars = lambda: {'env': env, 'config': env.config} content = resources.get_rendered('static/%s' % resource_name, env.lang, (env.repo, env.charset), get_vars) if content is None: response.set_status(404) response.out.write('Not found') else: content_type, encoding = mimetypes.guess_type(resource_name) response.headers['Content-Type'] = ( (content_type or 'text/plain') + ('; charset=%s' % encoding if encoding else '')) response.out.write(content)
def render_to_string(self, name, language_override=None, cache_seconds=0, get_vars=lambda: {}, **vars): """Renders a template to a string, passing in the variables specified in **vars as well as any additional variables returned by get_vars(). Since this is intended for use by a dynamic page handler, caching is off by default; if cache_seconds is positive, then get_vars() will be called only when cached content is unavailable.""" # TODO(kpy): Make the contents of extra_key overridable by callers? lang = language_override or self.env.lang extra_key = (self.env.repo, self.env.charset, self.request.query_string) def get_all_vars(): vars['env'] = self.env # pass along application-wide context vars['config'] = self.config # pass along the configuration vars['params'] = self.params # pass along the query parameters vars.update(get_vars()) return vars return resources.get_rendered( name, lang, extra_key, get_all_vars, cache_seconds)
def serve_static_content(self, resource_name): """Serve a static page or file in app/resources/static directory.""" response, env = self.response, self.env env.robots_ok = True get_vars = lambda: {'env': env, 'config': env.config} content = resources.get_rendered( 'static/%s' % resource_name, env.lang, (env.repo, env.charset), get_vars) if content is None: response.set_status(404) response.out.write('Not found') else: content_type, encoding = mimetypes.guess_type(resource_name) response.headers['Content-Type'] = ( (content_type or 'text/plain') + ('; charset=%s' % encoding if encoding else '')) response.out.write(content)