Refs #25847 -- Removed support for User.is_(anonymous|authenticated) as methods.

Per deprecation timeline.
This commit is contained in:
Tim Graham 2016-12-31 11:46:40 -05:00
parent b70094f040
commit eba093e8b0
8 changed files with 7 additions and 142 deletions

View File

@ -12,7 +12,6 @@ from django.contrib.auth.hashers import (
)
from django.db import models
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.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
anonymous users.
"""
return CallableFalse
return False
@property
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
authenticated in templates.
"""
return CallableTrue
return True
def set_password(self, raw_password):
self.password = make_password(raw_password)

View File

@ -9,7 +9,6 @@ from django.core.mail import send_mail
from django.db import models
from django.db.models.manager import EmptyManager
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.translation import ugettext_lazy as _
@ -443,11 +442,11 @@ class AnonymousUser(object):
@property
def is_anonymous(self):
return CallableTrue
return True
@property
def is_authenticated(self):
return CallableFalse
return False
def get_username(self):
return self.username

View File

@ -84,49 +84,6 @@ class DeprecationInstanceCheck(type):
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):
def __init__(self, get_response=None):
self.get_response = get_response

View File

@ -137,11 +137,6 @@ Attributes
(representing the currently logged-in user), you should know this
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
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
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
.. versionadded:: 1.10

View File

@ -358,3 +358,6 @@ these features.
* The shim for supporting custom related manager classes without a
``_apply_rel_filters()`` method is removed.
* Using ``User.is_authenticated()`` and ``User.is_anonymous()`` as methods
rather than properties is no longer be supported.

View File

@ -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
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
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
: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)
Sets the user's password to the given raw string, taking care of the

View File

@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import warnings
from django.contrib.auth import get_user, get_user_model
from django.contrib.auth.models import AnonymousUser, User
from django.core.exceptions import ImproperlyConfigured
@ -56,26 +54,6 @@ class BasicTestCase(TestCase):
with self.assertRaises(IntegrityError):
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):
"Users can be created without an email"
u = User.objects.create_user('testuser1')
@ -101,26 +79,6 @@ class BasicTestCase(TestCase):
self.assertEqual(a.groups.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):
"Check the creation and properties of a superuser"
super = User.objects.create_superuser('super', 'super@example.com', 'super')

View File

@ -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)