Knut Nesheim wrote:
> 27 sep 2008 kl. 11.58 skrev Steve Holden:
>
>   
>> Knut Nesheim wrote:
>>     
>>> Hi!
>>>
>>> We're trying to build a multi-page form using FormWizard. We need to
>>> authorize the user first. In other views we use the user_passes_test
>>> decorator, however the documented way of using FormWizard is to
>>> include it directly in the urlconf:
>>>
>>> (r'^contact/$', ContactWizard([ContactForm1, ContactForm2])),
>>>
>>> How can we use decorators with this?
>>>
>>>       
>> You can't. But remember, a decorator is only a shorthand for the
>> application of a function to another function. So you *should* be able
>> to use the following (though I haven't tested it):
>>
>> (r'^contact/$', login_required(ContactWizard)([ContactForm1,  
>> ContactForm2]))
>>     
>
>   
My suggestion was based on the observation that

@decorator
def function():
    ...

is equivalent to (and indeed is merely syntactic sugar, or rather
syntactic saccharine, for)

def function():
    ...
function = decorator(function)

> I couldn't get that to work, but the following did:
>
>       (r'^contact/$', login_required(ContactFormWizard([ContactFormPartOne,  
> ContactFormPartTwo])))
>   
> But it complained it couldn't find the __name__ attribute, so I added  
> the following to my FormWizard subclass:
>
>       def __name__(self):
>               """When using decorators, Django tries to get the name of the 
> function
>               and since we're a class, we'll fail. So add this method to  
> compensate."""
>               return 'OrderFormWizard'
>
> I have no idea how if this will create problems for us in the future,  
> but it does actually work.
>
>   
Well that's good, I guess, but I had overlooked the fact that FormWizard
is a class. I believe class decorators will be legal in 2.6. So what you
managed to do correctly was to manually decorate the instance, which is
what gets called when the URL is dispatched.
>>> We've tried
>>>
>>> (r'^contact/$', @login_required(ContactWizard([ContactForm1,
>>> ContactForm2]))),
>>>
>>> But it only gives
>>>
>>> Exception Type: TypeError at /order/add/ Exception Value: 'type'
>>> object is unsubscriptable
>>>
>>> Any help is appreciated.
>>>       
>> I am absolutely amazed that you don't get a syntax error on that  
>> statement.
>>     
>
> Oh, sorry. I pasted and edited inline.
>   
That was rather naughty, wasn't it?
> Thanks for your input!
>   
A pleasure. Thanks for the feedback.

regards
 Steve


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

Reply via email to