Ejemplo n.º 1
0
    def get_display(my):
        my.sobject = my.get_current_sobject()
        if not my.sobject:
            my.sobject = my.get_sobject_from_kwargs()

        if my.sobject and my.sobject.is_insert():
            return DivWdg()



        if my.sobject:
            my.search_key = SearchKey.get_by_sobject(my.sobject)
            my.kwargs['search_key'] = my.search_key

        else:
            my.search_key = my.kwargs.get('search_key')


        html = my.kwargs.get('html')
        if not html:
            html = ""

        # DEPRECATED
        my.state = my.kwargs.get("state")
        my.state = BaseRefreshWdg.process_state(my.state)
        if not my.state:
            my.state = my.kwargs
            my.state['search_key'] = my.search_key



        my.view = my.kwargs.get('view')
        my.view = my.view.replace("/", ".")
        my.view_folder = ""

        if my.view.startswith("."):
            my.view_folder = my.kwargs.get("__view_folder__")
            if my.view_folder:
                my.view = "%s%s" % (my.view_folder, my.view)

        parts = my.view.split(".")
        my.view_folder = ".".join(parts[:-1])



        if not my.view and not html:
            raise TacticException("No view defined in custom layout")

        # If html is not a string, then convert it?
        if not isinstance(html, basestring):
            html = str(html)

        my.view_attrs = {}

        my.category = my.kwargs.get("category")
        my.search_type = my.kwargs.get("search_type")

        my.encoding = my.kwargs.get("encoding")
        if not my.encoding:
             my.encoding = 'utf-8'
        my.plugin = None

        xml = None

        
        # if html is not provided, then get it from the config
        config = None
        if not html:

            if my.config != None:
                config = my.config
            else:
                config = my.kwargs.get("config")
                if not config:
                    config = my.get_config()



            if not config:
                #div = DivWdg()
                #div.add("No config defined for view [%s] for custom layout" % my.view)
                #return div
                raise TacticException("No config defined for view [%s] for custom layout" % my.view)

            if isinstance(config, WidgetDbConfig):
                config_str = config.get_value("config")
            else:
                config_str = ''

            if config_str.startswith("<html>"):
                html = config_str
                my.def_config = None
            else:
                xml = config.get_xml()

                if my.def_config == None:
                    my.def_config = my.get_def_config(xml)

                # get the view attributes
                if isinstance(config, WidgetConfigView):
                    top_config = config.get_configs()[0]
                else:
                    top_config = config
                view_node = top_config.get_view_node()
                if view_node is None:
                    div = DivWdg("No view node found in xml. Invalid XML entry found")
                    return div
                my.view_attrs = xml.get_attributes(view_node)

                nodes = xml.get_nodes("config/%s/html/*" % my.view)
                if not nodes:
                    div = DivWdg("No definition found")
                    return div

                # convert html tag to a div
                html = cStringIO.StringIO()
                for node in nodes:
                    # unfortunately, html does not recognize <textarea/>
                    # so we have to make sure it becomes <textarea></textarea>
                    text = xml.to_string(node)
                    text = text.encode('utf-8')
                    keys = ['textarea','input']
                    for key in keys:
                        p = re.compile("(<%s.*?/>)" % key)
                        m = p.search(text)
                        if m:
                            for group in m.groups():
                                xx = group.replace("/", "")
                                xx = "%s</%s>" % (xx, key)
                                text = text.replace(group, xx)

                        text = text.replace("<%s/>" % key, "<%s></%s>" % (key, key))

                    # add linebreaks to element tag
                    key = 'element'

                    # reg full tag <element><display...></element>
                    p = re.compile(r"(<%s\b[^>]*>(?:.*?)</%s>)" % (key, key))
                    # short-hand tag <element/>
                    p1 =  re.compile("(</%s>|<%s.*?/>)" %(key, key))
                    m = p.search(text)
                    m1 = p1.search(text)
                    if m:
                        for group in m.groups():
                            if group:
                                text = text.replace(group, '\n%s\n'%group)
                    if m1:
                        for group in m1.groups():
                            if group:
                                text = text.replace(group, '\n%s\n'%group)
                       
                    html.write(text)

                html = html.getvalue()


        my.config = config
        #my.def_config = config    # This is unnessary?

        # try to get the sobject if this is in a table element widget
        if my.search_key:
            try:
                # this will raise an exception if it is not in a table element
                sobject = my.get_current_sobject()
            except:
	        sobject = SearchKey.get_by_search_key(my.search_key)
            sobjects = [sobject]
        else:
            try:
                # this will raise an exception if it is not in a table element
                sobject = my.get_current_sobject()
                if sobject:
                    sobjects = [sobject]
                else:
                    sobjects = []
            except:
                sobject = my.sobjects


        my.layout = my.get_layout_wdg()



        # preprocess using mako
        include_mako = my.kwargs.get("include_mako")
        if not include_mako:
            include_mako = my.view_attrs.get("include_mako")


        if xml:
            mako_node = xml.get_node("config/%s/mako" % my.view)
            if mako_node is not None:
                mako_str = xml.get_node_value(mako_node)
                html = "<%%\n%s\n%%>\n%s" % (mako_str, html)



        from pyasm.web import Palette
        num_palettes = Palette.num_palettes()


        #if include_mako in ['true', True]:
        if include_mako not in ['false', False]:
            html = html.replace("&lt;", "<")
            html = html.replace("&gt;", ">")

            html = my.process_mako(html)



        # preparse out expressions

        # use relative expressions - [expr]xxx[/expr]
        p = re.compile('\[expr\](.*?)\[\/expr\]')
        parser = ExpressionParser()
        matches = p.finditer(html)
        for m in matches:
            full_expr = m.group()
            expr = m.groups()[0]
            result = parser.eval(expr, sobjects, single=True, state=my.state)
            if isinstance(result, basestring):
                result = Common.process_unicode_string(result)
            else:
                result = str(result)
            html = html.replace(full_expr, result )


        # use absolute expressions - [expr]xxx[/expr]
        p = re.compile('\[abs_expr\](.*?)\[\/abs_expr\]')
        parser = ExpressionParser()
        matches = p.finditer(html)
        for m in matches:
            full_expr = m.group()
            expr = m.groups()[0]
            result = parser.eval(expr, single=True)
            if isinstance(result, basestring):
                result = Common.process_unicode_string(result)
            else:
                result = str(result)
            html = html.replace(full_expr, result )



        # need a top widget that can be used to refresh
        top = my.top
        my.set_as_panel(top)
        top.add_class("spt_custom_top")

        ignore_events = my.kwargs.get("ignore_events") in ['true', True]

        if ignore_events:
            top.add_style("pointer-events: none")


        # create the content div
        content = DivWdg()
        content.add_class("spt_custom_content")
        content.add_style("position: relative")
        if ignore_events:
            content.add_style("pointer-events: none")
        top.add(content)
        my.content = content


        is_test = Container.get("CustomLayout::is_test")
        if not is_test:
            is_test = my.kwargs.get("is_test") in [True, 'true']

        if is_test:
            Container.put("CustomLayout::is_test", True)
            my.handle_is_test(content)



        html = my.replace_elements(html)
        content.add(html)

        if xml:
            my.add_behaviors(content, xml)


        # remove all the extra palettes created
        while True:
            extra_palettes = Palette.num_palettes() - num_palettes
            if extra_palettes > 0:
                Palette.pop_palette()
            else:
                break

            
        if my.kwargs.get("is_top") in ['true', True]:
            return html

        elif my.kwargs.get("is_refresh"):
            return content
        else:
            return top
