Code

Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#10525 closed (invalid)

staff_member_required doesn't work for FormWizard / __call__()

Reported by: Alpha1650 Owned by: nobody
Component: contrib.admin Version: master
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: UI/UX:

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 5 years ago.

Download all attachments as: .zip

Change History (5)

Changed 5 years ago by anonymous

comment:1 follow-up: Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

comment:2 in reply to: ↑ 1 Changed 5 years ago by anonymous

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 Changed 5 years ago by jacob

  • Resolution set to invalid
  • Status changed from new to 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 Changed 4 years ago by ckd

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

    @property
    def __name__(self):
        return self.__class__.__name__

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.