On 3/22/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> I'm trying to be too clever with my python again.  Maybe someone
> will see an easier way to do this.
>
> I've got a set of DB classes with very similar structure, and I
> want to write a single CRUD controller that will handle them
> all.  The only things that need to be different for the different
> classes are (1) the db class, (2) the display/edit forms, and
> (3) a display text label.  (1) and (3) are easy, but (2) is
> giving me some trouble, because the "normal" pattern is to
> reference the form in an @validate decorator.
>
> What I'm trying to do is something like:
>
>      class CRUD(Controller):
>
>          def __init__(self, dbclass, form, label):
>              ....
>              self.__class__.save = 
> expose()(validate(form=self.editform)(error_handler(self.__class__.edit)(self.__class__.save)))
>
>          @expose(.templates.editform)
>          def edit(self, id, tg_errors=None, **formdata):
>              ....
>              formdata = dbclass.get(id)
>              ....
>              return dict(form=self.editform, formdata=formdata, 
> label=self.label)
>
>          def save(self, id, label=None):
>              ....
>
>      class AccountsController(CRUD): pass
>      class EntriesController(CRUD): pass
>
>
>      class Root(RootController):
>
>          accounts = Accounts(Account, accountform, 'Account')
>          entries = EntriesController(Entry, entryform, 'Entry')
>
>
> The overly tricky bit should be obvious, in the init method.  This works
> great except for the fact that the error_handler isn't getting recognized.
> When there's a validation error I get the following traceback:
>
>       File 
> "/var/www/finance.bitdance.com/moneytracker/lib/python2.4/TurboGears-1.0.1-py2.4.egg/turbogears/errorhandling.py",
>         line 110, in run_with_errors raise NotImplementedError("Method 
> %s.%s() has no applicable "
>     NotImplementedError: Method FlowTypeController.add() has no applicable 
> error handler.
>
> I presume I can work around this by defining the save method in the
> subclass and decorating it, but I hate to have to write the redundant
> code :).  Is there some way to set the error handler (and the
> validator, for that matter) other than the decorator?

I did something similar myself in a different thread, which might help.

http://groups.google.com/group/turbogears/browse_thread/thread/2a9cca20dff1f10c

It's just the forms without the crud, but the principle is the same.

There's also a thread thread where Alberto taught me about im_func,
which I still don't understand but seems to be important for this kind
of thing.

http://groups.google.com/group/turbogears/browse_frm/thread/43cc8c5123160a66/d9ddffce90dfc6cb

I'm going to be doing a very similar style of basic crud controller in
the next couple of weeks, if you want to share code.

To answer your specific question, yes there is a way to specify an
error handler without a decorator.  I forget the specific details but
IIRC it is literally as easy as checking for tg_errors, and then
redirecting to the controller you want to use, whilst passing on
tg_errors.  Check the source of error_handler to see what it actually
does.

Ed

--~--~---------~--~----~------------~-------~--~----~
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