Ejemplo n.º 2
0
    def get_display(my):
        my.sobject = my.get_current_sobject()
        if not my.sobject:
            my.sobject = my.get_sobject_from_kwargs()

        if my.sobject and my.sobject.is_insert():
            return DivWdg()



        if my.sobject:
            my.search_key = SearchKey.get_by_sobject(my.sobject)
            my.kwargs['search_key'] = my.search_key

        else:
            my.search_key = my.kwargs.get('search_key')


        html = my.kwargs.get('html')
        if not html:
            html = ""

        # DEPRECATED
        my.state = my.kwargs.get("state")
        my.state = BaseRefreshWdg.process_state(my.state)
        if not my.state:
            my.state = my.kwargs
            my.state['search_key'] = my.search_key



        my.view = my.kwargs.get('view')
        my.view = my.view.replace("/", ".")
        my.view_folder = ""

        if my.view.startswith("."):
            my.view_folder = my.kwargs.get("__view_folder__")
            if my.view_folder:
                my.view = "%s%s" % (my.view_folder, my.view)

        parts = my.view.split(".")
        my.view_folder = ".".join(parts[:-1])



        if not my.view and not html:
            raise TacticException("No view defined in custom layout")

        # If html is not a string, then convert it?
        if not isinstance(html, basestring):
            html = str(html)

        my.view_attrs = {}

        my.category = my.kwargs.get("category")
        my.search_type = my.kwargs.get("search_type")

        my.encoding = my.kwargs.get("encoding")
        if not my.encoding:
             my.encoding = 'utf-8'
        my.plugin = None

        xml = None

        
        # if html is not provided, then get it from the config
        config = None
        if not html:

            if my.config != None:
                config = my.config
            else:
                config = my.kwargs.get("config")
                if not config:
                    config = my.get_config()



            if not config:
                #div = DivWdg()
                #div.add("No config defined for view [%s] for custom layout" % my.view)
                #return div
                raise TacticException("No config defined for view [%s] for custom layout" % my.view)

            if isinstance(config, WidgetDbConfig):
                config_str = config.get_value("config")
            else:
                config_str = ''

            if config_str.startswith("<html>"):
                html = config_str
                my.def_config = None
            else:
                xml = config.get_xml()

                if my.def_config == None:
                    my.def_config = my.get_def_config(xml)

                # get the view attributes
                if isinstance(config, WidgetConfigView):
                    top_config = config.get_configs()[0]
                else:
                    top_config = config
                view_node = top_config.get_view_node()
                if view_node is None:
                    div = DivWdg("No view node found in xml. Invalid XML entry found")
                    return div
                my.view_attrs = xml.get_attributes(view_node)

                nodes = xml.get_nodes("config/%s/html/*" % my.view)
                if not nodes:
                    div = DivWdg("No definition found")
                    return div

                # convert html tag to a div
                html = cStringIO.StringIO()
                for node in nodes:
                    # unfortunately, html does not recognize <textarea/>
                    # so we have to make sure it becomes <textarea></textarea>
                    text = xml.to_string(node)
                    text = text.encode('utf-8')
                    keys = ['textarea','input']
                    for key in keys:
                        p = re.compile("(<%s.*?/>)" % key)
                        m = p.search(text)
                        if m:
                            for group in m.groups():
                                xx = group.replace("/", "")
                                xx = "%s</%s>" % (xx, key)
                                text = text.replace(group, xx)

                        text = text.replace("<%s/>" % key, "<%s></%s>" % (key, key))

                    # add linebreaks to element tag
                    key = 'element'

                    # reg full tag <element><display...></element>
                    p = re.compile(r"(<%s\b[^>]*>(?:.*?)</%s>)" % (key, key))
                    # short-hand tag <element/>
                    p1 =  re.compile("(</%s>|<%s.*?/>)" %(key, key))
                    m = p.search(text)
                    m1 = p1.search(text)
                    if m:
                        for group in m.groups():
                            if group:
                                text = text.replace(group, '\n%s\n'%group)
                    if m1:
                        for group in m1.groups():
                            if group:
                                text = text.replace(group, '\n%s\n'%group)
                       
                    html.write(text)

                html = html.getvalue()


        my.config = config
        #my.def_config = config    # This is unnessary?

        # try to get the sobject if this is in a table element widget
        if my.search_key:
            try:
                # this will raise an exception if it is not in a table element
                sobject = my.get_current_sobject()
            except:
	        sobject = SearchKey.get_by_search_key(my.search_key)
            sobjects = [sobject]
        else:
            try:
                # this will raise an exception if it is not in a table element
                sobject = my.get_current_sobject()
                if sobject:
                    sobjects = [sobject]
                else:
                    sobjects = []
            except:
                sobject = my.sobjects


        my.layout = my.get_layout_wdg()



        # preprocess using mako
        include_mako = my.kwargs.get("include_mako")
        if not include_mako:
            include_mako = my.view_attrs.get("include_mako")


        if xml:
            mako_node = xml.get_node("config/%s/mako" % my.view)
            if mako_node is not None:
                mako_str = xml.get_node_value(mako_node)
                html = "<%%\n%s\n%%>\n%s" % (mako_str, html)



        from pyasm.web import Palette
        num_palettes = Palette.num_palettes()


        #if include_mako in ['true', True]:
        if include_mako not in ['false', False]:
            html = html.replace("&lt;", "<")
            html = html.replace("&gt;", ">")

            html = my.process_mako(html)



        # preparse out expressions

        # use relative expressions - [expr]xxx[/expr]
        p = re.compile('\[expr\](.*?)\[\/expr\]')
        parser = ExpressionParser()
        matches = p.finditer(html)
        for m in matches:
            full_expr = m.group()
            expr = m.groups()[0]
            result = parser.eval(expr, sobjects, single=True, state=my.state)
            if isinstance(result, basestring):
                result = Common.process_unicode_string(result)
            else:
                result = str(result)
            html = html.replace(full_expr, result )


        # use absolute expressions - [expr]xxx[/expr]
        p = re.compile('\[abs_expr\](.*?)\[\/abs_expr\]')
        parser = ExpressionParser()
        matches = p.finditer(html)
        for m in matches:
            full_expr = m.group()
            expr = m.groups()[0]
            result = parser.eval(expr, single=True)
            if isinstance(result, basestring):
                result = Common.process_unicode_string(result)
            else:
                result = str(result)
            html = html.replace(full_expr, result )



        # need a top widget that can be used to refresh
        top = my.top
        my.set_as_panel(top)
        top.add_class("spt_custom_top")


        # create the content div
        content = DivWdg()
        content.add_class("spt_custom_content")
        content.add_style("position: relative")
        top.add(content)
        my.content = content


        is_test = Container.get("CustomLayout::is_test")
        if not is_test:
            is_test = my.kwargs.get("is_test") in [True, 'true']

        if is_test:
            Container.put("CustomLayout::is_test", True)
            my.handle_is_test(content)



        html = my.replace_elements(html)
        content.add(html)

        if xml:
            my.add_behaviors(content, xml)


        # remove all the extra palettes created
        while True:
            extra_palettes = Palette.num_palettes() - num_palettes
            if extra_palettes > 0:
                Palette.pop_palette()
            else:
                break


        if my.kwargs.get("is_refresh"):
            return content
        else:
            return top