Example #1
0
 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 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)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
 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)
Example #8
0
 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)