示例#1
0
    def renderResponse(self, request):
        request = self.request or request

        ui = self.buildUI(request)
        self.initUI(ui, request)
        if self.autoReload:
            ui.clientSide(
                self.clientSide.get(silent=self.silentReload,
                                    timeout=self.autoReload))

        if request.method == "GET" and self.validGet(ui, request):
            self.processGet(ui, request)
        elif request.method == "POST" and self.validPost(ui, request):
            self.processPost(ui, request)
        elif request.method == "DELETE" and self.validDelete(ui, request):
            self.processDelete(ui, request)
        elif request.method == "PUT" and self.validPut(ui, request):
            self.processPut(ui, request)

        self.setUIData(ui, request)
        if not self.canEdit(request):
            ui.setEditable(False)
        if not request.response.scripts:
            request.response.scripts = ScriptContainer()
            ui.setScriptContainer(request.response.scripts)
            return ui.toHTML(
                request=request) + request.response.scripts.toHTML(
                    request=request)
        else:
            ui.setScriptContainer(request.response.scripts)
            return ui.toHTML(request=request)
示例#2
0
    def __init__(self,
                 id=None,
                 name=None,
                 parent=None,
                 parentHandler=None,
                 initScripts=None,
                 request=None,
                 **kwargs):
        if parentHandler:
            self.elementFactory = parentHandler.elementFactory

        RequestHandler.__init__(self,
                                parentHandler=parentHandler,
                                initScripts=initScripts)
        WebElement.__init__(self,
                            id=id or self.accessor,
                            name=name,
                            parent=None,
                            **kwargs)
        self.setPrefix("")
        self.attributes['handler'] = self.accessor
        self.request = request

        self._loading = self.Loading(self.id + ":Loading",
                                     self.name + ":Loading",
                                     parent=self,
                                     hide=True).toHTML()

        self.initScripts.append(
            self.setScriptContainer(ScriptContainer()).content())
def getTemplateGenerationTimes():
    template = "div#AllProducts\n"
    templateElements = []
    for product in Factory.products.keys():
        template += "  > %(product)s#test%(product)s\n" % {'product': product}
    template = UITemplate.fromSHPAML(template)

    startTime = time.time()
    for x in xrange(100):
        templateElement = TemplateElement(template=template, factory=Factory)
        templateElement.setScriptContainer(
            templateElement.addChildElement(ScriptContainer()))
        templateElements.append(templateElement)
        doneSection()
    results['templateInit'] = time.time() - startTime

    html = ""
    startTime = time.time()
    for templateElement in templateElements:
        html += templateElement.toHTML()
        doneSection()

    generationTime = (time.time() - startTime)
    results['templateToHtml'] = generationTime
    results['templateToHtmlSize'] = len(html)
    results[
        'templateCreate'] = results['templateInit'] + results['templateToHtml']
示例#4
0
    def renderResponse(self, request):
        """
            Override the response rendering to render the main document structure of the page
        """
        document = self.elementFactory.buildFromTemplate(self.template)
        request.response.scripts = ScriptContainer()
        request.response.scripts.addScript("\n".join(self.initScripts))
        document.setScriptContainer(request.response.scripts)
        document.setProperty('title', self.title(request))
        document.addChildElement(ResourceFile()).setProperty(
            "file", self.favicon(request))
        for resourceFile in set(
                self.requestResourceFiles(request) + self.resourceFiles):
            document.addChildElement(ResourceFile()).setProperty(
                "file", resourceFile)

        if csrf:
            token = document.body.addChildElement(
                HiddenValue('csrfmiddlewaretoken'))
            token.setValue(csrf(request.native)['csrf_token'])
        document.body += self.mainControl
        document.body += request.response.scripts

        self.modifyDocument(document, request)

        return document.toHTML(formatted=self.formatted, request=request)
def getSingleElementGenerationTimes():
    generationTimes = OrderedDict()
    for product in Factory.products.keys():
        if "." in product:
            continue
        doneSection()
        startTime = time.time()
        scripts = ScriptContainer()
        element = Factory.build(product, 'Test', 'Product')
        element.setScriptContainer(scripts)
        html = element.toHtml()
        html += scripts.toHtml()

        generationTime = time.time() - startTime
        results['createAllOnce'] += generationTime
        generationTimes[generationTime] = (product, len(html))
    results['longestCreationTime'] = generationTimes.orderedKeys[-1]
    return generationTimes
