def setUp(self): super(Test, self).setUp() traversingSetUp() # XXX this whole test setup is rather involved. Is there a # simpler way to publish widget_macros.pt? Probably. # load the registrations for formlib xmlconfig.file("configure.zcml", zope.formlib) # set up the widget_macros macro macro_template = PageTemplate() widget_macros = os.path.join(os.path.dirname(zope.formlib.__file__), 'widget_macros.pt') f = open(widget_macros, 'r') data = f.read() f.close() macro_template.write(data) @zope.component.adapter(None, None) @implementer(zope.traversing.interfaces.ITraversable) class view: def __init__(self, ob, r=None): pass def traverse(*args): return macro_template.macros provideAdapter(view, name='view') provideAdapter(DefaultTraversable, [None])
class RenderEmailSignature(BrowserView): """Render an email signature.""" def __init__(self, context, request): super(RenderEmailSignature, self).__init__(context, request) model = api.portal.get_registry_record( 'imio.dms.mail.browser.settings.IImioDmsMailConfig.' 'omail_email_signature') self.pt = PageTemplate() self.pt.pt_source_file = lambda: 'none' self.pt.write(model) self.namespace = self.pt.pt_getContext() self.namespace.update({ 'request': self.request, 'view': self, 'context': self.context, 'user': getSecurityManager().getUser(), 'modules': SecureModuleImporter }) dg_helper = getMultiAdapter((self.context, self.request), name='document_generation_helper_view') self.namespace['dghv'] = dg_helper self.namespace['sender'] = dg_helper.get_sender() def __call__(self): rendered = self.pt.pt_render(self.namespace) return richtextval(rendered)
def render(self): pt = PageTemplate() pt.write(self.get_template()) instance = self request = self.request namespace = self.get_context(instance, request) return pt.pt_render(namespace)
def create_html(rest_files, template): settings = {} settings["here"] = {"layout": template} page = PageTemplate() page.write("""<metal:block use-macro="here/layout/macros/pagelayout" />""") for restfile in rest_files: restfile.create_html(page, settings)
def profile_zpt(fn, count, profiler): from zope.pagetemplate.pagetemplate import PageTemplate pt = PageTemplate() pt.write(open(fn).read()) for i in range(4): pt.pt_render(extra_context=data) r = [None] * count for i in r: profiler.runcall(pt.pt_render, 0, data)
def test(file): raw_data = open(file, 'r').read() pt = PageTemplate() pt.write(raw_data) if pt._v_errors: print('*** Error in:', file) # noqa for error in pt._v_errors[1:]: formatted = error.replace('\n', ' ') linepos = formatted.rfind(', at line') if linepos != -1: formatted, formatted2 = (formatted[:linepos], formatted[linepos:]) else: formatted2 = '' print(' ', formatted) # noqa print(' ', formatted2) # noqa print() # noqa
def test(file): raw_data = open(file, 'r').read() pt = PageTemplate() pt.write(raw_data) if pt._v_errors: print "*** Error in:", file for error in pt._v_errors[1:]: formatted = error.replace('\n', ' ') linepos = formatted.rfind(', at line') if linepos != -1: formatted, formatted2 = ( formatted[:linepos], formatted[linepos:]) else: formatted2 = '' print ' ', formatted print ' ', formatted2 print
def template(self): if self.request.form.get('ajax_load'): layout_resource_path = getDefaultAjaxLayout(self.context) else: layout_resource_path = getDefaultSiteLayout(self.context) if layout_resource_path is None: return self.main_template try: layout = resolveResource(layout_resource_path) except NotFound as e: logger.warning('Missing layout {0:s}'.format(e)) cooked = cook_layout(layout) pt = PageTemplate() pt.write(cooked) return pt
def test(file): raw_data = open(file, "r").read() pt = PageTemplate() pt.pt_source_file = lambda: file pt.write(raw_data) if pt._v_errors: print("*** Error in:", file) for error in pt._v_errors[1:]: formatted = error.replace("\n", " ") linepos = formatted.rfind(", at line") if linepos != -1: formatted, formatted2 = (formatted[:linepos], formatted[linepos:]) else: formatted2 = "" print(" ", formatted) print(" ", formatted2) print() return False return True
def get_context(self, instance, request, **kw): pt = PageTemplate() namespace = pt.pt_getContext() namespace['request'] = request namespace['view'] = instance namespace['context'] = context = instance.context # get the root obj = self.context root = None meth = aq_get(obj, 'getPhysicalRoot', None) if meth is not None: root = meth() namespace.update(here=obj, # philiKON thinks container should be the view, # but BBB is more important than aesthetics. container=obj, root=root, modules=SecureModuleImporter, traverse_subpath=[], # BBB, never really worked user = getSecurityManager().getUser() ) return namespace
class BasicTemplateTests(unittest.TestCase): def setUp(self): self.t = PageTemplate() def test_if_in_var(self): # DTML test 1: if, in, and var: pass # for unittest """ %(comment)[ blah %(comment)] <html><head><title>Test of documentation templates</title></head> <body> %(if args)[ <dl><dt>The arguments to this test program were:<p> <dd> <ul> %(in args)[ <li>Argument number %(num)d was %(arg)s %(in args)] </ul></dl><p> %(if args)] %(else args)[ No arguments were given.<p> %(else args)] And thats da trooth. </body></html> """ tal = util.read_input('dtml1.html') self.t.write(tal) aa = util.argv(('one', 'two', 'three', 'cha', 'cha', 'cha')) o = self.t(content=aa) expect = util.read_output('dtml1a.html') util.check_xml(expect, o) aa = util.argv(()) o = self.t(content=aa) expect = util.read_output('dtml1b.html') util.check_xml(expect, o) def test_batches_and_formatting(self): # DTML test 3: batches and formatting: pass # for unittest """ <html><head><title>Test of documentation templates</title></head> <body> <!--#if args--> The arguments were: <!--#in args size=size end=end--> <!--#if previous-sequence--> (<!--#var previous-sequence-start-arg-->- <!--#var previous-sequence-end-arg-->) <!--#/if previous-sequence--> <!--#if sequence-start--> <dl> <!--#/if sequence-start--> <dt><!--#var sequence-arg-->.</dt> <dd>Argument <!--#var num fmt=d--> was <!--#var arg--></dd> <!--#if next-sequence--> (<!--#var next-sequence-start-arg-->- <!--#var next-sequence-end-arg-->) <!--#/if next-sequence--> <!--#/in args--> </dl> <!--#else args--> No arguments were given.<p> <!--#/if args--> And I\'m 100% sure! </body></html> """ tal = util.read_input('dtml3.html') self.t.write(tal) aa = util.argv(('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', )) from zope.pagetemplate.tests import batch o = self.t(content=aa, batch=batch.batch(aa.args, 5)) expect = util.read_output('dtml3.html') util.check_xml(expect, o) def test_on_error_in_slot_filler(self): # The `here` isn't defined, so the macro definition is # expected to catch the error that gets raised. text = '''\ <div metal:define-macro="foo"> <div tal:on-error="string:eek"> <div metal:define-slot="slot" /> cool </div> </div> <div metal:use-macro="template/macros/foo"> <div metal:fill-slot="slot"> <p tal:content="here/xxx" /> </div> </div> ''' self.t.write(text) self.t() def test_on_error_in_slot_default(self): # The `here` isn't defined, so the macro definition is # expected to catch the error that gets raised. text = '''\ <div metal:define-macro="foo"> <div tal:on-error="string:eek"> <div metal:define-slot="slot"> <div tal:content="here/xxx" /> </div> </div> </div> <div metal:use-macro="template/macros/foo"> </div> ''' self.t.write(text) self.t() def test_unicode_html(self): text = u'<p>\xe4\xf6\xfc\xdf</p>' # test with HTML parser self.t.pt_edit(text, 'text/html') self.assertEquals(self.t().strip(), text) # test with XML parser self.t.pt_edit(text, 'text/xml') self.assertEquals(self.t().strip(), text)
def pt_getContext(self, args=(), options={}, **kw): rval = PageTemplate.pt_getContext(self, args=args) options.update(rval) return options
class BasicTemplateTests(unittest.TestCase): def setUp(self): self.t = PageTemplate() def test_if_in_var(self): # DTML test 1: if, in, and var: pass # for unittest """ %(comment)[ blah %(comment)] <html><head><title>Test of documentation templates</title></head> <body> %(if args)[ <dl><dt>The arguments to this test program were:<p> <dd> <ul> %(in args)[ <li>Argument number %(num)d was %(arg)s %(in args)] </ul></dl><p> %(if args)] %(else args)[ No arguments were given.<p> %(else args)] And thats da trooth. </body></html> """ tal = util.read_input('dtml1.html') self.t.write(tal) aa = util.argv(('one', 'two', 'three', 'cha', 'cha', 'cha')) o = self.t(content=aa) expect = util.read_output('dtml1a.html') util.check_xml(expect, o) aa = util.argv(()) o = self.t(content=aa) expect = util.read_output('dtml1b.html') util.check_xml(expect, o) def test_template_usage(self): tal = util.read_input('template_usage.html') self.t.write(tal) o = self.t(template_usage=u"test") expect = util.read_output('template_usage1.html') util.check_xml(expect, o) o = self.t(template_usage=u"retest") expect = util.read_output('template_usage2.html') util.check_xml(expect, o) o = self.t(template_usage=u"other") expect = util.read_output('template_usage3.html') util.check_xml(expect, o) o = self.t(template_usage=u"") expect = util.read_output('template_usage4.html') util.check_xml(expect, o) def test_batches_and_formatting(self): # DTML test 3: batches and formatting: pass # for unittest """ <html><head><title>Test of documentation templates</title></head> <body> <!--#if args--> The arguments were: <!--#in args size=size end=end--> <!--#if previous-sequence--> (<!--#var previous-sequence-start-arg-->- <!--#var previous-sequence-end-arg-->) <!--#/if previous-sequence--> <!--#if sequence-start--> <dl> <!--#/if sequence-start--> <dt><!--#var sequence-arg-->.</dt> <dd>Argument <!--#var num fmt=d--> was <!--#var arg--></dd> <!--#if next-sequence--> (<!--#var next-sequence-start-arg-->- <!--#var next-sequence-end-arg-->) <!--#/if next-sequence--> <!--#/in args--> </dl> <!--#else args--> No arguments were given.<p> <!--#/if args--> And I\'m 100% sure! </body></html> """ tal = util.read_input('dtml3.html') self.t.write(tal) aa = util.argv(('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', )) from zope.pagetemplate.tests import batch o = self.t(content=aa, batch=batch.batch(aa.args, 5)) expect = util.read_output('dtml3.html') util.check_xml(expect, o)
def PageTemplateFile(filename): pt = PageTemplate() pt.write(codecs.open(filename, "r", encoding="utf-8").read()) return pt
def reportObjekte(self, reportcontext, typ, sort_on, sort_order, filter={}, reportname='Standard', test=False, **templatevars): """ @param reportcontext: context mit dem der Report erzeugt werden soll. @param typ: Entityname fuer die Datenbankabfrage zur Bestimmung des Reportumfangs. @param sort_on: DB-Sortierattribut @param sort_order: DB-Sortierreihenfolge @param filter: Moegliche Filterwerte fuer die Abfrage. @param reportname: Name des registrierten Reports @param test: Attribut zu Testzwecken mit einem echten View-Template """ log('reportObjekte') reportReg = _reportConfigReg.get(typ, []) reportConfig = None #print 'filter ', filter REQUEST = self._getRequest() # print reportReg # print reportname for ec in reportReg: if ec[0] == reportname: reportConfig = ec[1] # print reportConfig if reportConfig: # print 'reportConfig gefunden' reportTemplateName = reportConfig.get('reportTemplateName') if reportTemplateName: # print 'TemplateName gefunden' objekte = [] objsel = REQUEST.get('objsel', []) if objsel: for pks in objsel: pkvals = self.getPKDict(typ, eval(pks)) objekte.append(self.objektdatensatz(typ, **pkvals)) #ansonsten else: objekte = self.objekteSuchen(typ, filter, sort_on=sort_on, sort_order=sort_order) # print len(objekte) if not templatevars: templatevars = {'objekte':objekte, 'reportcontext':reportcontext} else: templatevars['objekte'] = objekte templatevars['reportcontext'] = reportcontext template_obj = None if reportcontext.getField(reportTemplateName): template_obj = PageTemplate() content = reportcontext.getField(reportTemplateName).getRaw(reportcontext)#.read() # print content template_obj.pt_edit(content,'text/html') else: if test: return templatevars['objekte'] template_obj = getattr(reportcontext, reportTemplateName) # print template_obj # return template_obj(**templatevars) return safe_unicode(template_obj(**templatevars))
def setUp(self): self.t = PageTemplate()
def time_zpt(fn, count): from zope.pagetemplate.pagetemplate import PageTemplate pt = PageTemplate() pt.write(open(fn).read()) return time_apply(pt.pt_render, (data, ), {}, count)
def setUp(self): self.folder = f = Folder() f.laf = PageTemplate() f.t = PageTemplate()
def time_zpt(fn, count): from zope.pagetemplate.pagetemplate import PageTemplate pt = PageTemplate() pt.write(open(fn).read()) return time_apply(pt.pt_render, (data,), {}, count)