From c33ae8c9dd14f99d102447efe2283bf257ad78e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 14 Oct 2019 13:43:08 +0200
Subject: [PATCH 2/5] dj2: is_anonymous and is_authenticated are now properties
 (#36895)

remove RemovedInDjango20Warning:
Using user.is_authenticated() and user.is_anonymous() as a method is deprecated.
Remove the parentheses to use it as an attribute.
---
 combo/apps/dashboard/models.py     |  2 +-
 combo/apps/dashboard/views.py      |  2 +-
 combo/apps/family/models.py        |  2 +-
 combo/apps/fargo/models.py         |  2 +-
 combo/apps/lingo/models.py         | 12 ++++++------
 combo/apps/lingo/views.py          | 12 ++++++------
 combo/apps/newsletters/models.py   |  4 ++--
 combo/apps/notifications/models.py |  6 +++---
 combo/apps/pwa/views.py            |  2 +-
 combo/apps/wcs/models.py           |  2 +-
 combo/data/models.py               |  8 ++++----
 combo/profile/models.py            |  2 +-
 combo/profile/utils.py             |  2 ++
 combo/public/templatetags/combo.py |  4 ++--
 combo/public/views.py              |  6 +++---
 combo/urls_utils.py                |  2 +-
 combo/utils/requests_wrapper.py    |  2 +-
 combo/utils/urls.py                |  2 +-
 tests/test_lingo_remote_regie.py   |  3 +--
 tests/test_requests.py             |  3 +--
 tests/test_utils.py                |  3 +--
 tests/test_wcs.py                  |  8 ++++----
 22 files changed, 45 insertions(+), 46 deletions(-)

diff --git a/combo/apps/dashboard/models.py b/combo/apps/dashboard/models.py
index 4868be1..bcc9eb8 100644
--- a/combo/apps/dashboard/models.py
+++ b/combo/apps/dashboard/models.py
@@ -40,7 +40,7 @@ class DashboardCell(CellBase):
         return settings.COMBO_DASHBOARD_ENABLED
 
     def is_relevant(self, context):
-        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
+        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
             return False
         return True
 
diff --git a/combo/apps/dashboard/views.py b/combo/apps/dashboard/views.py
index 1ca10dc..706272b 100644
--- a/combo/apps/dashboard/views.py
+++ b/combo/apps/dashboard/views.py
@@ -50,7 +50,7 @@ def dashboard_success(request, dashboard, cell_data):
 
 class DashboardAddTileView(View):
     def get(self, request, *args, **kwargs):
-        if not request.user.is_authenticated():
+        if not request.user.is_authenticated:
             raise PermissionDenied()
 
         dashboard = DashboardCell.objects.all()[0]
diff --git a/combo/apps/family/models.py b/combo/apps/family/models.py
index 00505ac..63d9f7d 100644
--- a/combo/apps/family/models.py
+++ b/combo/apps/family/models.py
@@ -41,7 +41,7 @@ class FamilyInfosCell(CellBase):
         if context.get('placeholder_search_mode'):
             return {}
         user = self.get_concerned_user(context)
-        if not user or user.is_anonymous():
+        if not user or user.is_anonymous:
             return {}
         response = get_family(user=user,
                                    raise_if_not_cached=not(context.get('synchronous')))
diff --git a/combo/apps/fargo/models.py b/combo/apps/fargo/models.py
index 9801341..32f2a49 100644
--- a/combo/apps/fargo/models.py
+++ b/combo/apps/fargo/models.py
@@ -53,7 +53,7 @@ class RecentDocumentsCell(CellBase):
                 widgets=self.get_form_widgets())
 
     def is_visible(self, user=None):
-        if not user or user.is_anonymous():
+        if not user or user.is_anonymous:
             return False
         return super(RecentDocumentsCell, self).is_visible(user)
 
diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py
index ed83712..309f28e 100644
--- a/combo/apps/lingo/models.py
+++ b/combo/apps/lingo/models.py
@@ -526,12 +526,12 @@ class LingoBasketCell(CellBase):
         return Regie.objects.count() > 0
 
     def is_relevant(self, context):
-        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
+        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
             return False
         return BasketItem.get_items_to_be_paid(context['request'].user).count() > 0
 
     def get_badge(self, context):
-        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
+        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
             return
         items = BasketItem.get_items_to_be_paid(context['request'].user)
         if not items:
@@ -569,7 +569,7 @@ class LingoRecentTransactionsCell(CellBase):
         return Regie.objects.count() > 0
 
     def is_relevant(self, context):
