Opened 9 years ago

Last modified 7 years ago

#25269 closed New feature

method_decorator() should accept a iterable of decorators — at Version 6

Reported by: zauddelig Owned by: nobody
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 (6)

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/5136

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.

Last edited 9 years ago by zauddelig (previous) (diff)

comment:6 by zauddelig, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top