def getSingleElementGenerationTimes():
    generationTimes = DictUtils.OrderedDict()
    for product in Factory.products.keys():
        if "." in product:
            continue
        doneSection()
        startTime = time.time()
        scripts = ScriptContainer()
        element = Factory.build(product, 'Test', 'Product')
        element.setScriptContainer(scripts)
        html = element.toHTML()
        html += scripts.toHTML()

        generationTime = time.time() - startTime
        results['createAllOnce'] += generationTime
        generationTimes[generationTime] = (product, len(html))
    results['longestCreationTime'] = generationTimes.orderedKeys[-1]
    return generationTimes
def getGenerationTimeForAllElementsLooped100Times():
    startTime = time.time()
    allProducts = Box('AllProducts')
    scripts = ScriptContainer()
    allProducts.setScriptContainer(scripts)
    for x in xrange(100):
        doneSection()
        for product in Factory.products.keys():
            allProducts.addChildElement(Factory.build(product, 'Test', 'Product'))
    instantiationTime = time.time() - startTime
    results['loopedInit'] = instantiationTime

    startTime = time.time()
    html = allProducts.toHtml()
    html += scripts.toHtml()
    generationTime = (time.time() - startTime)
    results['loopedToHtml'] = generationTime
    results['loopedToHtmlSize'] = len(html)
    results['loopedCreate'] = results['loopedInit'] + results['loopedToHtml']
def getGenerationTimeForAllElementsLooped100Times():
    startTime = time.time()
    allProducts = Box('AllProducts')
    scripts = ScriptContainer()
    allProducts.setScriptContainer(scripts)
    for x in xrange(100):
        doneSection()
        for product in Factory.products.keys():
            allProducts.addChildElement(
                Factory.build(product, 'Test', 'Product'))
    instantiationTime = time.time() - startTime
    results['loopedInit'] = instantiationTime

    startTime = time.time()
    html = allProducts.toHtml()
    html += scripts.toHtml()
    generationTime = (time.time() - startTime)
    results['loopedToHtml'] = generationTime
    results['loopedToHtmlSize'] = len(html)
    results['loopedCreate'] = results['loopedInit'] + results['loopedToHtml']
示例#9
0
    def _parseElement(self, element):
        if not element.scriptContainer():
            element.setScriptContainer(ScriptContainer())
            element.scriptContainer().toHTML()

        try:
            root = etree.fromstring(element.toHTML().replace("&", "&").replace("&", "&"). \
                                                     replace(" ", " ").replace("form:error", "span").
                                                     replace("error", "span"), parser)
        except Exception as e:
            print(element.toHTML())
            raise e
class TestScriptContainer(ElementTester):
    def setup_method(self, method):
        self.element = ScriptContainer()

    def test_addScript(self):
        assert self.element._scripts == []

        self.element.addScript("alert('I am a script :D');")
        self.element.addScript("var value = 'I am another script';")
        assert self.element._scripts == [
            "alert('I am a script :D');", "var value = 'I am another script';"
        ]
        assert "alert('I am a script :D');" in self.element.toHTML()
        assert "var value = 'I am another script';" in self.element.toHTML()

    def test_removeScript(self):
        assert self.element._scripts == []

        self.element.addScript("alert('I am a script :D');")
        assert self.element._scripts == ["alert('I am a script :D');"]

        self.element.removeScript("alert('I am a script :D');")
        assert self.element._scripts == []
class TestScriptContainer(ElementTester):

    def setup_method(self, method):
        self.element = ScriptContainer()

    def test_addScript(self):
        assert self.element._scripts == []

        self.element.addScript("alert('I am a script :D');")
        self.element.addScript("var value = 'I am another script';")
        assert self.element._scripts == ["alert('I am a script :D');",
                                                 "var value = 'I am another script';"]
        assert "alert('I am a script :D');" in self.element.toHtml()
        assert "var value = 'I am another script';" in self.element.toHtml()

    def test_removeScript(self):
        assert self.element._scripts == []

        self.element.addScript("alert('I am a script :D');")
        assert self.element._scripts == ["alert('I am a script :D');"]

        self.element.removeScript("alert('I am a script :D');")
        assert self.element._scripts == []
 def setup_method(self, method):
     self.element = ScriptContainer()
 def setup_method(self, method):
     self.element = ScriptContainer()