compat.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import warnings
  2. try:
  3. from django.urls import reverse, reverse_lazy
  4. except ImportError:
  5. from django.core.urlresolvers import reverse, reverse_lazy # NOQA
  6. class RemovedInDjango20Warning(DeprecationWarning):
  7. pass
  8. class CallableBool: # pragma: no cover
  9. """
  10. An boolean-like object that is also callable for backwards compatibility.
  11. """
  12. do_not_call_in_templates = True
  13. def __init__(self, value):
  14. self.value = value
  15. def __bool__(self):
  16. return self.value
  17. def __call__(self):
  18. warnings.warn(
  19. "Using user.is_authenticated() and user.is_anonymous() as a method "
  20. "is deprecated. Remove the parentheses to use it as an attribute.",
  21. RemovedInDjango20Warning, stacklevel=2
  22. )
  23. return self.value
  24. def __nonzero__(self): # Python 2 compatibility
  25. return self.value
  26. def __repr__(self):
  27. return 'CallableBool(%r)' % self.value
  28. def __eq__(self, other):
  29. return self.value == other
  30. def __ne__(self, other):
  31. return self.value != other
  32. def __or__(self, other):
  33. return bool(self.value or other)
  34. def __hash__(self):
  35. return hash(self.value)
  36. CallableFalse = CallableBool(False)
  37. CallableTrue = CallableBool(True)