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.
This commit is contained in:
Lauréline Guérin 2019-10-14 13:43:08 +02:00
parent 49830fc9c5
commit ccf99d908f
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
22 changed files with 45 additions and 46 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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']

View File

@ -332,7 +332,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
@ -386,7 +386,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():
@ -433,7 +433,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'])
@ -615,7 +615,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:
@ -699,11 +699,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())

View File

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

View File

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

View File

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

View File

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

View File

@ -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
@ -450,7 +450,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()
@ -1018,7 +1018,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()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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