[Product-Developers] Re: Traversing from a z3c.form action handler

cewing cewing at u.washington.edu
Mon Feb 15 06:29:22 UTC 2010


Martin,

Thanks for the tips.

Based on the input I got from Carsten and you, I've come up with this as an
override of the plone.z3cform FormWrapper class:

class HydraCrudFormWrapper(FormWrapper):
    """ subclass the formwrapper so that we can hide the green frame
globally
        on all CRUD-based forms
    """
    def __init__(self, context, request):
        """ hide the green frame
        """
        super(HydraCrudFormWrapper, self).__init__(context, request)
        self.request.set('disable_border', 1)
    
    
    def update(self):    
        z2.switch_on(self, request_layer=self.request_layer)
        self.form_instance.update()

        return_path = getattr(self.form_instance, '_return_other', None)
        if return_path:
            base_path = self.context.absolute_url_path()
            path = base_path + return_path
            view = self.context.restrictedTraverse(path)
            if view:
                self._contents = view()
        else:
            self.contents = self.form_instance.render()
            
    def __call__(self):
        self.update()
        rp = getattr(self.form_instance, '_return_other', None)
        if rp:
            return self._contents
        else:
            return self.render()


By combining this with a bit of stuff in the button handlers that sets a
path-ending string for the redirect above and I've got a mix that allows me
to create buttons that either process their own form values or pass them
along to some pre-built plone script.  Pretty nifty! 

    @button.handler(ITypeFormButtons['change_state'])
    def handle_state_change(self, action):
        """ traverse to the content_status_share form for processing
        """
        true_context = self.context.context
        selected = self.selected_items()
        if not selected:
            IStatusMessage(self.request).add("No items have been selected",
type=u'warning')
            return
        
        paths = [item.absolute_url_path() for id, item in selected]
        self.request.form['paths'] = paths
        self.context._return_other = "/content_status_history"


It was especially interesting to try to accomplish since the forms in
question are all based on the plone.z3cform CRUD form.  That sure added a
bit of fun.

Anyway, here's a pointer to a solution in case someone else comes along
later and needs a similar trick.

Thanks again Martin, and Carsten.  
-- 
View this message in context: http://n2.nabble.com/Traversing-from-a-z3c-form-action-handler-tp4569560p4573146.html
Sent from the Product Developers mailing list archive at Nabble.com.




More information about the Product-Developers mailing list