[silva-dev] Conditionally render viewlet

Jonathan Bowlas j.bowlas at ucl.ac.uk
Wed Jun 8 13:37:09 CEST 2011


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 at ucl.ac.uk> wrote:
> Sorry but it's still not working, I did this:
>
> class LeftColumn(silvaviews.ViewletManager):
>    """Left Content Area
>    """
>    def filter(self):
>         viewlets = super(LeftColumn, self).filter()
>         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):
>  Module infrae.wsgi.publisher, line 183, in error
>  Module silva.core.views.views, line 73, in __call__
>  Module infrae.layout.components, line 105, in __call__
>   - Published class: silva.core.layout.porto.errors.OtherErrorPage
>   - Object path: /silva
>   - Object type: Silva Root
>  Module infrae.layout.components, line 77, in __call__
>  Module infrae.layout.components, line 70, in render
>  Module infrae.layout.components, line 67, in _render_template
>  Module five.grok.components, line 133, in render
>  Module zope.pagetemplate.pagetemplate, line 113, in pt_render
>  Module zope.tal.talinterpreter, line 271, in __call__
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 531, in do_optTag_tal
>  Module zope.tal.talinterpreter, line 513, in no_tag
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
>  Module Products.PageTemplates.Expressions, line 220, in evaluateStructure
>  Module zope.tales.tales, line 696, in evaluate
>   - URL: c:\silva-2.3.1\eggs\silva.core.layout-2.3.2-py2.6.egg\silva\core\layout\porto\porto_templates\mainlayout.pt
>   - Line 29, Column 4
>   - Expression: <StringExpr u'layout'>
>   - Names:
>      {'args': (),
>       'container': <Root at /silva>,
>       'context': <Root at /silva>,
>       'default': <object object at 0x01AD1720>,
>       'here': <Root at /silva>,
>       'layout':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.MainErrorLayout
> object at 0x0722CE10>,
>       'loop': {},
>       'nothing': None,
>       'options': {},
>       'repeat': <Products.PageTemplates.Expressions.SafeMapping
> object at 0x0721D660>,
>       'request': <WSGIRequest,
> URL=http://localhost:8080/silva/test/@@index.html>,
>       'root': <Application at >,
>       'static': <silva.resourceinclude.resource.ResourceView object
> at 0x0722C750>,
>       'template':
> <Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at
> 0x06364A90>,
>       'traverse_subpath': [],
>       'user': <User 'admin'>,
>       'view': <silva.core.layout.porto.errors.OtherErrorPage object
> at 0x06A63BF0>,
>       'views': <Products.Five.browser.pagetemplatefile.ViewMapper
> object at 0x0723A6D0>}
>  Module zope.contentprovider.tales, line 80, in __call__
>  Module grokcore.viewlet.components, line 80, in render
>  Module five.grok.components, line 133, in render
>  Module zope.pagetemplate.pagetemplate, line 113, in pt_render
>  Module zope.tal.talinterpreter, line 271, in __call__
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 533, in do_optTag_tal
>  Module zope.tal.talinterpreter, line 518, in do_optTag
>  Module zope.tal.talinterpreter, line 513, in no_tag
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 852, in do_condition
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 531, in do_optTag_tal
>  Module zope.tal.talinterpreter, line 513, in no_tag
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
>  Module Products.PageTemplates.Expressions, line 220, in evaluateStructure
>  Module zope.tales.tales, line 696, in evaluate
>   - URL: c:\silva-2.3.1\src\silvatheme.uclcorporateidentity\silvatheme\uclcorporateidentity\uclcorporateidentity_templates\layout.pt
>   - Line 5, Column 8
>   - Expression: <StringExpr u'threecolumn'>
>   - Names:
>      {'args': (),
>       'container': <Root at /silva>,
>       'context': <Root at /silva>,
>       'default': <object object at 0x01AD1720>,
>       'here': <Root at /silva>,
>       'layout':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.MainErrorLayout
> object at 0x0722CE10>,
>       'loop': {},
>       'nothing': None,
>       'options': {},
>       'provider':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.Layout object at
> 0x072033F0>,
>       'repeat': <Products.PageTemplates.Expressions.SafeMapping
> object at 0x0721D690>,
>       'request': <WSGIRequest,
> URL=http://localhost:8080/silva/test/@@index.html>,
>       'root': <Application at >,
>       'static': <silva.resourceinclude.resource.ResourceView object
> at 0x07234D30>,
>       'template':
> <Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at
> 0x063EF510>,
>       'traverse_subpath': [],
>       'user': <User 'admin'>,
>       'view': <silva.core.layout.porto.errors.OtherErrorPage object
> at 0x06A63BF0>,
>       'viewletmanager':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.Layout object at
> 0x072033F0>,
>       'views': <Products.Five.browser.pagetemplatefile.ViewMapper
> object at 0x07234DB0>}
>  Module zope.contentprovider.tales, line 80, in __call__
>  Module grokcore.viewlet.components, line 80, in render
>  Module five.grok.components, line 133, in render
>  Module zope.pagetemplate.pagetemplate, line 113, in pt_render
>  Module zope.tal.talinterpreter, line 271, in __call__
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 531, in do_optTag_tal
>  Module zope.tal.talinterpreter, line 513, in no_tag
>  Module zope.tal.talinterpreter, line 343, in interpret
>  Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
>  Module Products.PageTemplates.Expressions, line 220, in evaluateStructure
>  Module zope.tales.tales, line 696, in evaluate
>   - URL: c:\silva-2.3.1\src\silvatheme.uclcorporateidentity\silvatheme\uclcorporateidentity\uclcorporateidentity_templates\threecolumn.pt
>   - Line 6, Column 12
>   - Expression: <StringExpr u'leftcolumn'>
>   - Names:
>      {'args': (),
>       'container': <Root at /silva>,
>       'context': <Root at /silva>,
>       'default': <object object at 0x01AD1720>,
>       'here': <Root at /silva>,
>       'layout':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.MainErrorLayout
> object at 0x0722CE10>,
>       'loop': {},
>       'nothing': None,
>       'options': {},
>       'provider':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.ThreeColumn
> object at 0x07234ED0>,
>       'repeat': <Products.PageTemplates.Expressions.SafeMapping
> object at 0x0721D6F0>,
>       'request': <WSGIRequest,
> URL=http://localhost:8080/silva/test/@@index.html>,
>       'root': <Application at >,
>       'static': <silva.resourceinclude.resource.ResourceView object
> at 0x07234F30>,
>       'template':
> <Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at
> 0x063EF7D0>,
>       'traverse_subpath': [],
>       'user': <User 'admin'>,
>       'view': <silva.core.layout.porto.errors.OtherErrorPage object
> at 0x06A63BF0>,
>       'viewletmanager':
> <silvatheme.uclcorporateidentity.uclcorporateidentity.ThreeColumn
> object at 0x07234ED0>,
>       'views': <Products.Five.browser.pagetemplatefile.ViewMapper
> object at 0x07234F50>}
>  Module zope.contentprovider.tales, line 77, in __call__
>  Module 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 <antonin at infrae.com> 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
>>>> = False,  but 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(...):
>>   ...
>>   def filter(self):
>>      viewlets = super(MyViewletManager, self).filter()
>>      return [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 at lists.infrae.com
>> https://lists.infrae.com/mailman/listinfo/silva-dev
>>
>
>
>
> --
> Jonathan Bowlas
> Web Support Officer
> Media Services
> University College London
> Email: j.bowlas at ucl.ac.uk
>



-- 
Jonathan Bowlas
Web Support Officer
Media Services
University College London
Email: j.bowlas at ucl.ac.uk


More information about the silva-dev mailing list