pythonplonezopegenericsetup

Custom views for ATDocuments created through GenericSetup


I have a folderish ATDocument class create through generic setup, generic setup xml configuration file has the following view variables defined:

<property name="immediate_view">TemplateFileName</property>
<property name="default_view">TemplateFileName_view</property>
<property name="view_methods">
    <element value="TemplateFileName_view"/>
</property>

Is it possible to replace that view with a dispatcher, i.e. a BrowserView class that on __call__ would dispatch an actual ViewPagetTemplateFile() instance?

I tried replacing template file name with a method name of that class but that doesn't seem to work. Also I followed inheritance tree of ATDocument though ATCTContent to BaseContent, but I didn't find any definitions of views, so I'm guessing views are processed via one of the inherited mix-ins.


Solution

  • The names in the GenericSetup xml file are either view names or skin items; view names are looked up using the same traversal mechanisms as when you direcly name the view in the URL.

    So, anything you can reach with a URL can be used as a view method. That includes views that dispatch to other views in the __call__ method:

    from zope.publisher.browser import BrowserView
    from zope.component import getMultiAdapter
    
    class DispatchingView(BrowserView):
        def __call__(self):
            if somecondition:
                newviewname = 'foo'
            else:
                newviewname = 'bar'
    
            return getMultiAdapter((self.context, self.request), name=newviewname)()
    

    This example view looks up other views by their name, and renders them in place to return as the result of the dispatcher itself.

    Note that generally, I make sure that if a view is being used I make sure to include the @@ view namespace in front of it's name to prevent that a skin item with the same name accidentally is being used.