Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#25269 closed New feature (fixed)

Allow method_decorator() to accept a list/tuple of decorators

Reported by: zauddelig Owned by: Tim Graham
Component: Utilities Version: 1.8
Severity: Normal Keywords: 1.9
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by zauddelig)

Hello,
The new method decorator allow to decorate a class directly. to augment its readability I would propose to let it accept an iterable of decorator functions.

This could be useful because it's common to decorate views using redundant sets of decorators inside the same project or subproject, one could argue that the same result is attained creating a new decorator or a Mixin, yet an iterable, i. e. a tuple, seem to me more plastic and maintainable than a class or a function.

This could be also useful if one intend to dynamically decorate dynamically created classes although this is most probably going to be useful in less than the 1% of the cases, so actually not significant.

The proposal introduces a polymorphic behaviour to method_decorator which should now accept both function and iterables, so both the following examples should be valid:

decorators = (
    blacklist_ip,
    request_is_cool,
    login_required,
)

@method_decorator(decorators,"dispatch")
class ProtectedView(View):
    # lot of meaningful code
    pass

@method_decorator(login_required, "dispatch")
class LessProtectedView(View):
    # lot of meaningful code
    pass

pull request: https://github.com/django/django/pull/5136

Change History (14)

comment:1 by zauddelig, 9 years ago

Component: UncategorizedUtilities

comment:2 by zauddelig, 9 years ago

Description: modified (diff)
Needs documentation: set

comment:3 by zauddelig, 9 years ago

Type: UncategorizedNew feature

comment:4 by Tim Graham, 9 years ago

Summary: Method decorator should accept a iterable of decoratorsmethod_decorator() should accept a iterable of decorators
Triage Stage: UnreviewedAccepted

comment:5 by zauddelig, 9 years ago

I updated the pull requested:

https://github.com/django/django/pull/5134

It was added some documentation to the new behavior pretty much mimicking the excellent one already done by zedr.

I will squash when the PR is accepted and before it is merged if that is not a problem.

Version 0, edited 9 years ago by zauddelig (next)

comment:6 by zauddelig, 9 years ago

Description: modified (diff)

comment:7 by zauddelig, 9 years ago

I have done one big mess up while rebasing I closed the PR and Opened another

comment:8 by Tim Graham, 9 years ago

Needs documentation: unset

comment:9 by Tim Graham, 9 years ago

Keywords: 1.9 added
Patch needs improvement: set

A few more updates are required but this seems to be in mostly good shape.

comment:10 by Tim Graham, 9 years ago

Owner: changed from nobody to Tim Graham
Status: newassigned

I'm updating the patch.

comment:11 by Tim Graham, 9 years ago

Patch needs improvement: unset
Summary: method_decorator() should accept a iterable of decoratorsAllow method_decorator() to accept a list/tuple of decorators

comment:12 by Berker Peksag, 9 years ago

Triage Stage: AcceptedReady for checkin

PR #5311 LGTM.

comment:13 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 186eb21d:

Fixed #25269 -- Allowed method_decorator() to accept a list/tuple of decorators.

comment:14 by Chris Jerdonek, 7 years ago

FYI, I filed ticket #29253 about the implementation of this feature.

Note: See TracTickets for help on using tickets.
Back to Top