Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/views/decorators/debug.py: 58%
28 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import functools
3from django.http import HttpRequest
6def sensitive_variables(*variables):
7 """
8 Indicate which variables used in the decorated function are sensitive so
9 that those variables can later be treated in a special way, for example
10 by hiding them when logging unhandled exceptions.
12 Accept two forms:
14 * with specified variable names:
16 @sensitive_variables('user', 'password', 'credit_card')
17 def my_function(user):
18 password = user.pass_word
19 credit_card = user.credit_card_number
20 ...
22 * without any specified variable names, in which case consider all
23 variables are sensitive:
25 @sensitive_variables()
26 def my_function()
27 ...
28 """
29 if len(variables) == 1 and callable(variables[0]): 29 ↛ 30line 29 didn't jump to line 30, because the condition on line 29 was never true
30 raise TypeError(
31 "sensitive_variables() must be called to use it as a decorator, "
32 "e.g., use @sensitive_variables(), not @sensitive_variables."
33 )
35 def decorator(func):
36 @functools.wraps(func)
37 def sensitive_variables_wrapper(*func_args, **func_kwargs):
38 if variables: 38 ↛ 41line 38 didn't jump to line 41, because the condition on line 38 was never false
39 sensitive_variables_wrapper.sensitive_variables = variables
40 else:
41 sensitive_variables_wrapper.sensitive_variables = "__ALL__"
42 return func(*func_args, **func_kwargs)
44 return sensitive_variables_wrapper
46 return decorator
49def sensitive_post_parameters(*parameters):
50 """
51 Indicate which POST parameters used in the decorated view are sensitive,
52 so that those parameters can later be treated in a special way, for example
53 by hiding them when logging unhandled exceptions.
55 Accept two forms:
57 * with specified parameters:
59 @sensitive_post_parameters('password', 'credit_card')
60 def my_view(request):
61 pw = request.POST['password']
62 cc = request.POST['credit_card']
63 ...
65 * without any specified parameters, in which case consider all
66 variables are sensitive:
68 @sensitive_post_parameters()
69 def my_view(request)
70 ...
71 """
72 if len(parameters) == 1 and callable(parameters[0]): 72 ↛ 73line 72 didn't jump to line 73, because the condition on line 72 was never true
73 raise TypeError(
74 "sensitive_post_parameters() must be called to use it as a "
75 "decorator, e.g., use @sensitive_post_parameters(), not "
76 "@sensitive_post_parameters."
77 )
79 def decorator(view):
80 @functools.wraps(view)
81 def sensitive_post_parameters_wrapper(request, *args, **kwargs):
82 if not isinstance(request, HttpRequest):
83 raise TypeError(
84 "sensitive_post_parameters didn't receive an HttpRequest "
85 "object. If you are decorating a classmethod, make sure "
86 "to use @method_decorator."
87 )
88 if parameters:
89 request.sensitive_post_parameters = parameters
90 else:
91 request.sensitive_post_parameters = "__ALL__"
92 return view(request, *args, **kwargs)
94 return sensitive_post_parameters_wrapper
96 return decorator