-        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
+        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
             return False
         transactions = Transaction.objects.filter(
                 user=context['request'].user,
@@ -598,12 +598,12 @@ class LingoBasketLinkCell(CellBase):
         return Regie.objects.count() > 0
 
     def is_relevant(self, context):
-        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
+        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
             return False
         return BasketItem.get_items_to_be_paid(context['request'].user).count() > 0
 
     def render(self, context):
-        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
+        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
             return ''
         try:
             context['basket_url'] = LingoBasketCell.objects.all()[0].page.get_online_url()
@@ -636,7 +636,7 @@ class Items(CellBase):
         return Regie.objects.exclude(webservice_url='').count() > 0
 
     def is_relevant(self, context):
-        return (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated())
+        return (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated)
 
     def get_default_form_class(self):
         fields = ['title', 'text']
diff --git a/combo/apps/lingo/views.py b/combo/apps/lingo/views.py
index 18670f6..8804dca 100644
--- a/combo/apps/lingo/views.py
+++ b/combo/apps/lingo/views.py
@@ -329,7 +329,7 @@ class PayMixin(object):
             if item.regie != regie:
                 messages.error(request, _(u'Invalid grouping for basket items.'))
                 return HttpResponseRedirect(next_url)
-        user = request.user if request.user.is_authenticated() else None
+        user = request.user if request.user.is_authenticated else None
         transaction = Transaction()
         if user:
             transaction.user = user
@@ -383,7 +383,7 @@ class PayView(PayMixin, View):
     def post(self, request, *args, **kwargs):
         regie_id = request.POST.get('regie')
         next_url = request.POST.get('next_url') or '/'
-        user = request.user if request.user.is_authenticated() else None
+        user = request.user if request.user.is_authenticated else None
         remote_items = []
         items = []
         if regie_id and Regie.objects.get(pk=regie_id).is_remote():
@@ -430,7 +430,7 @@ class PayView(PayMixin, View):
 class BasketItemPayView(PayMixin, View):
     def get(self, request, *args, **kwargs):
         next_url = request.GET.get('next_url') or '/'
-        if not (request.user and request.user.is_authenticated()):
+        if not (request.user and request.user.is_authenticated):
             return HttpResponseForbidden(_('No item payment allowed for anonymous users.'))
 
         item = BasketItem.objects.get(pk=kwargs['item_id'])
@@ -612,7 +612,7 @@ class ReturnView(PaymentView):
                 return HttpResponseRedirect(redirect_url)
 
         # return to basket page if there are still items to pay
-        if request.user.is_authenticated():
+        if request.user.is_authenticated:
             remaining_basket_items = BasketItem.get_items_to_be_paid(
                 user=self.request.user).count()
             if remaining_basket_items:
@@ -696,11 +696,11 @@ class CancelItemView(DetailView):
         return context
 
     def get_queryset(self):
-        user = self.request.user if self.request.user.is_authenticated() else None
+        user = self.request.user if self.request.user.is_authenticated else None
         return BasketItem.get_items_to_be_paid(user=user)
 
     def post(self, request, *args, **kwargs):
-        if not request.user.is_authenticated():
+        if not request.user.is_authenticated:
             messages.error(request, _('An error occured when removing the item. '
                                       '(no authenticated user)'))
             return HttpResponseRedirect(get_basket_url())
diff --git a/combo/apps/newsletters/models.py b/combo/apps/newsletters/models.py
index 6a9feee..13138b1 100644
--- a/combo/apps/newsletters/models.py
+++ b/combo/apps/newsletters/models.py
@@ -136,12 +136,12 @@ class NewslettersCell(CellBase):
 
     def render(self, context):
         user = context.get('user')
-        if user and user.is_authenticated():
+        if user and user.is_authenticated:
             form = NewslettersManageForm(instance=self, request=context['request'])
             context['form'] = form
         return super(NewslettersCell, self).render(context)
 
     def is_visible(self, user=None):
-        if user is None or not user.is_authenticated():
+        if user is None or not user.is_authenticated:
             return False
         return super(NewslettersCell, self).is_visible(user)
diff --git a/combo/apps/notifications/models.py b/combo/apps/notifications/models.py
index e3bf7a2..21a08fb 100644
--- a/combo/apps/notifications/models.py
+++ b/combo/apps/notifications/models.py
@@ -172,14 +172,14 @@ class NotificationsCell(CellBase):
         verbose_name = _('User Notifications')
 
     def is_visible(self, user=None):
-        if user is None or not user.is_authenticated():
+        if user is None or not user.is_authenticated:
             return False
         return super(NotificationsCell, self).is_visible(user)
 
     def get_cell_extra_context(self, context):
         extra_context = super(NotificationsCell, self).get_cell_extra_context(context)
         user = getattr(context.get('request'), 'user', None)
-        if user and user.is_authenticated():
+        if user and user.is_authenticated:
             qs = Notification.objects.visible(user)
             extra_context['notifications'] = qs
             extra_context['new_notifications'] = qs.new()
@@ -189,7 +189,7 @@ class NotificationsCell(CellBase):
 
     def get_badge(self, context):
         user = getattr(context.get('request'), 'user', None)
-        if not user or not user.is_authenticated():
+        if not user or not user.is_authenticated:
             return
         new_count = Notification.objects.visible(user).new().count()
         if not new_count:
diff --git a/combo/apps/pwa/views.py b/combo/apps/pwa/views.py
index 61ad0c6..0d0493b 100644
--- a/combo/apps/pwa/views.py
+++ b/combo/apps/pwa/views.py
@@ -83,7 +83,7 @@ def service_worker_registration_js(request, *args, **kwargs):
 
 @csrf_exempt
 def subscribe_push(request, *args, **kwargs):
-    if not (request.user and request.user.is_authenticated()):
+    if not (request.user and request.user.is_authenticated):
         return HttpResponseForbidden()
     if request.method != 'POST':
         return HttpResponseForbidden()
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index c305336..84e7c4f 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -295,7 +295,7 @@ class WcsUserDataBaseCell(WcsDataBaseCell):
         abstract = True
 
     def is_visible(self, user=None):
-        if not user or user.is_anonymous():
+        if not user or user.is_anonymous:
             return False
         return super(WcsUserDataBaseCell, self).is_visible(user)
 
diff --git a/combo/data/models.py b/combo/data/models.py
index e4c0483..991dc8a 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -67,9 +67,9 @@ class PostException(Exception):
 def element_is_visible(element, user=None):
     if element.public:
         if getattr(element, 'restricted_to_unlogged', None) is True:
-            return (user is None or user.is_anonymous())
+            return (user is None or user.is_anonymous)
         return True
-    if user is None or user.is_anonymous():
+    if user is None or user.is_anonymous:
         return False
     if user.is_superuser:
         return True
@@ -448,7 +448,7 @@ class PageSnapshot(models.Model):
     @classmethod
     def take(cls, page, request=None, comment=None, deletion=False):
         snapshot = cls(page=page, comment=comment)
-        if request and not request.user.is_anonymous():
+        if request and not request.user.is_anonymous:
             snapshot.user = request.user
         if not deletion:
             snapshot.serialization = page.get_serialized_page()
@@ -1011,7 +1011,7 @@ class ParametersCell(CellBase):
 
     def get_form(self, request):
         from .forms import ParametersForm
-        if not request.user.is_anonymous():
+        if not request.user.is_anonymous:
             groups = set(request.user.groups.values_list('name', flat=True))
         else:
             groups = set()
diff --git a/combo/profile/models.py b/combo/profile/models.py
index 1bcc84d..0e1ad4c 100644
--- a/combo/profile/models.py
+++ b/combo/profile/models.py
@@ -46,7 +46,7 @@ class ProfileCell(JsonCellBase):
         return '{%% load combo %%}%sapi/users/{{ concerned_user|name_id }}/' % idp.get('url')
 
     def is_visible(self, user=None):
-        if not user or user.is_anonymous():
+        if not user or user.is_anonymous:
             return False
         return super(ProfileCell, self).is_visible(user)
 
diff --git a/combo/profile/utils.py b/combo/profile/utils.py
index 22cb55e..8432d05 100644
--- a/combo/profile/utils.py
+++ b/combo/profile/utils.py
@@ -32,9 +32,11 @@ class ProxiedUser(object):
     def get_name_id(self):
         return self.name_id
 
+    @property
     def is_authenticated(self):
         return CallableTrue
 
+    @property
     def is_anonymous(self):
         return CallableFalse
 
diff --git a/combo/public/templatetags/combo.py b/combo/public/templatetags/combo.py
index 6730229..fa09d66 100644
--- a/combo/public/templatetags/combo.py
+++ b/combo/public/templatetags/combo.py
@@ -209,7 +209,7 @@ def shown_because_admin(cell, request):
 
 @register.filter(name='has_role')
 def has_role(user, groupname):
-    if not user or user.is_anonymous():
+    if not user or user.is_anonymous:
         return False
     return user.groups.filter(name=groupname).exists()
 
@@ -253,7 +253,7 @@ def signed(obj):
 
 @register.filter
 def name_id(user):
-    if user and user.is_authenticated():
+    if user and user.is_authenticated:
         user_name_id = user.get_name_id()
         if user_name_id:
             return user_name_id
diff --git a/combo/public/views.py b/combo/public/views.py
index e690754..bf35b5e 100644
--- a/combo/public/views.py
+++ b/combo/public/views.py
@@ -396,7 +396,7 @@ def page(request):
         parts = ['index']
 
     if parts == ['index'] and settings.COMBO_INITIAL_LOGIN_PAGE_PATH and (
-            request.user and not request.user.is_anonymous()):
+            request.user and not request.user.is_anonymous):
         profile, created = Profile.objects.get_or_create(user=request.user)
         if not profile.initial_login_view_timestamp:
             # first connection of user, record that and redirect to welcome URL
