Normalized decorator style for functools.wraps.

This commit is contained in:
Aymeric Augustin 2022-05-22 08:26:21 +02:00 committed by Mariusz Felisiak
parent aff649a3bd
commit 8cf4de206c
4 changed files with 12 additions and 6 deletions

View File

@ -12,13 +12,14 @@ def xframe_options_deny(view_func):
... ...
""" """
@wraps(view_func)
def wrapper_view(*args, **kwargs): def wrapper_view(*args, **kwargs):
resp = view_func(*args, **kwargs) resp = view_func(*args, **kwargs)
if resp.get("X-Frame-Options") is None: if resp.get("X-Frame-Options") is None:
resp["X-Frame-Options"] = "DENY" resp["X-Frame-Options"] = "DENY"
return resp return resp
return wraps(view_func)(wrapper_view) return wrapper_view
def xframe_options_sameorigin(view_func): def xframe_options_sameorigin(view_func):
@ -32,13 +33,14 @@ def xframe_options_sameorigin(view_func):
... ...
""" """
@wraps(view_func)
def wrapper_view(*args, **kwargs): def wrapper_view(*args, **kwargs):
resp = view_func(*args, **kwargs) resp = view_func(*args, **kwargs)
if resp.get("X-Frame-Options") is None: if resp.get("X-Frame-Options") is None:
resp["X-Frame-Options"] = "SAMEORIGIN" resp["X-Frame-Options"] = "SAMEORIGIN"
return resp return resp
return wraps(view_func)(wrapper_view) return wrapper_view
def xframe_options_exempt(view_func): def xframe_options_exempt(view_func):
@ -51,9 +53,10 @@ def xframe_options_exempt(view_func):
... ...
""" """
@wraps(view_func)
def wrapper_view(*args, **kwargs): def wrapper_view(*args, **kwargs):
resp = view_func(*args, **kwargs) resp = view_func(*args, **kwargs)
resp.xframe_options_exempt = True resp.xframe_options_exempt = True
return resp return resp
return wraps(view_func)(wrapper_view) return wrapper_view

View File

@ -8,8 +8,9 @@ def no_append_slash(view_func):
""" """
# view_func.should_append_slash = False would also work, but decorators are # view_func.should_append_slash = False would also work, but decorators are
# nicer if they don't have side effects, so return a new function. # nicer if they don't have side effects, so return a new function.
@wraps(view_func)
def wrapper_view(*args, **kwargs): def wrapper_view(*args, **kwargs):
return view_func(*args, **kwargs) return view_func(*args, **kwargs)
wrapper_view.should_append_slash = False wrapper_view.should_append_slash = False
return wraps(view_func)(wrapper_view) return wrapper_view

View File

@ -50,8 +50,9 @@ def csrf_exempt(view_func):
"""Mark a view function as being exempt from the CSRF view protection.""" """Mark a view function as being exempt from the CSRF view protection."""
# view_func.csrf_exempt = True would also work, but decorators are nicer # view_func.csrf_exempt = True would also work, but decorators are nicer
# if they don't have side effects, so return a new function. # if they don't have side effects, so return a new function.
@wraps(view_func)
def wrapper_view(*args, **kwargs): def wrapper_view(*args, **kwargs):
return view_func(*args, **kwargs) return view_func(*args, **kwargs)
wrapper_view.csrf_exempt = True wrapper_view.csrf_exempt = True
return wraps(view_func)(wrapper_view) return wrapper_view

View File

@ -159,10 +159,11 @@ class DecoratorsTest(TestCase):
# For testing method_decorator, a decorator that assumes a single argument. # For testing method_decorator, a decorator that assumes a single argument.
# We will get type arguments if there is a mismatch in the number of arguments. # We will get type arguments if there is a mismatch in the number of arguments.
def simple_dec(func): def simple_dec(func):
@wraps(func)
def wrapper(arg): def wrapper(arg):
return func("test:" + arg) return func("test:" + arg)
return wraps(func)(wrapper) return wrapper
simple_dec_m = method_decorator(simple_dec) simple_dec_m = method_decorator(simple_dec)