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 get(self, request, *args, **kwargs): """Sets up datastore, if it's not already set up.""" del request, args, kwargs # unused if self.env.config.get('initialized'): return self.error(400) setup_pf.setup_datastore() return django.shortcuts.redirect(self.build_absolute_uri('/'))
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 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 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)