Opened 9 years ago

Last modified 7 years ago

#25269 closed New feature

Method decorator should accept a iterable of decorators — at Version 2

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

Change History (2)

comment:1 by zauddelig, 9 years ago

Component: UncategorizedUtilities

comment:2 by zauddelig, 9 years ago

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