#10525 closed (invalid)
staff_member_required doesn't work for FormWizard / __call__()
Reported by: | Alpha1650 | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Keywords: | staff_member_required, FormWizard | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
The "staff_member_required" decorator doesn't work for FormWizard or in general for decorating methods as for example _ _call_ _(). That's because the decorator expects the first argument to be a request instance but with methods the first argument is self and the second argument is the request object.
The patch changes this by adding a check whether the object to be decorated is a function or a method. Then it decorates the appropriate way.
In order to decorate FormWizard's _ _call_ _() you have to do this:
from django.contrib.formtools.wizard import FormWizard from django.contrib.admin.views.decorators import staff_member_required class Wizard(FormWizard): # ... pass Wizard.__call__ = staff_member_required(Wizard.__call__)
I added my changes as a patch.
Alpha1650
Attachments (1)
Change History (5)
by , 16 years ago
Attachment: | staff_member_required.patch added |
---|
follow-up: 2 comment:1 by , 16 years ago
comment:2 by , 16 years ago
Replying to Alex:
You've got an immense amount of code duplication here(plus this same issue will be true for any other decorator), so I think a cleaner solution is needed.
I totally agree with you. I couldn't find a better solution. Maybe anyone else with better Python knowledge can. Maybe there's a general way to apply "normal" decorators to methods.
comment:3 by , 16 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
The trick isn't to apply the decorator to the method, but to the callable directly:
wizard = Wizard() protected_wizard = staff_member_required(wizard)
comment:4 by , 15 years ago
for this to work add the following to your wizard class:
@property def __name__(self): return self.__class__.__name__
You've got an immense amount of code duplication here(plus this same issue will be true for any other decorator), so I think a cleaner solution is needed.