Generic Form Controller

1 view
Skip to first unread message

Ed Singleton

unread,
Dec 8, 2006, 7:05:57 AM12/8/06
to turbo...@googlegroups.com
With a lot of help from Alberto, I've created a Generic Form
Controller that reduces a lot of the repetition when you have lots of
straightforward forms on your site. It should be fairly easy to
customise for your own purposes:

class GenericFormController(controllers.Controller):
def __init__(self, form, data_handler, intro_text="",
thank_you_message="Thank you."):
super(self.__class__, self).__init__()
self.form = form
self.data_handler = data_handler
self.intro_text = intro_text
self.thank_you_message = thank_you_message

@expose()
@validate(form=self.form)
@error_handler(self.index.im_func)
def save(self, **kwargs):
self.data_handler(**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_message = self.thank_you_message
return dict(thank_you_message=thank_you_message)

It can be used like this:

register = GenericFormController(my_form, model.MyTable,
intro_text='Please fill in my form.', thank_you_message='Thank you for
filling in my form.')

Hope it's of use to people.

Ed

Alberto

unread,
Dec 9, 2006, 6:38:14 AM12/9/06
to TurboGears
This should be saved somewhere so it doesn't get diluted in the
flood... :)

Is the Trac still the place to post recipes like this one?

Alberto

Lee McFadden

unread,
Dec 9, 2006, 8:19:19 AM12/9/06
to turbo...@googlegroups.com

No, the RoughDocs[1] area of the docs site is the place for recipes
like this now.

[1]: http://docs.turbogears.org/1.0/RoughDocs

Lee


--
Lee McFadden

blog: http://www.splee.co.uk
work: http://fireflisystems.com
skype: fireflisystems

Karl Guertin

unread,
Dec 9, 2006, 5:34:18 PM12/9/06
to turbo...@googlegroups.com
On 12/9/06, Alberto <alb...@toscat.net> wrote:
> This should be saved somewhere so it doesn't get diluted in the
> flood... :)

I tag posts like this to prevent this from happening. Eventually I'll
move the tagged posts to the wiki, but it's always nice when somebody
else does it for me. ;]

Ian Charnas

unread,
Dec 10, 2006, 12:11:57 PM12/10/06
to TurboGears
How do you tag posts? Are you the one using the google "rate this
post" option, or are you doing something in your email client or
something? Just curious!
-ian

On Dec 9, 5:34 pm, "Karl Guertin" <grayr...@gmail.com> wrote:


> On 12/9/06, Alberto <albe...@toscat.net> wrote:
>
> > This should be saved somewhere so it doesn't get diluted in the

> > flood... :)I tag posts like this to prevent this from happening. Eventually I'll

Karl Guertin

unread,
Dec 11, 2006, 12:20:19 AM12/11/06
to turbo...@googlegroups.com
On 12/10/06, Ian Charnas <i...@case.edu> wrote:
> How do you tag posts? Are you the one using the google "rate this
> post" option, or are you doing something in your email client or
> something? Just curious!

I read this list through gmail, which does support labels. I use an
expanded set of keyboard shortcuts [1], which means I don't have to
pick it from the menu and makes using labels as tags a bit simpler.

[1] http://gr.ayre.st/~grayrest/greasemonkey/gmail/gmail-macros.user.js
(press ? for key listing)

Ed Singleton

unread,
Dec 12, 2006, 7:08:23 AM12/12/06
to turbo...@googlegroups.com
I'll pop it in myself over the next couple of days.

Ed

Reply all
Reply to author
Forward
0 new messages