Exemplo n.º 1
0
    def __call__(self, environ, start_response):
        rl = core.request_local()
        rl.clear()
        rl['middleware'] = self
        req = wo.Request(environ)

        path = req.path_info
        if self.config.serve_resources and \
           path.startswith(self.config.res_prefix):
            return self.resources(environ, start_response)
        else:
            if self.config.serve_controllers and \
               path.startswith(self.config.controller_prefix):
                resp = self.controllers(req)
            else:
                if self.app:
                    resp = req.get_response(self.app, catch_exc_info=True)
                else:
                    resp = wo.Response(status="404 Not Found")

            ct = resp.headers.get('Content-Type', 'text/plain').lower()

            should_inject = (
                self.config.inject_resources
                and 'html' in ct
                and not isinstance(resp.app_iter, types.GeneratorType)
            )
            if should_inject:
                body = inject_resources(
                    resp.body,
                    encoding=resp.charset,
                )
                if isinstance(body, unicode):
                    resp.unicode_body = body
                else:
                    resp.body = body
        core.request_local().clear()
        return resp(environ, start_response)
Exemplo n.º 2
0
    def __call__(self, html, resources=None, encoding=None):
        if resources is None:
            resources = core.request_local().get('resources', None)
        if resources:
            toadd = ''
            popit = []
            for res in resources:
                if 'JSLink' in str(res):
                    # avoid jquery javascript to not override the main one
                    if not 'jquery' in res.filename:
                        if res.filename == 'static/dynforms.js':
                            # replace dynfom by a patched one because some javascript is not well written
                            resource_path = os.path.join(resource_filename('bs', 'public'), 'javascript', 'patched_dynforms.js')
                        else:
                            resource_path = os.path.join(resource_filename(res.modname, ''), res.filename)
                         # some debug
                        # toadd += '<script type="text/javascript">'
                        # toadd += 'console.log("Log from custom middleware.");'
                        # toadd += 'console.log("{}");'.format(resource_path)
                        # toadd += '</script>'
                        with open(resource_path, 'r') as resource_file:
                            toadd += '<script type="text/javascript">'
                            toadd += resource_file.read()
                            toadd += '</script>'
                    popit.append(res)
            for topop in popit:
                resources.remove(topop)
            encoding = encoding or find_charset(html) or 'utf-8'
            # add css
            html = util.MultipleReplacer.__call__(
                self, html, resources, encoding
            )

            # add toadd (js files)
            one, two, three = htmlfilepattern.match(html).groups()
            html = one + two + toadd + three
            core.request_local().pop('resources', None)
        return html