Re: Conditionally render viewlet

Jonathan Bowlas <j.bowlas-hclig2XLE9Zaa/[email protected]> Wed, 8 Jun 2011 12:37:09 +0100
Newsgroups gmane.comp.web.zope.silva.devel
Message-ID <[email protected]>
Is there an example infrae extension or theme where the filter method
is overridden?

J

On 8 June 2011 12:35, Jonathan Bowlas <j.bowlas-hclig2XLE9Zaa/[email protected]> wrote:
> Sorry but it's still not working, I did this:
>
> class LeftColumn(silvaviews.ViewletManager):
> =A0 =A0"""Left Content Area
> =A0 =A0"""
> =A0 =A0def filter(self):
> =A0 =A0 =A0 =A0 viewlets =3D super(LeftColumn, self).filter()
> =A0 =A0 =A0 =A0 return [v for v in viewlets if v.available()]
>
> But it returns this traceback:
>
> 2011-06-08 12:30:35,868 ERROR [infrae.wsgi]
> Error while rendering error message
> Object class: Products.Silva.Root.Root
> Object path: /silva
> Request URL: http://localhost:8080/silva/test/@@index.html
> Request method: GET
> User: admin
> User-agent: Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox=
/4.0.1
> Refer: http://localhost:8080/silva/test
> Traceback (innermost last):
> =A0Module infrae.wsgi.publisher, line 183, in error
> =A0Module silva.core.views.views, line 73, in __call__
> =A0Module infrae.layout.components, line 105, in __call__
> =A0 - Published class: silva.core.layout.porto.errors.OtherErrorPage
> =A0 - Object path: /silva
> =A0 - Object type: Silva Root
> =A0Module infrae.layout.components, line 77, in __call__
> =A0Module infrae.layout.components, line 70, in render
> =A0Module infrae.layout.components, line 67, in _render_template
> =A0Module five.grok.components, line 133, in render
> =A0Module zope.pagetemplate.pagetemplate, line 113, in pt_render
> =A0Module zope.tal.talinterpreter, line 271, in __call__
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 531, in do_optTag_tal
> =A0Module zope.tal.talinterpreter, line 513, in no_tag
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
> =A0Module Products.PageTemplates.Expressions, line 220, in evaluateStruct=
ure
> =A0Module zope.tales.tales, line 696, in evaluate
> =A0 - URL: c:\silva-2.3.1\eggs\silva.core.layout-2.3.2-py2.6.egg\silva\co=
re\layout\porto\porto_templates\mainlayout.pt
> =A0 - Line 29, Column 4
> =A0 - Expression: <StringExpr u'layout'>
> =A0 - Names:
> =A0 =A0 =A0{'args': (),
> =A0 =A0 =A0 'container': <Root at /silva>,
> =A0 =A0 =A0 'context': <Root at /silva>,
> =A0 =A0 =A0 'default': <object object at 0x01AD1720>,
> =A0 =A0 =A0 'here': <Root at /silva>,
> =A0 =A0 =A0 'layout':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.MainErrorLayout
> object at 0x0722CE10>,
> =A0 =A0 =A0 'loop': {},
> =A0 =A0 =A0 'nothing': None,
> =A0 =A0 =A0 'options': {},
> =A0 =A0 =A0 'repeat': <Products.PageTemplates.Expressions.SafeMapping
> object at 0x0721D660>,
> =A0 =A0 =A0 'request': <WSGIRequest,
> URL=3Dhttp://localhost:8080/silva/test/@@index.html>,
> =A0 =A0 =A0 'root': <Application at >,
> =A0 =A0 =A0 'static': <silva.resourceinclude.resource.ResourceView object
> at 0x0722C750>,
> =A0 =A0 =A0 'template':
> <Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at
> 0x06364A90>,
> =A0 =A0 =A0 'traverse_subpath': [],
> =A0 =A0 =A0 'user': <User 'admin'>,
> =A0 =A0 =A0 'view': <silva.core.layout.porto.errors.OtherErrorPage object
> at 0x06A63BF0>,
> =A0 =A0 =A0 'views': <Products.Five.browser.pagetemplatefile.ViewMapper
> object at 0x0723A6D0>}
> =A0Module zope.contentprovider.tales, line 80, in __call__
> =A0Module grokcore.viewlet.components, line 80, in render
> =A0Module five.grok.components, line 133, in render
> =A0Module zope.pagetemplate.pagetemplate, line 113, in pt_render
> =A0Module zope.tal.talinterpreter, line 271, in __call__
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 533, in do_optTag_tal
> =A0Module zope.tal.talinterpreter, line 518, in do_optTag
> =A0Module zope.tal.talinterpreter, line 513, in no_tag
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 852, in do_condition
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 531, in do_optTag_tal
> =A0Module zope.tal.talinterpreter, line 513, in no_tag
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
> =A0Module Products.PageTemplates.Expressions, line 220, in evaluateStruct=
ure
> =A0Module zope.tales.tales, line 696, in evaluate
> =A0 - URL: c:\silva-2.3.1\src\silvatheme.uclcorporateidentity\silvatheme\=
uclcorporateidentity\uclcorporateidentity_templates\layout.pt
> =A0 - Line 5, Column 8
> =A0 - Expression: <StringExpr u'threecolumn'>
> =A0 - Names:
> =A0 =A0 =A0{'args': (),
> =A0 =A0 =A0 'container': <Root at /silva>,
> =A0 =A0 =A0 'context': <Root at /silva>,
> =A0 =A0 =A0 'default': <object object at 0x01AD1720>,
> =A0 =A0 =A0 'here': <Root at /silva>,
> =A0 =A0 =A0 'layout':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.MainErrorLayout
> object at 0x0722CE10>,
> =A0 =A0 =A0 'loop': {},
> =A0 =A0 =A0 'nothing': None,
> =A0 =A0 =A0 'options': {},
> =A0 =A0 =A0 'provider':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.Layout object at
> 0x072033F0>,
> =A0 =A0 =A0 'repeat': <Products.PageTemplates.Expressions.SafeMapping
> object at 0x0721D690>,
> =A0 =A0 =A0 'request': <WSGIRequest,
> URL=3Dhttp://localhost:8080/silva/test/@@index.html>,
> =A0 =A0 =A0 'root': <Application at >,
> =A0 =A0 =A0 'static': <silva.resourceinclude.resource.ResourceView object
> at 0x07234D30>,
> =A0 =A0 =A0 'template':
> <Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at
> 0x063EF510>,
> =A0 =A0 =A0 'traverse_subpath': [],
> =A0 =A0 =A0 'user': <User 'admin'>,
> =A0 =A0 =A0 'view': <silva.core.layout.porto.errors.OtherErrorPage object
> at 0x06A63BF0>,
> =A0 =A0 =A0 'viewletmanager':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.Layout object at
> 0x072033F0>,
> =A0 =A0 =A0 'views': <Products.Five.browser.pagetemplatefile.ViewMapper
> object at 0x07234DB0>}
> =A0Module zope.contentprovider.tales, line 80, in __call__
> =A0Module grokcore.viewlet.components, line 80, in render
> =A0Module five.grok.components, line 133, in render
> =A0Module zope.pagetemplate.pagetemplate, line 113, in pt_render
> =A0Module zope.tal.talinterpreter, line 271, in __call__
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 531, in do_optTag_tal
> =A0Module zope.tal.talinterpreter, line 513, in no_tag
> =A0Module zope.tal.talinterpreter, line 343, in interpret
> =A0Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
> =A0Module Products.PageTemplates.Expressions, line 220, in evaluateStruct=
ure
> =A0Module zope.tales.tales, line 696, in evaluate
> =A0 - URL: c:\silva-2.3.1\src\silvatheme.uclcorporateidentity\silvatheme\=
uclcorporateidentity\uclcorporateidentity_templates\threecolumn.pt
> =A0 - Line 6, Column 12
> =A0 - Expression: <StringExpr u'leftcolumn'>
> =A0 - Names:
> =A0 =A0 =A0{'args': (),
> =A0 =A0 =A0 'container': <Root at /silva>,
> =A0 =A0 =A0 'context': <Root at /silva>,
> =A0 =A0 =A0 'default': <object object at 0x01AD1720>,
> =A0 =A0 =A0 'here': <Root at /silva>,
> =A0 =A0 =A0 'layout':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.MainErrorLayout
> object at 0x0722CE10>,
> =A0 =A0 =A0 'loop': {},
> =A0 =A0 =A0 'nothing': None,
> =A0 =A0 =A0 'options': {},
> =A0 =A0 =A0 'provider':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.ThreeColumn
> object at 0x07234ED0>,
> =A0 =A0 =A0 'repeat': <Products.PageTemplates.Expressions.SafeMapping
> object at 0x0721D6F0>,
> =A0 =A0 =A0 'request': <WSGIRequest,
> URL=3Dhttp://localhost:8080/silva/test/@@index.html>,
> =A0 =A0 =A0 'root': <Application at >,
> =A0 =A0 =A0 'static': <silva.resourceinclude.resource.ResourceView object
> at 0x07234F30>,
> =A0 =A0 =A0 'template':
> <Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at
> 0x063EF7D0>,
> =A0 =A0 =A0 'traverse_subpath': [],
> =A0 =A0 =A0 'user': <User 'admin'>,
> =A0 =A0 =A0 'view': <silva.core.layout.porto.errors.OtherErrorPage object
> at 0x06A63BF0>,
> =A0 =A0 =A0 'viewletmanager':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.ThreeColumn
> object at 0x07234ED0>,
> =A0 =A0 =A0 'views': <Products.Five.browser.pagetemplatefile.ViewMapper
> object at 0x07234F50>}
> =A0Module zope.contentprovider.tales, line 77, in __call__
> =A0Module zope.viewlet.manager, line 104, in update
> TypeError: filter() takes exactly 1 argument (2 given)
>
> Any Ideas?
>
>
> On 8 June 2011 11:43, Antonin AMAND <[email protected]> wrote:
>>>> The viewlet still rendered even though theres no index_left.
>>>> Then I checked the documentation here
>>>> http://pypi.python.org/pypi/zope.viewlet, specifically the
>>>> 'ConditionalViewletManager' section where it states that Viewlets have
>>>> an 'available' attribute (not a method), so I tried setting available
>>>> =3D False, =A0but still the viewlet rendered.
>>
>> You're right, Viewlet have no available method, I've been mistaken.
>> What we usually do is this:
>>
>> In the viewlet manager we override the filters method :
>>
>> class MyViewletManager(...):
>> =A0 ...
>> =A0 def filter(self):
>> =A0 =A0 =A0viewlets =3D super(MyViewletManager, self).filter()
>> =A0 =A0 =A0return [v for v in viewlets if v.available()]
>>
>> Then define an available method on all viewlets of this ViewletManager.
>>
>> BTW, be carefull with hasattr(self.context, 'index_left'), it checks
>> with acquisition.
>>
>> Antonin
>> _______________________________________________
>> silva-dev mailing list
>> silva-dev-IAPFreCvJWM6s/[email protected]
>> https://lists.infrae.com/mailman/listinfo/silva-dev
>>
>
>
>
> --
> Jonathan Bowlas
> Web Support Officer
> Media Services
> University College London
> Email: j.bowlas-hclig2XLE9Zaa/[email protected]
>



-- =

Jonathan Bowlas
Web Support Officer
Media Services
University College London
Email: j.bowlas-hclig2XLE9Zaa/[email protected]