From 7bbde14d78238584d98367940e7f153be25776ec Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Thu, 1 Nov 2012 19:23:26 -0400 Subject: [PATCH] Use functools.wraps on the functions returned by our decorators This lets Django report the correct view name in errors (imported from commit b21347e7af39cda439125355f99f4fc63fc3bd2f) --- zephyr/decorator.py | 3 +++ zephyr/lib/cache.py | 4 ++++ zephyr/views.py | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/zephyr/decorator.py b/zephyr/decorator.py index e456fe71a7..39894a09c3 100644 --- a/zephyr/decorator.py +++ b/zephyr/decorator.py @@ -1,3 +1,5 @@ +from functools import wraps + import types class TornadoAsyncException(Exception): pass @@ -10,6 +12,7 @@ def returnResponse(value): raise _DefGen_Return(value) def asynchronous(method): + @wraps(method) def wrapper(request, *args, **kwargs): try: v = method(request, request._tornado_handler, *args, **kwargs) diff --git a/zephyr/lib/cache.py b/zephyr/lib/cache.py index 6de9b301ce..ed2550ef0a 100644 --- a/zephyr/lib/cache.py +++ b/zephyr/lib/cache.py @@ -1,3 +1,5 @@ +from functools import wraps + import django.core.cache def cache_with_key(keyfunc): @@ -11,6 +13,7 @@ def cache_with_key(keyfunc): def decorator(func): djcache = django.core.cache.cache + @wraps(func) def func_with_caching(*args, **kwargs): key = keyfunc(*args, **kwargs) val = djcache.get(key) @@ -36,6 +39,7 @@ def cache(func): func_uniqifier = '%s-%s' % (func.func_code.co_filename, func.func_name) + @wraps(func) def keyfunc(*args, **kwargs): # Django complains about spaces because memcached rejects them key = func_uniqifier + repr((args, kwargs)) diff --git a/zephyr/views.py b/zephyr/views.py index bd672f808d..4b3667a5a9 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -25,6 +25,8 @@ from zephyr.lib.avatar import gravatar_hash from confirmation.models import Confirmation +from functools import wraps + import datetime import simplejson import socket @@ -38,6 +40,7 @@ import base64 SERVER_GENERATION = int(time.time()) def require_post(view_func): + @wraps(view_func) def _wrapped_view_func(request, *args, **kwargs): if request.method != "POST": return json_error('This form can only be submitted by POST.') @@ -50,6 +53,7 @@ def require_post(view_func): def authenticated_api_view(view_func): @csrf_exempt @require_post + @wraps(view_func) def _wrapped_view_func(request, *args, **kwargs): try: user_profile = UserProfile.objects.get(user__email=request.POST.get("email")) @@ -65,6 +69,7 @@ def authenticated_api_view(view_func): # redirecting to a login page doesn't make sense for json views) def authenticated_json_view(view_func): @require_post + @wraps(view_func) def _wrapped_view_func(request, *args, **kwargs): if not request.user.is_authenticated(): return json_error("Not logged in")