コード例 #1
0
ファイル: view.py プロジェクト: skarphed/skarphed
    def render(self, environ):
        View.set_currently_rendering_view(self)
        frame = """
        <!DOCTYPE html>
        <html>
          <head>
            <title>%(title)s</title>
            <link href="/static/%(page_css)s" rel="stylesheet" type="text/css">
            <link href="%(scv_css)s" rel="stylesheet" type="text/css">
            %(head)s
            <script type="text/javascript">%(ajax_script)s</script>
          </head>
          <body>
            %(body)s
          </body>
        </html>
        """
        js_frame = """<script type="text/javascript" id="%d_scr">%s</script>"""
        page = Page.get_page(self._page) 

        head = page.get_html_head()
        body = page.get_html_body()

        # Find placeholders to substitute
        
        space_name_map = page.get_space_names()
        for space, widget_id in self._space_widget_mapping.items():
            space_name = space_name_map[space]
            widget = ModuleManager.get_widget(widget_id)

            args = {} 
            if self._widget_param_mapping.has_key(widget_id):
                args.update(self._widget_param_mapping[widget_id])
            elif self._widget_param_mapping.has_key(str(widget_id)):
                args.update(self._widget_param_mapping[str(widget_id)])
            
            if self._post_widget_id == widget_id:
                # Check whether the viewjson-string is included here, too:
                # if so, eliminate it.
                post_args = FieldStorage(fp=environ['wsgi.input'],environ=environ)
                for key in post_args.keys():
                    args[key] = post_args[key].value

            widget_html = widget.render_html(args)
            widget_js = widget.render_javascript(args)
            widget_html += js_frame%(widget.get_id(), widget_js)
            body = re.sub(r"<%%\s?space:%s\s?%%>"%space_name,widget_html,body)

        for box, boxcontent in self._box_mapping.items():
            box_orientation, box_name = self.get_box_info(box)
            box_html = StringIO.StringIO()
            for widget_id in boxcontent:
                widget = ModuleManager.get_widget(widget_id)

                args = {} 
                if self._widget_param_mapping.has_key(widget_id):
                    args.update(self._widget_param_mapping[widget_id])
                elif self._widget_param_mapping.has_key(str(widget_id)):
                    args.update(self._widget_param_mapping[str(widget_id)])

                if self._post_widget_id == widget_id:
                    # Check whether the viewjson-string is included here, too:
                    # if so, eliminate it.
                    post_args = FieldStorage(fp=environ['wsgi.input'],environ=environ)
                    for key in post_args.keys():
                        args[key] = post_args[key].value
            
                widget_html = widget.render_html(args)
                widget_js = widget.render_javascript(args)
                widget_html += js_frame%(widget.get_id(), widget_js)
                box_html.write(widget_html)
                if box_orientation == BoxOrientation.VERTICAL:
                    box_html.write("<br>")

            if box_orientation == BoxOrientation.HORIZONTAL:
                body = re.sub(r"<%%\s?hbox:%s\s?%%>"%box_name,box_html.getvalue(),body)
            elif box_orientation == BoxOrientation.VERTICAL:
                body = re.sub(r"<%%\s?vbox:%s\s?%%>"%box_name,box_html.getvalue(),body)

        body = re.sub(r"<%[^%>]+%>","",body) #Replace all unused spaces with emptystring

        css_manager = CSSManager()
        css_url = css_manager.get_css_url()

        configuration = Configuration()
        title = configuration.get_entry("core.name")

        page_css = page.get_css_filename()
        View.set_currently_rendering_view(None)
        return frame%{'title':title,
                      'scv_css':css_url,
                      'page_css':page_css,
                      'ajax_script':AJAXScript,
                      'head':head,
                      'body':body}