Ticket #5701: dj-functools-decorators.diff
File dj-functools-decorators.diff, 9.2 KB (added by , 17 years ago) |
---|
-
django/views/decorators/http.py
3 3 """ 4 4 5 5 from django.utils.decorators import decorator_from_middleware 6 from django.utils.functional import wraps 6 7 from django.middleware.http import ConditionalGetMiddleware 7 8 from django.http import HttpResponseNotAllowed 8 9 … … 24 25 if request.method not in request_method_list: 25 26 return HttpResponseNotAllowed(request_method_list) 26 27 return func(request, *args, **kwargs) 27 return inner28 return wraps(func)(inner) 28 29 return decorator 29 30 30 31 require_GET = require_http_methods(["GET"]) -
django/views/decorators/vary.py
1 1 from django.utils.cache import patch_vary_headers 2 from django.utils.functional import wraps 2 3 3 4 def vary_on_headers(*headers): 4 5 """ … … 16 17 response = func(*args, **kwargs) 17 18 patch_vary_headers(response, headers) 18 19 return response 19 return inner_func20 return wraps(func)(inner_func) 20 21 return decorator 21 22 22 23 def vary_on_cookie(func): … … 32 33 response = func(*args, **kwargs) 33 34 patch_vary_headers(response, ('Cookie',)) 34 35 return response 35 return inner_func36 return wraps(func)(inner_func) -
django/views/decorators/cache.py
13 13 14 14 from django.utils.decorators import decorator_from_middleware 15 15 from django.utils.cache import patch_cache_control, add_never_cache_headers 16 from django.utils.functional import wraps 16 17 from django.middleware.cache import CacheMiddleware 17 18 18 19 cache_page = decorator_from_middleware(CacheMiddleware) … … 26 27 patch_cache_control(response, **kwargs) 27 28 return response 28 29 29 return _cache_controlled30 return wraps(viewfunc)(_cache_controlled) 30 31 31 32 return _cache_controller 32 33 … … 39 40 response = view_func(request, *args, **kwargs) 40 41 add_never_cache_headers(response) 41 42 return response 42 return _wrapped_view_func43 return wraps(view_func)(_wrapped_view_func) -
django/utils/functional.py
3 3 return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) 4 4 return _curried 5 5 6 #Begin from Python 2.5 functools.py ================================= 7 #N.B. swapping ``partial`` for ``curry`` 8 # to maintain backwards-compatibility in Django. 9 10 # update_wrapper() and wraps() are tools to help write 11 # wrapper functions that can handle naive introspection 12 13 WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__') 14 WRAPPER_UPDATES = ('__dict__',) 15 def update_wrapper(wrapper, 16 wrapped, 17 assigned = WRAPPER_ASSIGNMENTS, 18 updated = WRAPPER_UPDATES): 19 """Update a wrapper function to look like the wrapped function 20 21 wrapper is the function to be updated 22 wrapped is the original function 23 assigned is a tuple naming the attributes assigned directly 24 from the wrapped function to the wrapper function (defaults to 25 functools.WRAPPER_ASSIGNMENTS) 26 updated is a tuple naming the attributes off the wrapper that 27 are updated with the corresponding attribute from the wrapped 28 function (defaults to functools.WRAPPER_UPDATES) 29 """ 30 for attr in assigned: 31 setattr(wrapper, attr, getattr(wrapped, attr)) 32 for attr in updated: 33 getattr(wrapper, attr).update(getattr(wrapped, attr)) 34 # Return the wrapper so this can be used as a decorator via curry() 35 return wrapper 36 37 def wraps(wrapped, 38 assigned = WRAPPER_ASSIGNMENTS, 39 updated = WRAPPER_UPDATES): 40 """Decorator factory to apply update_wrapper() to a wrapper function 41 42 Returns a decorator that invokes update_wrapper() with the decorated 43 function as the wrapper argument and the arguments to wraps() as the 44 remaining arguments. Default arguments are as for update_wrapper(). 45 This is a convenience function to simplify applying curry() to 46 update_wrapper(). 47 """ 48 return curry(update_wrapper, wrapped=wrapped, 49 assigned=assigned, updated=updated) 50 51 #End from Python 2.5 functools.py =================================== 52 6 53 def memoize(func, cache, num_args): 7 54 """ 8 55 Wrap a function so that results for any argument tuple are stored in -
django/utils/decorators.py
1 1 "Functions that help with dynamically creating decorators for views." 2 2 3 3 import types 4 from django.utils.functional import wraps 4 5 5 6 def decorator_from_middleware(middleware_class): 6 7 """ … … 53 54 if result is not None: 54 55 return result 55 56 return response 56 return _wrapped_view57 return wraps(view_func)(_wrapped_view) 57 58 return _decorator_from_middleware -
django/contrib/admin/views/decorators.py
4 4 from django.contrib.auth import authenticate, login 5 5 from django.shortcuts import render_to_response 6 6 from django.utils.translation import ugettext_lazy, ugettext as _ 7 from django.utils.functional import wraps 7 8 import base64, datetime, md5 8 9 import cPickle as pickle 9 10 … … 103 104 else: 104 105 return _display_login_form(request, ERROR_MESSAGE) 105 106 106 return _checklogin 107 return wraps(view_func)(_checklogin) 108 No newline at end of file -
django/contrib/auth/decorators.py
1 1 from django.contrib.auth import REDIRECT_FIELD_NAME 2 2 from django.http import HttpResponseRedirect 3 3 from django.utils.http import urlquote 4 from django.utils.functional import wraps 4 5 5 6 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): 6 7 """ … … 19 20 _checklogin.__doc__ = view_func.__doc__ 20 21 _checklogin.__dict__ = view_func.__dict__ 21 22 22 return _checklogin23 return wraps(view_func)(_checklogin) 23 24 return _dec 24 25 25 26 def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME): -
tests/regressiontests/decorators/tests.py
1 """ 2 >>> fully_decorated.__module__ 3 'regressiontests.decorators.tests' 4 >>> fully_decorated.__name__ 5 'fully_decorated' 6 >>> fully_decorated.__doc__ 7 'Expected __doc__' 8 >>> fully_decorated.__dict__['anything'] 9 'Expected __dict__' 10 """ 11 from django.views.decorators.http import require_http_methods 12 from django.views.decorators.vary import vary_on_headers, vary_on_cookie 13 from django.views.decorators.cache import cache_page, never_cache, cache_control 14 from django.contrib.auth.decorators import user_passes_test 15 from django.contrib.admin.views.decorators import staff_member_required 16 17 def fully_decorated(request): 18 """Expected __doc__""" 19 return HttpResponse('<html><body>dummy</body></html>') 20 fully_decorated.anything = "Expected __dict__" 21 22 fully_decorated = require_http_methods(["GET"])(fully_decorated) 23 24 fully_decorated = vary_on_headers('Accept-language')(fully_decorated) 25 fully_decorated = vary_on_cookie(fully_decorated) 26 27 fully_decorated = cache_page(60*15)(fully_decorated) 28 fully_decorated = cache_control(private=True)(fully_decorated) 29 fully_decorated = never_cache(fully_decorated) 30 31 fully_decorated = user_passes_test(lambda u:True)(fully_decorated) 32 fully_decorated = staff_member_required(fully_decorated) 33 34 if __name__ == "__main__": 35 import doctest 36 doctest.testmod() -
tests/regressiontests/views/views.py
4 4 def index_page(request): 5 5 """Dummy index page""" 6 6 return HttpResponse('<html><body>Dummy page</body></html>') 7 7