Development of an internal social media platform with personalised dashboards for students
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.

deprecation.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import inspect
  2. import warnings
  3. class RemovedInDjango30Warning(PendingDeprecationWarning):
  4. pass
  5. class RemovedInNextVersionWarning(DeprecationWarning):
  6. pass
  7. class warn_about_renamed_method:
  8. def __init__(self, class_name, old_method_name, new_method_name, deprecation_warning):
  9. self.class_name = class_name
  10. self.old_method_name = old_method_name
  11. self.new_method_name = new_method_name
  12. self.deprecation_warning = deprecation_warning
  13. def __call__(self, f):
  14. def wrapped(*args, **kwargs):
  15. warnings.warn(
  16. "`%s.%s` is deprecated, use `%s` instead." %
  17. (self.class_name, self.old_method_name, self.new_method_name),
  18. self.deprecation_warning, 2)
  19. return f(*args, **kwargs)
  20. return wrapped
  21. class RenameMethodsBase(type):
  22. """
  23. Handles the deprecation paths when renaming a method.
  24. It does the following:
  25. 1) Define the new method if missing and complain about it.
  26. 2) Define the old method if missing.
  27. 3) Complain whenever an old method is called.
  28. See #15363 for more details.
  29. """
  30. renamed_methods = ()
  31. def __new__(cls, name, bases, attrs):
  32. new_class = super().__new__(cls, name, bases, attrs)
  33. for base in inspect.getmro(new_class):
  34. class_name = base.__name__
  35. for renamed_method in cls.renamed_methods:
  36. old_method_name = renamed_method[0]
  37. old_method = base.__dict__.get(old_method_name)
  38. new_method_name = renamed_method[1]
  39. new_method = base.__dict__.get(new_method_name)
  40. deprecation_warning = renamed_method[2]
  41. wrapper = warn_about_renamed_method(class_name, *renamed_method)
  42. # Define the new method if missing and complain about it
  43. if not new_method and old_method:
  44. warnings.warn(
  45. "`%s.%s` method should be renamed `%s`." %
  46. (class_name, old_method_name, new_method_name),
  47. deprecation_warning, 2)
  48. setattr(base, new_method_name, old_method)
  49. setattr(base, old_method_name, wrapper(old_method))
  50. # Define the old method as a wrapped call to the new method.
  51. if not old_method and new_method:
  52. setattr(base, old_method_name, wrapper(new_method))
  53. return new_class
  54. class DeprecationInstanceCheck(type):
  55. def __instancecheck__(self, instance):
  56. warnings.warn(
  57. "`%s` is deprecated, use `%s` instead." % (self.__name__, self.alternative),
  58. self.deprecation_warning, 2
  59. )
  60. return super().__instancecheck__(instance)
  61. class MiddlewareMixin:
  62. def __init__(self, get_response=None):
  63. self.get_response = get_response
  64. super().__init__()
  65. def __call__(self, request):
  66. response = None
  67. if hasattr(self, 'process_request'):
  68. response = self.process_request(request)
  69. response = response or self.get_response(request)
  70. if hasattr(self, 'process_response'):
  71. response = self.process_response(request, response)
  72. return response