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']
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']
def test_fromSHPAML(): """ Ensure UITemplate creates a dictionary structure from SHPAML correctly """ shpmal = """ container randomattribute=Hello childelement#SomeRandomId name=SomeRandomName > childishchildelement """ assert UITemplate.fromSHPAML(shpmal) == EXPECTED_STRUCTURE
def test_fromSHPAML(): """ Ensure UITemplate creates a dictionary structure from SHPAML correctly """ shpmal = """ container randomattribute=Hello childelement#SomeRandomId name=SomeRandomName > childishchildelement """ assert UITemplate.fromSHPAML(shpmal) == EXPECTED_STRUCTURE
def getBigTableGenerationTime(): template = UITemplate.fromSHPAML("> dom-table#bigTableTest") table = [dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10) for x in xrange(1000)] startTime = time.time() templateElement = TemplateElement(template=template, factory=Factory) for rowData in table: row = templateElement.bigTableTest.addChildElement(DOM.TR()) for data in rowData.itervalues(): row.addChildElement(DOM.TD()).addChildElement(TextNode(data)) doneSection() html = templateElement.toHtml() results['bigTable'] = time.time() - startTime results['bigTableSize'] = len(html)
def getBigTableGenerationTime(): template = UITemplate.fromSHPAML("> dom-table#bigTableTest") table = [ dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10) for x in xrange(1000) ] startTime = time.time() templateElement = TemplateElement(template=template, factory=Factory) for rowData in table: row = templateElement.bigTableTest.addChildElement(DOM.TR()) for data in itervalues(rowData): row.addChildElement(DOM.TD()).addChildElement(TextNode(data)) doneSection() html = templateElement.toHTML() results['bigTable'] = time.time() - startTime results['bigTableSize'] = len(html)
class BasicTemplateControl(PageControls.TemplateControl): template = UITemplate.fromSHPAML("> label@label text=TemplateLabel")
class DynamicForm(RequestHandler): """ Defines the base dynamic form - a page made where any section can be updated independently from the rest """ template = UITemplate.fromSHPAML("> document") elementFactory = Factory formatted = False resourceFiles = ('js/WebBot.js', 'stylesheets/Site.css') if csrf: sharedFields = ('csrfmiddlewaretoken', ) 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 modifyDocument(self, document, request): """ Override to change the structure of the base document """ pass def title(self, request): """ Returns the title of the page - by default this is the class name of the DynamicForm subclass override this method to change that """ return self.__class__.__name__ def favicon(self, request): """ Returns the title of the page - by default this is the class name of the DynamicForm subclass override this method to change that """ return "images/favicon.png" def requestResourceFiles(self, request): """ Returns the resource files that should be loaded with this page by request - override this to change """ return () class MainControl(PageControls.PageControl): """ Override this controller to define how the body of the page should render """ pass