You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debug.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import functools
  2. from django.http import HttpRequest
  3. def sensitive_variables(*variables):
  4. """
  5. Indicate which variables used in the decorated function are sensitive so
  6. that those variables can later be treated in a special way, for example
  7. by hiding them when logging unhandled exceptions.
  8. Accept two forms:
  9. * with specified variable names:
  10. @sensitive_variables('user', 'password', 'credit_card')
  11. def my_function(user):
  12. password = user.pass_word
  13. credit_card = user.credit_card_number
  14. ...
  15. * without any specified variable names, in which case consider all
  16. variables are sensitive:
  17. @sensitive_variables()
  18. def my_function()
  19. ...
  20. """
  21. def decorator(func):
  22. @functools.wraps(func)
  23. def sensitive_variables_wrapper(*func_args, **func_kwargs):
  24. if variables:
  25. sensitive_variables_wrapper.sensitive_variables = variables
  26. else:
  27. sensitive_variables_wrapper.sensitive_variables = '__ALL__'
  28. return func(*func_args, **func_kwargs)
  29. return sensitive_variables_wrapper
  30. return decorator
  31. def sensitive_post_parameters(*parameters):
  32. """
  33. Indicate which POST parameters used in the decorated view are sensitive,
  34. so that those parameters can later be treated in a special way, for example
  35. by hiding them when logging unhandled exceptions.
  36. Accept two forms:
  37. * with specified parameters:
  38. @sensitive_post_parameters('password', 'credit_card')
  39. def my_view(request):
  40. pw = request.POST['password']
  41. cc = request.POST['credit_card']
  42. ...
  43. * without any specified parameters, in which case consider all
  44. variables are sensitive:
  45. @sensitive_post_parameters()
  46. def my_view(request)
  47. ...
  48. """
  49. def decorator(view):
  50. @functools.wraps(view)
  51. def sensitive_post_parameters_wrapper(request, *args, **kwargs):
  52. assert isinstance(request, HttpRequest), (
  53. "sensitive_post_parameters didn't receive an HttpRequest. "
  54. "If you are decorating a classmethod, be sure to use "
  55. "@method_decorator."
  56. )
  57. if parameters:
  58. request.sensitive_post_parameters = parameters
  59. else:
  60. request.sensitive_post_parameters = '__ALL__'
  61. return view(request, *args, **kwargs)
  62. return sensitive_post_parameters_wrapper
  63. return decorator