#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 )
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 , 9 years ago
Component: | Uncategorized → Utilities |
---|
comment:2 by , 9 years ago
Description: | modified (diff) |
---|---|
Needs documentation: | set |
comment:3 by , 9 years ago
Type: | Uncategorized → New feature |
---|
comment:4 by , 9 years ago
Summary: | Method decorator should accept a iterable of decorators → method_decorator() should accept a iterable of decorators |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:6 by , 9 years ago
Description: | modified (diff) |
---|
comment:7 by , 9 years ago
I have done one big mess up while rebasing I closed the PR and Opened another
comment:8 by , 9 years ago
Needs documentation: | unset |
---|
comment:9 by , 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:11 by , 9 years ago
Patch needs improvement: | unset |
---|---|
Summary: | method_decorator() should accept a iterable of decorators → Allow method_decorator() to accept a list/tuple of decorators |
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.