On 07/12/06, Alberto Valverde <[EMAIL PROTECTED]> wrote:
>
>
> On Dec 7, 2006, at 1:53 PM, Alberto Valverde wrote:
>
> >
> >
> > On Dec 7, 2006, at 1:17 PM, Ed Singleton wrote:
> > ...
> >
> >>
> >> For me, I have to produce lots of forms, but there's few variables
> >> between them (form, table, welcome message, thanbk you message, etc).
> >> Most of which would work fine except that the validate decorator
> >> complains if the form doesn't exist at the point when the function is
> >> decorated.  So I moved it into __init__:
> >>
> >> class FormController(controllers.Controller):
> >>     def __init__(self, form, table, page, intro_text="",
> >> thank_you="Thank you."):
> >>         super(FormController, self).__init__()
> >>         self.form = form
> >>         self.table = table
> >>         self.page = page
> >>         self.intro_text = intro_text
> >>         self.thank_you = thank_you
> >>
> >>         @expose()
> >>         @validate(form=self.form)
> >>         @error_handler(self.index)
> >>         def save(**kwargs):
> >>             table.save(**kwargs)
> >>             raise redirect('thank_you')
> >>
> >>         self.save = save
> >>
> >>     @expose(template='.templates.form')
> >>     def index(self):
> >>         page = self.page
> >>         form = self.form
> >>         intro_text = self.intro_text
> >>         return dict(page=page, form=form,
> >>               intro_text=intro_text)
> >>
> >>     @expose(template='.templates.form_thankyou')
> >>     def thank_you(self):
> >>         page = self.page
> >>         thank_you = self.thank_you
> >>         return dict(page=page, thank_you=
> >>               thank_you)
> >>
> >> It doesn't yet work, as when you save the form you get the error:
> >> ....
> >> TypeError: try_call() takes at least 2 non-keyword arguments (1
> >> given)
> >>
> >> I'm wondering if this might be easier to do with your method of
> >> having
> >> a single function that does everything.  But I'm not too
> >
> > When you're doing self.save = save your binding save as an unbound
> > method so when it's called 'self' is not passed as first positional
> > argument. You need to bind it as an instance method:
> >
> > from new import instancemethod
> >
> > def __init__(....):
> >      ...
> >      def save(self, **kwargs): # don't forget to specify 'self' as
> > first parameter
> >           ....
> >       self.save = instancemethod(save, self, self.__class__)
> >
> > Alberto
>
> Hmmm, and I think you might also need to pass the instance method
> function to error_handler (but not sure):
>
> @error_handler(self.index.im_func)
> def save(...):
>
> Please post back the solution when you find it... :)

Yes that did it.  I had to do a log.debug(dir(self.index)) to be
really sure that you meant self.index.im_func as I'd never heard of
im_func before.

The solution below works fine except for the redirect to the thank you
page.  I have to stop now but I'll play some more tomorrow.  I'll
probably fix it by adapting Matthew's single form controller so that
there isn't a redirect.

**If anyone else wants to use this controller, a quick warning to note
that I add a save() method to my tables that mangles my data for me.
You may want to change it to read table(**kwargs)

class FormController(controllers.Controller):
        def __init__(self, form, table, intro_text="", thank_you="Thank you."):
                super(FormController, self).__init__()
                self.form = form
                self.table = table
                self.intro_text = intro_text
                self.thank_you = thank_you

                @expose()
                @validate(form=self.form)
                @error_handler(self.index.im_func)
                def save(self, **kwargs):
                        table.save(**kwargs)
                        raise redirect('thank_you')

                self.save = new.instancemethod(save, self, self.__class__)

        @expose(template='.templates.form')
        def index(self):
                form = self.form
                intro_text = self.intro_text
                return dict(form=form, intro_text=intro_text)

        @expose(template='.templates.form_thankyou')
        def thank_you(self):
                thank_you = self.thank_you
                return dict(thank_you=thank_you)

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to