I think there was a notion to make validate into a "real" decorator
with no particular hooks in the TurboGears controller's __call__
method. That would make it easy enough to replace the TG validator
with custom validators from tosca, or wherever.
Max Ishenko also had some ideas on how he would like TG validators to
be better, coming from newforms I think. I think Alberto did a bit
of hacking around, but I don't think Max made any validator
changes....
I'll look into this more and see what's happening.
But I'm definitely open to ideas about how to make validation better
in TurboGears 2...
So, if there's something missing from the current version that was in
there before, feel free to add it back. ;)
And if there's something that it should be doing better, (like taking
form as a keyword param as mentioned in another thread), I'm open to
making those changes too.
--Mark Ramm
--
Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog
--Mark
> I think there was a notion to make validate into a "real"
> decorator with no particular hooks in the TurboGears controller's
> __call__ method. That would make it easy enough to replace the TG
> validator with custom validators from tosca, or wherever.
Count me as a huge -1 on this idea. "Real" decorators are often
"real" difficult to debug. I'd greatly prefer it if we could just
make the existing validate decorator more extensible by allowing you
to pass in callables. All of the gain, with none of the hassle!
We specifically did it this way, because we didn't like how the
previous TurboGears 1.x decorators ended up nesting your controller
methods tons of times, producing insane stack traces.
--
Jonathan LaCour
http://cleverdevil.org
I agree with that on some basic level, but I also think it's important
to differentiate @validate from @expose.
@validate is designed to change the method signature, whereas @expose
should not. @validate is a reasonable candidate for somebody to
switch out with a new implementation, and @expose is not.
I'm not so worried about @validate as I was about expose, the
decorator version of @validate wouldn't require extra work to
maintaint the method signature, and would have a very well-understood
scope. I haven't done it, but I think the "actual wrapper" version
of validate wouldn't be much more code, or much more difficult to
read.
Alberto particularly wanted to be able to supply his own validate in
ToscaWidgets, and if there's signficant benifit in letting people
tweek validate, it makes sense to decouple it from the controller so
that people could much more reasonably do that.
> I agree with that on some basic level, but I also think it's
> important to differentiate @validate from @expose.
Okay, I sort of agree, and sort of disagree with you here...
> @validate is designed to change the method signature, whereas
> @expose should not.
This should read "@validate *was* designed to change the method
signature", but that doesn't mean that it *has* to be done this way.
Personally, I find decorators that change the method signature to
be a little bit difficult to understand, and if its hard for me to
understand, then its probably going to be really hard for beginners
to understand.
I believe that we can get all the functionality that we need in
@validate, without having to do crazy things, or create nutty stack
traces.
> @validate is a reasonable candidate for somebody to switch out
> with a new implementation, and @expose is not.
I agree with you here, but there are two ways we could do this that
are far less insane than creating a "wrapping" decorator:
1. Allow our @validate decorator to take in callables to perform
generic validation, and error handling code that someone
defines on their own. This way, people can do whatever they
like.
2. If someone wants to replace the implementation of @validate,
then they should create their own decorator, not monkeypatch
TurboGears to use their implementation.
So, I don't see this as a good argument, personally.
> I'm not so worried about @validate as I was about expose,
> the decorator version of @validate wouldn't require extra
> work to maintaint the method signature, and would have a very
> well-understood scope. I haven't done it, but I think the "actual
> wrapper" version of validate wouldn't be much more code, or much
> more difficult to read.
It'll still mangle stack traces, though, which is a big deal to
me. Also, decorators that perform real "wrapping" instantly become
significantly more complex, as they have to make sure to "migrate"
all of the function attributes from the old function to the newly
wrapped function.
> Alberto particularly wanted to be able to supply his own validate
> in ToscaWidgets, and if there's signficant benifit in letting
> people tweek validate, it makes sense to decouple it from the
> controller so that people could much more reasonably do that.
I can certainly understand that, but again, I think we can
accomplish this without having to sacrifice the elegance of the
current solution, or making the decorator more complex than it needs
to be. I'd rather see us design @validate to be extensible by
passing in callables.
Just my two cents...
Well, I think the stack trace is a bit confused by all the
inspect_cal, perform_call, stuff in the controller anyway, but I
pretty much agree that if both validate and expose maintain the same
form there is a nice symmetry to that. Plus I agree that the current
validation setup is easy to understand, and I hope adding a callable
and the specific hook toscawidget hook where you can pass a form (or
anything with a .validate method) to the @validate, and possibly the
suggested ability to pass a generic callable in, should provide enough
flexibility.
But I think we should wait for Alberto to make his argument before we
make a final decision. But in the meantime I'm adding tickets for
improving the current validation system.
--Mark Ramm
I'm not so strong anymore on the points I expressed back then in
http://trac.turbogears.org/ticket/1426, I still favor a "real" decorator
approach because I find it cleaner (all validation done in the same
closure opposed to needing support infrastructure in the controller
subclass) and more pythonic (hey, decorators even got a "@" syntax in
2.4! ;)
As long as the validation system is pluggable I don't really care how
its implemented. The current duck-typing for a validate() method on the
validator object sounds like solution, maybe even better passing the
validation function directly (ie: "validate"), but needs a defined
protocol to handle validation errors since raising a FormEncode Invalid
might get in Max's way when he tries to adapt it to use django's
newforms. Maybe just returning True if validation passes or a
"validation errors" object that evaluates to False when it doesn't?
Alberto
That sounds like a reasonable amount of overhead! ;-)
Thanks for all your work Chris.
Iain
My current pylons is broken due to lots of 2.4 webob compatability
fixes, but I think I'll archive this branch for posterity and just
accept the 2.5 requirement for a bit so, I should be able to get this
merged into my pylons branch, and pushed back up to ben sometime this
week.
At work we have a python 2.4 requirement, so I'm finding that I'm not
able to progress on tg2 as quickly as I'd like. And at the same time
we have a big project due in the next couple of weeks, so Unfotunately
my TG2 time is going to continute to be reduced for a little bit.