@@ -405,7 +405,7 @@ def page(request):
             return HttpResponseRedirect(settings.COMBO_INITIAL_LOGIN_PAGE_PATH)
 
     if parts == ['index'] and settings.COMBO_WELCOME_PAGE_PATH and (
-            not request.user or request.user.is_anonymous()):
+            not request.user or request.user.is_anonymous):
         if not request.session.setdefault('visited', False):
             # first visit, the user is not logged in.
             request.session['visited'] = True
@@ -465,7 +465,7 @@ def publish_page(request, page, status=200, template_name=None):
     pages = page.get_parents_and_self()
 
     if not page.is_visible(request.user):
-        if not request.user.is_authenticated():
+        if not request.user.is_authenticated:
             from django.contrib.auth.views import redirect_to_login
             return redirect_to_login(request.build_absolute_uri())
         raise PermissionDenied()
diff --git a/combo/urls_utils.py b/combo/urls_utils.py
index 32cd762..fecf770 100644
--- a/combo/urls_utils.py
+++ b/combo/urls_utils.py
@@ -52,7 +52,7 @@ def manager_required(function=None, login_url=None):
     def check_manager(user):
         if user and user.is_staff:
             return True
-        if user and not user.is_anonymous():
+        if user and not user.is_anonymous:
             raise PermissionDenied()
         # As the last resort, show the login form
         return False
