diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2016-04-02 13:18:26 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-09 14:54:18 -0400 |
| commit | c1aec0feda73ede09503192a66f973598aef901d (patch) | |
| tree | f1e4c09f3e98177cfe78cc9039b300f8984e7aed /django | |
| parent | c16b9dd8e0ae757616e9accbaffecc521191ee98 (diff) | |
Fixed #25847 -- Made User.is_(anonymous|authenticated) properties.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/auth/__init__.py | 2 | ||||
| -rw-r--r-- | django/contrib/auth/backends.py | 4 | ||||
| -rw-r--r-- | django/contrib/auth/base_user.py | 7 | ||||
| -rw-r--r-- | django/contrib/auth/decorators.py | 2 | ||||
| -rw-r--r-- | django/contrib/auth/middleware.py | 4 | ||||
| -rw-r--r-- | django/contrib/auth/mixins.py | 2 | ||||
| -rw-r--r-- | django/contrib/auth/models.py | 7 | ||||
| -rw-r--r-- | django/contrib/auth/views.py | 2 | ||||
| -rw-r--r-- | django/contrib/flatpages/templatetags/flatpages.py | 2 | ||||
| -rw-r--r-- | django/contrib/flatpages/views.py | 2 | ||||
| -rw-r--r-- | django/utils/deprecation.py | 30 |
11 files changed, 50 insertions, 14 deletions
diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index 734c03731a..4f4f08987a 100644 --- a/django/contrib/auth/__init__.py +++ b/django/contrib/auth/__init__.py @@ -138,7 +138,7 @@ def logout(request): # Dispatch the signal before the user is logged out so the receivers have a # chance to find out *who* logged out. user = getattr(request, 'user', None) - if hasattr(user, 'is_authenticated') and not user.is_authenticated(): + if hasattr(user, 'is_authenticated') and not user.is_authenticated: user = None user_logged_out.send(sender=user.__class__, request=request, user=user) diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py index 43d8627d0c..f145a1929a 100644 --- a/django/contrib/auth/backends.py +++ b/django/contrib/auth/backends.py @@ -45,7 +45,7 @@ class ModelBackend(object): be either "group" or "user" to return permissions from `_get_group_permissions` or `_get_user_permissions` respectively. """ - if not user_obj.is_active or user_obj.is_anonymous() or obj is not None: + if not user_obj.is_active or user_obj.is_anonymous or obj is not None: return set() perm_cache_name = '_%s_perm_cache' % from_name @@ -73,7 +73,7 @@ class ModelBackend(object): return self._get_permissions(user_obj, obj, 'group') def get_all_permissions(self, user_obj, obj=None): - if not user_obj.is_active or user_obj.is_anonymous() or obj is not None: + if not user_obj.is_active or user_obj.is_anonymous or obj is not None: return set() if not hasattr(user_obj, '_perm_cache'): user_obj._perm_cache = self.get_user_permissions(user_obj) diff --git a/django/contrib/auth/base_user.py b/django/contrib/auth/base_user.py index 48ab950c43..c8f748cf02 100644 --- a/django/contrib/auth/base_user.py +++ b/django/contrib/auth/base_user.py @@ -10,6 +10,7 @@ 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 python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -79,19 +80,21 @@ class AbstractBaseUser(models.Model): def natural_key(self): return (self.get_username(),) + @property def is_anonymous(self): """ Always return False. This is a way of comparing User objects to anonymous users. """ - return False + return CallableFalse + @property def is_authenticated(self): """ Always return True. This is a way to tell if the user has been authenticated in templates. """ - return True + return CallableTrue def set_password(self, raw_password): self.password = make_password(raw_password) diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py index 6ab428e9cd..9c44108c89 100644 --- a/django/contrib/auth/decorators.py +++ b/django/contrib/auth/decorators.py @@ -43,7 +43,7 @@ def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login to the log-in page if necessary. """ actual_decorator = user_passes_test( - lambda u: u.is_authenticated(), + lambda u: u.is_authenticated, login_url=login_url, redirect_field_name=redirect_field_name ) diff --git a/django/contrib/auth/middleware.py b/django/contrib/auth/middleware.py index c829038f89..476729c147 100644 --- a/django/contrib/auth/middleware.py +++ b/django/contrib/auth/middleware.py @@ -70,13 +70,13 @@ class RemoteUserMiddleware(object): # If specified header doesn't exist then remove any existing # authenticated remote-user, or return (leaving request.user set to # AnonymousUser by the AuthenticationMiddleware). - if self.force_logout_if_no_header and request.user.is_authenticated(): + if self.force_logout_if_no_header and request.user.is_authenticated: self._remove_invalid_user(request) return # If the user is already authenticated and that user is the user we are # getting passed in the headers, then the correct user is already # persisted in the session and we don't need to continue. - if request.user.is_authenticated(): + if request.user.is_authenticated: if request.user.get_username() == self.clean_username(username, request): return else: diff --git a/django/contrib/auth/mixins.py b/django/contrib/auth/mixins.py index d98c909112..4a7759435b 100644 --- a/django/contrib/auth/mixins.py +++ b/django/contrib/auth/mixins.py @@ -51,7 +51,7 @@ class LoginRequiredMixin(AccessMixin): CBV mixin which verifies that the current user is authenticated. """ def dispatch(self, request, *args, **kwargs): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return self.handle_no_permission() return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index 0600b96774..2f3e297ba6 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -10,6 +10,7 @@ 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 _ @@ -438,11 +439,13 @@ class AnonymousUser(object): def has_module_perms(self, module): return _user_has_module_perms(self, module) + @property def is_anonymous(self): - return True + return CallableTrue + @property def is_authenticated(self): - return False + return CallableFalse def get_username(self): return self.username diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py index ff6643b035..3cf7c36b8b 100644 --- a/django/contrib/auth/views.py +++ b/django/contrib/auth/views.py @@ -68,7 +68,7 @@ def login(request, template_name='registration/login.html', """ redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, '')) - if redirect_authenticated_user and request.user.is_authenticated(): + if redirect_authenticated_user and request.user.is_authenticated: redirect_to = _get_login_redirect_url(request, redirect_to) if redirect_to == request.path: raise ValueError( diff --git a/django/contrib/flatpages/templatetags/flatpages.py b/django/contrib/flatpages/templatetags/flatpages.py index db82c4aa2c..3abe6f5433 100644 --- a/django/contrib/flatpages/templatetags/flatpages.py +++ b/django/contrib/flatpages/templatetags/flatpages.py @@ -33,7 +33,7 @@ class FlatpageNode(template.Node): # was provided, filter the list to only public flatpages. if self.user: user = self.user.resolve(context) - if not user.is_authenticated(): + if not user.is_authenticated: flatpages = flatpages.filter(registration_required=False) else: flatpages = flatpages.filter(registration_required=False) diff --git a/django/contrib/flatpages/views.py b/django/contrib/flatpages/views.py index 10e13a73be..6df0791837 100644 --- a/django/contrib/flatpages/views.py +++ b/django/contrib/flatpages/views.py @@ -52,7 +52,7 @@ def render_flatpage(request, f): """ # If registration is required for accessing this page, and the user isn't # logged in, redirect to the login page. - if f.registration_required and not request.user.is_authenticated(): + if f.registration_required and not request.user.is_authenticated: from django.contrib.auth.views import redirect_to_login return redirect_to_login(request.path) if f.template_name: diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py index 905bbc4f6d..70216e8c5b 100644 --- a/django/utils/deprecation.py +++ b/django/utils/deprecation.py @@ -79,3 +79,33 @@ class DeprecationInstanceCheck(type): self.deprecation_warning, 2 ) 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 + +CallableFalse = CallableBool(False) +CallableTrue = CallableBool(True) |
