[Zope3-Users] actions in formlib, redirects

Andreas Reuleaux reuleaux at web.de
Tue Jun 6 22:19:04 EDT 2006


I have got a formlib edit form, that I am populating with
actions now (note that by inheriting from EditForm there
is one action already defined):

  # some imports omitted here

  class EditView(form.EditForm):
      form_fields = form.Fields(IAdr)
      template = namedtemplate.NamedTemplate('sample')

      # my actions


Several questions:

* Ideally I would like in an action to redirect from my form to
  index.html (another view) thereby reusing the already defined 
  action handle_edit_action (which does the details of applying 
  the changes)

    @action("Foo", condition=haveInputWidgets)
    def changed(self, action, data):
	super(EditView, self).handle_edit_action(action, data)
	self.request.response.redirect('index.html')

  However I get

     'Action' object is not callable

  which seems strange to me, so I looked around and found
  that others had the same problem:
  http://mail.zope.org/pipermail/zope3-dev/2005-July/014851.html
  and followups. However this was in July 2005. The question
  was raised in this thread if this was a bug. - Is it? Still?

* In another answer to the same problem,
  http://mail.zope.org/pipermail/zope3-users/2006-February/002525.html
  I found I can redefine render to redirect me to index.html

    def render(self):
        if self.errors is None or self.errors:
            return super(EditView, self).render()
        self.request.response.redirect('index.html')

  So this is what I am doing now, but this has its flaws:
  It is a general exit for all the actions in my form.
  I cannot in the case of action1 redirect to some other place
  than in the case of action2. After all actions should
  make possible the usage of multiple submit buttons.

* Anyways, for now I am using the solution with render()
  above, and I learned I can reuse the actions from EditForm
  as follows
    
    actions = form.EditForm.actions[:]
  
  I am reusing them for two reasons:
  
   - I can rename the Label of my predefined Action
    
       a=actions[u'actions.apply']
       a.label=u'Speichern'
  
   - And I can add additional actions like this one

       @action(u'Back')
       def back(self, action, data):
	 ...

     However trying to work with unicode results
     in an error here: 

       @action(u'Zurück')
       def back(...):
         ...

       # UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 3: ordinal not in range(128)

     although I have a line: # -*- coding: utf-8 -*-
     at the beginning of my file

* Also when looking at the source code in 
  src/zope/formlib/form.py 
  I found that in 

    class Action(object):
      ...

  there a two different definitions for submitted(self):

    def submitted(self):
        if not self.available():
            return False
        form = self.form
        name = "%s.%s" % (form.prefix, self.__name__)
        return name in form.request.form

  and

    def submitted(self):
        return (self.__name__ in self.form.request.form) and self.available()

  Should there really be two definitions?

By the way I used Zope 3.3.0b1 here.

-Andreas

  


More information about the Zope3-users mailing list