diff --git a/combo/utils/requests_wrapper.py b/combo/utils/requests_wrapper.py
index 8befd05..4add535 100644
--- a/combo/utils/requests_wrapper.py
+++ b/combo/utils/requests_wrapper.py
@@ -82,7 +82,7 @@ class Requests(RequestsSession):
         if remote_service:
             if isinstance(user, dict):
                 query_params = user.copy()
-            elif not user or not user.is_authenticated():
+            elif not user or not user.is_authenticated:
                 if without_user:
                     query_params = {}
                 else:
diff --git a/combo/utils/urls.py b/combo/utils/urls.py
index 10703e9..c7f41f3 100644
--- a/combo/utils/urls.py
+++ b/combo/utils/urls.py
@@ -43,7 +43,7 @@ def get_templated_url(url, context=None):
         template_vars['user_email'] = ''
         template_vars['user_nameid'] = ''
         user = getattr(context.get('request'), 'user', None)
-        if user and user.is_authenticated():
+        if user and user.is_authenticated:
             template_vars['user_email'] = quote(user.email)
             user_nameid = user.get_name_id()
             if user_nameid:
diff --git a/tests/test_lingo_remote_regie.py b/tests/test_lingo_remote_regie.py
index 355bb07..d4e2c08 100644
--- a/tests/test_lingo_remote_regie.py
+++ b/tests/test_lingo_remote_regie.py
@@ -76,8 +76,7 @@ def remote_regie():
 
 class MockUser(object):
     email = 'foo@example.net'
-    def is_authenticated(self):
-        return True
+    is_authenticated = True
 
     def get_name_id(self):
         return 'r2d2'
diff --git a/tests/test_requests.py b/tests/test_requests.py
index a116cd0..7052269 100644
--- a/tests/test_requests.py
+++ b/tests/test_requests.py
@@ -10,8 +10,7 @@ from combo.utils import requests, check_query, NothingInCacheException
 
 class MockUser(object):
     email = 'foo@example.net'
-    def is_authenticated(self):
-        return True
+    is_authenticated = True
 
     def get_name_id(self):
         if self.samlized:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 422b9a8..356bc02 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -11,8 +11,7 @@ from django.contrib.auth.models import AnonymousUser
 
 class MockUser(object):
     email = 'foo=3@example.net'
-    def is_authenticated(self):
-        return True
+    is_authenticated = True
 
     def __init__(self, samlized=True):
         self.samlized = samlized
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 4f03bf9..15d30e2 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -166,16 +166,16 @@ WCS_PID = None
 
 class MockUser(object):
     email = 'foo@example.net'
-    def is_authenticated(self):
-        return True
+    is_authenticated = True
+
     def get_name_id(self):
         return None
 
 
 class MockUserWithNameId(object):
     email = 'foo@example.net'
-    def is_authenticated(self):
-        return True
+    is_authenticated = True
+
     def get_name_id(self):
         return 'xyz'
 
-- 
2.20.1

