mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Refs #25847 -- Removed support for User.is_(anonymous|authenticated) as methods.
Per deprecation timeline.
This commit is contained in:
parent
b70094f040
commit
eba093e8b0
@ -12,7 +12,6 @@ from django.contrib.auth.hashers import (
|
|||||||
)
|
)
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.crypto import get_random_string, salted_hmac
|
from django.utils.crypto import get_random_string, salted_hmac
|
||||||
from django.utils.deprecation import CallableFalse, CallableTrue
|
|
||||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
@ -91,7 +90,7 @@ class AbstractBaseUser(models.Model):
|
|||||||
Always return False. This is a way of comparing User objects to
|
Always return False. This is a way of comparing User objects to
|
||||||
anonymous users.
|
anonymous users.
|
||||||
"""
|
"""
|
||||||
return CallableFalse
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_authenticated(self):
|
def is_authenticated(self):
|
||||||
@ -99,7 +98,7 @@ class AbstractBaseUser(models.Model):
|
|||||||
Always return True. This is a way to tell if the user has been
|
Always return True. This is a way to tell if the user has been
|
||||||
authenticated in templates.
|
authenticated in templates.
|
||||||
"""
|
"""
|
||||||
return CallableTrue
|
return True
|
||||||
|
|
||||||
def set_password(self, raw_password):
|
def set_password(self, raw_password):
|
||||||
self.password = make_password(raw_password)
|
self.password = make_password(raw_password)
|
||||||
|
@ -9,7 +9,6 @@ from django.core.mail import send_mail
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.manager import EmptyManager
|
from django.db.models.manager import EmptyManager
|
||||||
from django.utils import six, timezone
|
from django.utils import six, timezone
|
||||||
from django.utils.deprecation import CallableFalse, CallableTrue
|
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
@ -443,11 +442,11 @@ class AnonymousUser(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def is_anonymous(self):
|
def is_anonymous(self):
|
||||||
return CallableTrue
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_authenticated(self):
|
def is_authenticated(self):
|
||||||
return CallableFalse
|
return False
|
||||||
|
|
||||||
def get_username(self):
|
def get_username(self):
|
||||||
return self.username
|
return self.username
|
||||||
|
@ -84,49 +84,6 @@ class DeprecationInstanceCheck(type):
|
|||||||
return super(DeprecationInstanceCheck, self).__instancecheck__(instance)
|
return super(DeprecationInstanceCheck, self).__instancecheck__(instance)
|
||||||
|
|
||||||
|
|
||||||
class CallableBool:
|
|
||||||
"""
|
|
||||||
An boolean-like object that is also callable for backwards compatibility.
|
|
||||||
"""
|
|
||||||
do_not_call_in_templates = True
|
|
||||||
|
|
||||||
def __init__(self, value):
|
|
||||||
self.value = value
|
|
||||||
|
|
||||||
def __bool__(self):
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
def __call__(self):
|
|
||||||
warnings.warn(
|
|
||||||
"Using user.is_authenticated() and user.is_anonymous() as a method "
|
|
||||||
"is deprecated. Remove the parentheses to use it as an attribute.",
|
|
||||||
RemovedInDjango20Warning, stacklevel=2
|
|
||||||
)
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
def __nonzero__(self): # Python 2 compatibility
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return 'CallableBool(%r)' % self.value
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
return self.value == other
|
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return self.value != other
|
|
||||||
|
|
||||||
def __or__(self, other):
|
|
||||||
return bool(self.value or other)
|
|
||||||
|
|
||||||
def __hash__(self):
|
|
||||||
return hash(self.value)
|
|
||||||
|
|
||||||
|
|
||||||
CallableFalse = CallableBool(False)
|
|
||||||
CallableTrue = CallableBool(True)
|
|
||||||
|
|
||||||
|
|
||||||
class MiddlewareMixin(object):
|
class MiddlewareMixin(object):
|
||||||
def __init__(self, get_response=None):
|
def __init__(self, get_response=None):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
@ -137,11 +137,6 @@ Attributes
|
|||||||
(representing the currently logged-in user), you should know this
|
(representing the currently logged-in user), you should know this
|
||||||
attribute is ``True`` for any :class:`~models.User` instance.
|
attribute is ``True`` for any :class:`~models.User` instance.
|
||||||
|
|
||||||
.. versionchanged:: 1.10
|
|
||||||
|
|
||||||
In older versions, this was a method. Backwards-compatibility
|
|
||||||
support for using it as a method will be removed in Django 2.0.
|
|
||||||
|
|
||||||
.. attribute:: is_anonymous
|
.. attribute:: is_anonymous
|
||||||
|
|
||||||
Read-only attribute which is always ``False``. This is a way of
|
Read-only attribute which is always ``False``. This is a way of
|
||||||
@ -150,11 +145,6 @@ Attributes
|
|||||||
:attr:`~django.contrib.auth.models.User.is_authenticated` to this
|
:attr:`~django.contrib.auth.models.User.is_authenticated` to this
|
||||||
attribute.
|
attribute.
|
||||||
|
|
||||||
.. versionchanged:: 1.10
|
|
||||||
|
|
||||||
In older versions, this was a method. Backwards-compatibility
|
|
||||||
support for using it as a method will be removed in Django 2.0.
|
|
||||||
|
|
||||||
.. attribute:: username_validator
|
.. attribute:: username_validator
|
||||||
|
|
||||||
.. versionadded:: 1.10
|
.. versionadded:: 1.10
|
||||||
|
@ -358,3 +358,6 @@ these features.
|
|||||||
|
|
||||||
* The shim for supporting custom related manager classes without a
|
* The shim for supporting custom related manager classes without a
|
||||||
``_apply_rel_filters()`` method is removed.
|
``_apply_rel_filters()`` method is removed.
|
||||||
|
|
||||||
|
* Using ``User.is_authenticated()`` and ``User.is_anonymous()`` as methods
|
||||||
|
rather than properties is no longer be supported.
|
||||||
|
@ -695,11 +695,6 @@ The following attributes and methods are available on any subclass of
|
|||||||
(representing the currently logged-in user), you should know this
|
(representing the currently logged-in user), you should know this
|
||||||
attribute is ``True`` for any :class:`~models.User` instance.
|
attribute is ``True`` for any :class:`~models.User` instance.
|
||||||
|
|
||||||
.. versionchanged:: 1.10
|
|
||||||
|
|
||||||
In older versions, this was a method. Backwards-compatibility
|
|
||||||
support for using it as a method will be removed in Django 2.0.
|
|
||||||
|
|
||||||
.. attribute:: models.AbstractBaseUser.is_anonymous
|
.. attribute:: models.AbstractBaseUser.is_anonymous
|
||||||
|
|
||||||
Read-only attribute which is always ``False``. This is a way of
|
Read-only attribute which is always ``False``. This is a way of
|
||||||
@ -707,11 +702,6 @@ The following attributes and methods are available on any subclass of
|
|||||||
objects. Generally, you should prefer using
|
objects. Generally, you should prefer using
|
||||||
:attr:`~models.User.is_authenticated` to this attribute.
|
:attr:`~models.User.is_authenticated` to this attribute.
|
||||||
|
|
||||||
.. versionchanged:: 1.10
|
|
||||||
|
|
||||||
In older versions, this was a method. Backwards-compatibility
|
|
||||||
support for using it as a method will be removed in Django 2.0.
|
|
||||||
|
|
||||||
.. method:: models.AbstractBaseUser.set_password(raw_password)
|
.. method:: models.AbstractBaseUser.set_password(raw_password)
|
||||||
|
|
||||||
Sets the user's password to the given raw string, taking care of the
|
Sets the user's password to the given raw string, taking care of the
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.contrib.auth import get_user, get_user_model
|
from django.contrib.auth import get_user, get_user_model
|
||||||
from django.contrib.auth.models import AnonymousUser, User
|
from django.contrib.auth.models import AnonymousUser, User
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
@ -56,26 +54,6 @@ class BasicTestCase(TestCase):
|
|||||||
with self.assertRaises(IntegrityError):
|
with self.assertRaises(IntegrityError):
|
||||||
User.objects.create_user(omega_username)
|
User.objects.create_user(omega_username)
|
||||||
|
|
||||||
def test_is_anonymous_authenticated_method_deprecation(self):
|
|
||||||
deprecation_message = (
|
|
||||||
'Using user.is_authenticated() and user.is_anonymous() as a '
|
|
||||||
'method is deprecated. Remove the parentheses to use it as an '
|
|
||||||
'attribute.'
|
|
||||||
)
|
|
||||||
u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
|
|
||||||
# Backwards-compatibility callables
|
|
||||||
with warnings.catch_warnings(record=True) as warns:
|
|
||||||
warnings.simplefilter('always')
|
|
||||||
self.assertFalse(u.is_anonymous())
|
|
||||||
self.assertEqual(len(warns), 1)
|
|
||||||
self.assertEqual(str(warns[0].message), deprecation_message)
|
|
||||||
|
|
||||||
with warnings.catch_warnings(record=True) as warns:
|
|
||||||
warnings.simplefilter('always')
|
|
||||||
self.assertTrue(u.is_authenticated())
|
|
||||||
self.assertEqual(len(warns), 1)
|
|
||||||
self.assertEqual(str(warns[0].message), deprecation_message)
|
|
||||||
|
|
||||||
def test_user_no_email(self):
|
def test_user_no_email(self):
|
||||||
"Users can be created without an email"
|
"Users can be created without an email"
|
||||||
u = User.objects.create_user('testuser1')
|
u = User.objects.create_user('testuser1')
|
||||||
@ -101,26 +79,6 @@ class BasicTestCase(TestCase):
|
|||||||
self.assertEqual(a.groups.all().count(), 0)
|
self.assertEqual(a.groups.all().count(), 0)
|
||||||
self.assertEqual(a.user_permissions.all().count(), 0)
|
self.assertEqual(a.user_permissions.all().count(), 0)
|
||||||
|
|
||||||
def test_anonymous_user_is_anonymous_authenticated_method_deprecation(self):
|
|
||||||
a = AnonymousUser()
|
|
||||||
deprecation_message = (
|
|
||||||
'Using user.is_authenticated() and user.is_anonymous() as a '
|
|
||||||
'method is deprecated. Remove the parentheses to use it as an '
|
|
||||||
'attribute.'
|
|
||||||
)
|
|
||||||
# Backwards-compatibility callables
|
|
||||||
with warnings.catch_warnings(record=True) as warns:
|
|
||||||
warnings.simplefilter('always') # prevent warnings from appearing as errors
|
|
||||||
self.assertTrue(a.is_anonymous())
|
|
||||||
self.assertEqual(len(warns), 1)
|
|
||||||
self.assertEqual(str(warns[0].message), deprecation_message)
|
|
||||||
|
|
||||||
with warnings.catch_warnings(record=True) as warns:
|
|
||||||
warnings.simplefilter('always') # prevent warnings from appearing as errors
|
|
||||||
self.assertFalse(a.is_authenticated())
|
|
||||||
self.assertEqual(len(warns), 1)
|
|
||||||
self.assertEqual(str(warns[0].message), deprecation_message)
|
|
||||||
|
|
||||||
def test_superuser(self):
|
def test_superuser(self):
|
||||||
"Check the creation and properties of a superuser"
|
"Check the creation and properties of a superuser"
|
||||||
super = User.objects.create_superuser('super', 'super@example.com', 'super')
|
super = User.objects.create_superuser('super', 'super@example.com', 'super')
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
from django.test import SimpleTestCase
|
|
||||||
from django.utils.deprecation import CallableFalse, CallableTrue
|
|
||||||
|
|
||||||
|
|
||||||
class TestCallableBool(SimpleTestCase):
|
|
||||||
def test_true(self):
|
|
||||||
self.assertTrue(CallableTrue)
|
|
||||||
self.assertEqual(CallableTrue, True)
|
|
||||||
self.assertFalse(CallableTrue != True) # noqa: E712
|
|
||||||
self.assertNotEqual(CallableTrue, False)
|
|
||||||
|
|
||||||
def test_false(self):
|
|
||||||
self.assertFalse(CallableFalse)
|
|
||||||
self.assertEqual(CallableFalse, False)
|
|
||||||
self.assertFalse(CallableFalse != False) # noqa: E712
|
|
||||||
self.assertNotEqual(CallableFalse, True)
|
|
||||||
|
|
||||||
def test_or(self):
|
|
||||||
self.assertIs(CallableTrue | CallableTrue, True)
|
|
||||||
self.assertIs(CallableTrue | CallableFalse, True)
|
|
||||||
self.assertIs(CallableFalse | CallableTrue, True)
|
|
||||||
self.assertIs(CallableFalse | CallableFalse, False)
|
|
||||||
|
|
||||||
self.assertIs(CallableTrue | True, True)
|
|
||||||
self.assertIs(CallableTrue | False, True)
|
|
||||||
self.assertIs(CallableFalse | True, True)
|
|
||||||
self.assertFalse(CallableFalse | False, False)
|
|
||||||
|
|
||||||
def test_set_membership(self):
|
|
||||||
self.assertIs(CallableTrue in {True}, True)
|
|
||||||
self.assertIs(CallableFalse not in {True}, True)
|
|
Loading…
x
Reference in New Issue
Block a user