2005-10-09 05:44:37 +08:00
|
|
|
"Functions that help with dynamically creating decorators for views."
|
|
|
|
|
2017-01-20 00:28:30 +08:00
|
|
|
# For backwards compatibility in Django 2.0.
|
|
|
|
from contextlib import ContextDecorator # noqa
|
2015-01-28 20:35:27 +08:00
|
|
|
from functools import WRAPPER_ASSIGNMENTS, update_wrapper, wraps
|
2009-09-22 06:31:51 +08:00
|
|
|
|
2013-03-12 18:28:01 +08:00
|
|
|
|
2010-11-20 13:10:13 +08:00
|
|
|
class classonlymethod(classmethod):
|
2015-10-26 23:31:16 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
2010-11-20 13:10:13 +08:00
|
|
|
if instance is not None:
|
2014-08-15 21:03:43 +08:00
|
|
|
raise AttributeError("This method is available only on the class, not on instances.")
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().__get__(instance, cls)
|
2009-10-16 04:25:20 +08:00
|
|
|
|
2013-03-12 18:28:01 +08:00
|
|
|
|
2018-04-17 01:38:37 +08:00
|
|
|
def _update_method_wrapper(_wrapper, decorator):
|
|
|
|
# _multi_decorate()'s bound_method isn't available in this scope. Cheat by
|
|
|
|
# using it on a dummy function.
|
|
|
|
@decorator
|
|
|
|
def dummy(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
update_wrapper(_wrapper, dummy)
|
|
|
|
|
|
|
|
|
|
|
|
def _multi_decorate(decorators, method):
|
|
|
|
"""
|
|
|
|
Decorate `method` with one or more function decorators. `decorators` can be
|
|
|
|
a single decorator or an iterable of decorators.
|
|
|
|
"""
|
|
|
|
if hasattr(decorators, '__iter__'):
|
|
|
|
# Apply a list/tuple of decorators if 'decorators' is one. Decorator
|
|
|
|
# functions are applied so that the call order is the same as the
|
|
|
|
# order in which they appear in the iterable.
|
|
|
|
decorators = decorators[::-1]
|
|
|
|
else:
|
|
|
|
decorators = [decorators]
|
|
|
|
|
|
|
|
def _wrapper(self, *args, **kwargs):
|
|
|
|
# bound_method has the signature that 'decorator' expects i.e. no
|
|
|
|
# 'self' argument.
|
|
|
|
bound_method = method.__get__(self, type(self))
|
|
|
|
for dec in decorators:
|
|
|
|
bound_method = dec(bound_method)
|
|
|
|
return bound_method(*args, **kwargs)
|
|
|
|
|
|
|
|
# Copy any attributes that a decorator adds to the function it decorates.
|
|
|
|
for dec in decorators:
|
|
|
|
_update_method_wrapper(_wrapper, dec)
|
|
|
|
# Preserve any existing attributes of 'method', including the name.
|
|
|
|
update_wrapper(_wrapper, method)
|
|
|
|
return _wrapper
|
|
|
|
|
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
def method_decorator(decorator, name=''):
|
2009-09-22 06:31:51 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Convert a function decorator into a method decorator
|
2009-09-22 06:31:51 +08:00
|
|
|
"""
|
2015-07-22 04:54:37 +08:00
|
|
|
# 'obj' can be a class or a function. If 'obj' is a function at the time it
|
|
|
|
# is passed to _dec, it will eventually be a method of the class it is
|
|
|
|
# defined on. If 'obj' is a class, the 'name' is required to be the name
|
|
|
|
# of the method that will be decorated.
|
|
|
|
def _dec(obj):
|
2018-04-17 01:38:37 +08:00
|
|
|
if not isinstance(obj, type):
|
|
|
|
return _multi_decorate(decorator, obj)
|
|
|
|
if not (name and hasattr(obj, name)):
|
|
|
|
raise ValueError(
|
|
|
|
"The keyword argument `name` must be the name of a method "
|
|
|
|
"of the decorated class: %s. Got '%s' instead." % (obj, name)
|
|
|
|
)
|
|
|
|
method = getattr(obj, name)
|
|
|
|
if not callable(method):
|
|
|
|
raise TypeError(
|
|
|
|
"Cannot decorate '%s' as it isn't a callable attribute of "
|
|
|
|
"%s (%s)." % (name, obj, method)
|
|
|
|
)
|
|
|
|
_wrapper = _multi_decorate(decorator, method)
|
|
|
|
setattr(obj, name, _wrapper)
|
|
|
|
return obj
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2015-08-11 19:35:50 +08:00
|
|
|
# Don't worry about making _dec look similar to a list/tuple as it's rather
|
|
|
|
# meaningless.
|
|
|
|
if not hasattr(decorator, '__iter__'):
|
2017-01-22 02:20:17 +08:00
|
|
|
update_wrapper(_dec, decorator)
|
2010-02-09 23:02:39 +08:00
|
|
|
# Change the name to aid debugging.
|
2018-04-17 01:38:37 +08:00
|
|
|
obj = decorator if hasattr(decorator, '__name__') else decorator.__class__
|
|
|
|
_dec.__name__ = 'method_decorator(%s)' % obj.__name__
|
2010-02-09 23:02:39 +08:00
|
|
|
return _dec
|
2009-09-22 06:31:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def decorator_from_middleware_with_args(middleware_class):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Like decorator_from_middleware, but return a function
|
2009-09-22 06:31:51 +08:00
|
|
|
that accepts the arguments to be passed to the middleware_class.
|
|
|
|
Use like::
|
|
|
|
|
2009-09-23 05:21:51 +08:00
|
|
|
cache_page = decorator_from_middleware_with_args(CacheMiddleware)
|
2009-09-22 06:31:51 +08:00
|
|
|
# ...
|
|
|
|
|
|
|
|
@cache_page(3600)
|
|
|
|
def my_view(request):
|
|
|
|
# ...
|
|
|
|
"""
|
|
|
|
return make_middleware_decorator(middleware_class)
|
2007-07-05 19:10:27 +08:00
|
|
|
|
2010-02-09 23:02:39 +08:00
|
|
|
|
2005-10-09 05:44:37 +08:00
|
|
|
def decorator_from_middleware(middleware_class):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Given a middleware class (not an instance), return a view decorator. This
|
2009-09-22 06:31:51 +08:00
|
|
|
lets you use middleware functionality on a per-view basis. The middleware
|
|
|
|
is created with no params passed.
|
2005-10-09 05:44:37 +08:00
|
|
|
"""
|
2009-09-22 06:31:51 +08:00
|
|
|
return make_middleware_decorator(middleware_class)()
|
|
|
|
|
2010-05-13 19:11:27 +08:00
|
|
|
|
2017-01-22 02:20:17 +08:00
|
|
|
# Unused, for backwards compatibility in Django 2.0.
|
2010-03-12 21:06:13 +08:00
|
|
|
def available_attrs(fn):
|
|
|
|
"""
|
|
|
|
Return the list of functools-wrappable attributes on a callable.
|
2016-12-01 18:38:01 +08:00
|
|
|
This was required as a workaround for http://bugs.python.org/issue3445
|
2013-03-12 18:28:01 +08:00
|
|
|
under Python 2.
|
2010-03-12 21:06:13 +08:00
|
|
|
"""
|
2016-12-01 18:38:01 +08:00
|
|
|
return WRAPPER_ASSIGNMENTS
|
2010-02-09 23:02:39 +08:00
|
|
|
|
2010-05-13 19:11:27 +08:00
|
|
|
|
2009-09-22 06:31:51 +08:00
|
|
|
def make_middleware_decorator(middleware_class):
|
|
|
|
def _make_decorator(*m_args, **m_kwargs):
|
|
|
|
middleware = middleware_class(*m_args, **m_kwargs)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2009-09-22 06:31:51 +08:00
|
|
|
def _decorator(view_func):
|
2017-01-22 02:20:17 +08:00
|
|
|
@wraps(view_func)
|
2009-09-22 06:31:51 +08:00
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if hasattr(middleware, 'process_request'):
|
|
|
|
result = middleware.process_request(request)
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
if hasattr(middleware, 'process_view'):
|
|
|
|
result = middleware.process_view(request, view_func, args, kwargs)
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
try:
|
|
|
|
response = view_func(request, *args, **kwargs)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2009-09-22 06:31:51 +08:00
|
|
|
if hasattr(middleware, 'process_exception'):
|
|
|
|
result = middleware.process_exception(request, e)
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
raise
|
2011-05-25 05:28:43 +08:00
|
|
|
if hasattr(response, 'render') and callable(response.render):
|
|
|
|
if hasattr(middleware, 'process_template_response'):
|
|
|
|
response = middleware.process_template_response(request, response)
|
|
|
|
# Defer running of process_response until after the template
|
|
|
|
# has been rendered:
|
|
|
|
if hasattr(middleware, 'process_response'):
|
2016-01-24 00:47:07 +08:00
|
|
|
def callback(response):
|
|
|
|
return middleware.process_response(request, response)
|
2011-05-25 05:28:43 +08:00
|
|
|
response.add_post_render_callback(callback)
|
|
|
|
else:
|
|
|
|
if hasattr(middleware, 'process_response'):
|
|
|
|
return middleware.process_response(request, response)
|
2009-09-22 06:31:51 +08:00
|
|
|
return response
|
2011-05-02 00:46:02 +08:00
|
|
|
return _wrapped_view
|
2010-02-09 23:02:39 +08:00
|
|
|
return _decorator
|
2009-09-22 06:31:51 +08:00
|
|
|
return _make_decorator
|
2014-08-20 00:32:02 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class classproperty:
|
2015-06-11 04:57:51 +08:00
|
|
|
def __init__(self, method=None):
|
|
|
|
self.fget = method
|
|
|
|
|
2015-10-26 23:31:16 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
|
|
|
return self.fget(cls)
|
2015-06-11 04:57:51 +08:00
|
|
|
|
|
|
|
def getter(self, method):
|
|
|
|
self.fget = method
|
|
|
|
return self
|