Opened 16 years ago

Closed 16 years ago

Last modified 15 years ago

#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)

staff_member_required.patch (3.6 KB ) - added by anonymous 16 years ago.

Download all attachments as: .zip

Change History (5)

by anonymous, 16 years ago

Attachment: staff_member_required.patch added

comment:1 by Alex Gaynor, 16 years ago

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.

in reply to:  1 comment:2 by anonymous, 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 Jacob, 16 years ago

Resolution: invalid
Status: newclosed

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 ckd, 15 years ago

for this to work add the following to your wizard class:

    @property
    def __name__(self):
        return self.__class__.__name__
Note: See TracTickets for help on using tickets.
Back to Top