@validate recursion guard revisited + PATCH

1 view
Skip to first unread message

aspineux

unread,
Sep 22, 2008, 10:56:12 AM9/22/08
to TurboGears
Hi

I had the use to cascade @validate, but discovered this is not working
since 1.0.4 I thing
For example :

def validate_promotional_code(domain_name, promotional_code=None):
....

@validate(form=pre_subscription_form)

@validate(validators=tgx.ValidatorFunctionConverter(validate_promotional_code))
@error_handler(index)
@turbogears.expose(template=".templates.subscribe")
def pre_subscription(self, domain_name, promotional_code,
**kwargs):
....

That way I can validate the form data "format" using the form
validation system,
but also the data "consistency" depending where and why the form is
used.
tgx.ValidatorFunctionConverter() is just a self made convertor,
converting a function
in validator.

I was blocked by the new "recursion guard" used in the the "validate"
decorator,
that now limit the validation of only ONE condition per HTTP request !


Here is the patch I suggest

--- controllers.py.orig 2008-09-16 00:43:07.000000000 +0200
+++ controllers.py 2008-09-22 16:50:37.000000000 +0200
@@ -147,8 +147,9 @@
init_form = lambda self: form

def validate(func, *args, **kw):
- # do not validate a second time if already validated
- if hasattr(request, 'validation_state'):
+ # ASX this is a more simplistic recursion guard
+ request.validation_count=getattr(request,
'validation_count', 0)+1
+ if request.validation_count>15:
return func(*args, **kw)

form = init_form(args and args[0] or kw["self"])


And for free, here is a patch that add the name of the module and
class of the function, instead of just the
name of the function in the log


@@ -406,7 +407,13 @@
if config.get('server.environment', 'development') ==
'development':
# Only output this in development mode: If it's a field
storage object,
# this means big memory usage, and we don't want that in
production
- log.debug("Calling %s with *(%s), **(%s)", func, args, kw)
+ try:
+ func_name='%s.%s.%s' % (args[0].__module__,
args[0].__class__.__name__, func.func_name)
+ # ASX try to make the logging more user friendly
+ log.debug("Calling %s with *(%s), **(%s)", func_name,
args[1:], kw)
+ except:
+ log.debug("Calling %s with *(%s), **(%s)", func, args,
kw)
+
output = errorhandling.try_call(func, *args, **kw)
if isinstance(output, list):
return output


Regards


Alain Spineux




Florent Aide

unread,
Sep 22, 2008, 12:07:45 PM9/22/08
to turbo...@googlegroups.com
On 9/22/08, aspineux <aspi...@gmail.com> wrote:
>
> Hi

[...]

> Here is the patch I suggest

Could you create a ticket in our trac with this info ? This will help
us to not lose it. I have been applying old patches lately so this one
could go in 1.1 soon if the review goes well.

Alain, would you mind if this patch only went in 1.1 and not in 1.0 ?

Florent.

aspineux

unread,
Sep 22, 2008, 1:47:56 PM9/22/08
to TurboGears


On Sep 22, 6:07 pm, "Florent Aide" <florent.a...@gmail.com> wrote:
> On 9/22/08, aspineux <aspin...@gmail.com> wrote:
>
>
>
> >  Hi
>
> [...]
>
> >  Here is the patch I suggest
>
> Could you create a ticket in our trac with this info ? This will help
> us to not lose it. I have been applying old patches lately so this one
> could go in 1.1 soon if the review goes well.

http://trac.turbogears.org/ticket/1993

>
> Alain, would you mind if this patch only went in 1.1 and not in 1.0 ?

It fit the 1.0.7

Regards.

>
> Florent.

Christopher Arndt

unread,
Sep 22, 2008, 2:55:05 PM9/22/08
to turbo...@googlegroups.com
Hi,
aspineux schrieb:

> I had the use to cascade @validate, but discovered this is not working
> since 1.0.4 I thing
> For example :
> [...]

> That way I can validate the form data "format" using the form
> validation system, but also the data "consistency" depending
> where and why the form is used.

I'm not sure if I understand your use case completely, without seeing
what the 'validate_promotional_code' function does. Why can't this
validator be part of the form validation schema? I guess the request
param it checks has to be part of the form anyway to be submitted.

> I was blocked by the new "recursion guard" used in the the "validate"
> decorator, that now limit the validation of only ONE condition per
> HTTP request !
>
> Here is the patch I suggest
>
> --- controllers.py.orig 2008-09-16 00:43:07.000000000 +0200
> +++ controllers.py 2008-09-22 16:50:37.000000000 +0200
> @@ -147,8 +147,9 @@
> init_form = lambda self: form
>
> def validate(func, *args, **kw):
> - # do not validate a second time if already validated
> - if hasattr(request, 'validation_state'):
> + # ASX this is a more simplistic recursion guard
> + request.validation_count=getattr(request,
> 'validation_count', 0)+1

Please follow PEP8, i.e.:

request.validation_count = getattr(request, 'validation_count', 0) + 1

> + if request.validation_count>15:
^^^^

Where does that number come from? What's the difference between
arbitrarily setting the maximum number of validations to one or fifteen?

> And for free, here is a patch that add the name of the module and
> class of the function, instead of just the
> name of the function in the log

This is an independent issue, please open a separate ticket for this.


Chris

Florent Aide

unread,
Sep 22, 2008, 4:18:32 PM9/22/08
to turbo...@googlegroups.com
On Mon, Sep 22, 2008 at 7:47 PM, aspineux <aspi...@gmail.com> wrote:
>
> http://trac.turbogears.org/ticket/1993

Thanks.
Do not assign tickets to me though, this will prevent your casual
committer doing a ticket review to grab and work on it because they'll
assume I'm already working on it.

>
>>
>> Alain, would you mind if this patch only went in 1.1 and not in 1.0 ?
>
> It fit the 1.0.7

What does it mean ? 1.0 is in bugfix only and obviously this is a new
kind of feature. Thus my question. I don't see a compelling reason to
include this in 1.0.

Florent.

Reply all
Reply to author
Forward
0 new messages