Projet

Général

Profil

0002-dj2-is_anonymous-and-is_authenticated-are-now-proper.patch

Lauréline Guérin, 14 octobre 2019 14:30

Télécharger (20 ko)

Voir les différences:

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(-)
combo/apps/dashboard/models.py
40 40
        return settings.COMBO_DASHBOARD_ENABLED
41 41

  
42 42
    def is_relevant(self, context):
43
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
43
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
44 44
            return False
45 45
        return True
46 46

  
combo/apps/dashboard/views.py
50 50

  
51 51
class DashboardAddTileView(View):
52 52
    def get(self, request, *args, **kwargs):
53
        if not request.user.is_authenticated():
53
        if not request.user.is_authenticated:
54 54
            raise PermissionDenied()
55 55

  
56 56
        dashboard = DashboardCell.objects.all()[0]
combo/apps/family/models.py
41 41
        if context.get('placeholder_search_mode'):
42 42
            return {}
43 43
        user = self.get_concerned_user(context)
44
        if not user or user.is_anonymous():
44
        if not user or user.is_anonymous:
45 45
            return {}
46 46
        response = get_family(user=user,
47 47
                                   raise_if_not_cached=not(context.get('synchronous')))
combo/apps/fargo/models.py
53 53
                widgets=self.get_form_widgets())
54 54

  
55 55
    def is_visible(self, user=None):
56
        if not user or user.is_anonymous():
56
        if not user or user.is_anonymous:
57 57
            return False
58 58
        return super(RecentDocumentsCell, self).is_visible(user)
59 59

  
combo/apps/lingo/models.py
526 526
        return Regie.objects.count() > 0
527 527

  
528 528
    def is_relevant(self, context):
529
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
529
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
530 530
            return False
531 531
        return BasketItem.get_items_to_be_paid(context['request'].user).count() > 0
532 532

  
533 533
    def get_badge(self, context):
534
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
534
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
535 535
            return
536 536
        items = BasketItem.get_items_to_be_paid(context['request'].user)
537 537
        if not items:
......
569 569
        return Regie.objects.count() > 0
570 570

  
571 571
    def is_relevant(self, context):
572
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
572
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
573 573
            return False
574 574
        transactions = Transaction.objects.filter(
575 575
                user=context['request'].user,
......
598 598
        return Regie.objects.count() > 0
599 599

  
600 600
    def is_relevant(self, context):
601
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
601
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
602 602
            return False
603 603
        return BasketItem.get_items_to_be_paid(context['request'].user).count() > 0
604 604

  
605 605
    def render(self, context):
606
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated()):
606
        if not (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated):
607 607
            return ''
608 608
        try:
609 609
            context['basket_url'] = LingoBasketCell.objects.all()[0].page.get_online_url()
......
636 636
        return Regie.objects.exclude(webservice_url='').count() > 0
637 637

  
638 638
    def is_relevant(self, context):
639
        return (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated())
639
        return (getattr(context['request'], 'user', None) and context['request'].user.is_authenticated)
640 640

  
641 641
    def get_default_form_class(self):
642 642
        fields = ['title', 'text']
combo/apps/lingo/views.py
329 329
            if item.regie != regie:
330 330
                messages.error(request, _(u'Invalid grouping for basket items.'))
331 331
                return HttpResponseRedirect(next_url)
332
        user = request.user if request.user.is_authenticated() else None
332
        user = request.user if request.user.is_authenticated else None
333 333
        transaction = Transaction()
334 334
        if user:
335 335
            transaction.user = user
......
383 383
    def post(self, request, *args, **kwargs):
384 384
        regie_id = request.POST.get('regie')
385 385
        next_url = request.POST.get('next_url') or '/'
386
        user = request.user if request.user.is_authenticated() else None
386
        user = request.user if request.user.is_authenticated else None
387 387
        remote_items = []
388 388
        items = []
389 389
        if regie_id and Regie.objects.get(pk=regie_id).is_remote():
......
430 430
class BasketItemPayView(PayMixin, View):
431 431
    def get(self, request, *args, **kwargs):
432 432
        next_url = request.GET.get('next_url') or '/'
433
        if not (request.user and request.user.is_authenticated()):
433
        if not (request.user and request.user.is_authenticated):
434 434
            return HttpResponseForbidden(_('No item payment allowed for anonymous users.'))
435 435

  
436 436
        item = BasketItem.objects.get(pk=kwargs['item_id'])
......
612 612
                return HttpResponseRedirect(redirect_url)
613 613

  
614 614
        # return to basket page if there are still items to pay
615
        if request.user.is_authenticated():
615
        if request.user.is_authenticated:
616 616
            remaining_basket_items = BasketItem.get_items_to_be_paid(
617 617
                user=self.request.user).count()
618 618
            if remaining_basket_items:
......
696 696
        return context
697 697

  
698 698
    def get_queryset(self):
699
        user = self.request.user if self.request.user.is_authenticated() else None
699
        user = self.request.user if self.request.user.is_authenticated else None
700 700
        return BasketItem.get_items_to_be_paid(user=user)
701 701

  
702 702
    def post(self, request, *args, **kwargs):
703
        if not request.user.is_authenticated():
703
        if not request.user.is_authenticated:
704 704
            messages.error(request, _('An error occured when removing the item. '
705 705
                                      '(no authenticated user)'))
706 706
            return HttpResponseRedirect(get_basket_url())
combo/apps/newsletters/models.py
136 136

  
137 137
    def render(self, context):
138 138
        user = context.get('user')
139
        if user and user.is_authenticated():
139
        if user and user.is_authenticated:
140 140
            form = NewslettersManageForm(instance=self, request=context['request'])
141 141
            context['form'] = form
142 142
        return super(NewslettersCell, self).render(context)
143 143

  
144 144
    def is_visible(self, user=None):
145
        if user is None or not user.is_authenticated():
145
        if user is None or not user.is_authenticated:
146 146
            return False
147 147
        return super(NewslettersCell, self).is_visible(user)
combo/apps/notifications/models.py
172 172
        verbose_name = _('User Notifications')
173 173

  
174 174
    def is_visible(self, user=None):
175
        if user is None or not user.is_authenticated():
175
        if user is None or not user.is_authenticated:
176 176
            return False
177 177
        return super(NotificationsCell, self).is_visible(user)
178 178

  
179 179
    def get_cell_extra_context(self, context):
180 180
        extra_context = super(NotificationsCell, self).get_cell_extra_context(context)
181 181
        user = getattr(context.get('request'), 'user', None)
182
        if user and user.is_authenticated():
182
        if user and user.is_authenticated:
183 183
            qs = Notification.objects.visible(user)
184 184
            extra_context['notifications'] = qs
185 185
            extra_context['new_notifications'] = qs.new()
......
189 189

  
190 190
    def get_badge(self, context):
191 191
        user = getattr(context.get('request'), 'user', None)
192
        if not user or not user.is_authenticated():
192
        if not user or not user.is_authenticated:
193 193
            return
194 194
        new_count = Notification.objects.visible(user).new().count()
195 195
        if not new_count:
combo/apps/pwa/views.py
83 83

  
84 84
@csrf_exempt
85 85
def subscribe_push(request, *args, **kwargs):
86
    if not (request.user and request.user.is_authenticated()):
86
    if not (request.user and request.user.is_authenticated):
87 87
        return HttpResponseForbidden()
88 88
    if request.method != 'POST':
89 89
        return HttpResponseForbidden()
combo/apps/wcs/models.py
295 295
        abstract = True
296 296

  
297 297
    def is_visible(self, user=None):
298
        if not user or user.is_anonymous():
298
        if not user or user.is_anonymous:
299 299
            return False
300 300
        return super(WcsUserDataBaseCell, self).is_visible(user)
301 301

  
combo/data/models.py
67 67
def element_is_visible(element, user=None):
68 68
    if element.public:
69 69
        if getattr(element, 'restricted_to_unlogged', None) is True:
70
            return (user is None or user.is_anonymous())
70
            return (user is None or user.is_anonymous)
71 71
        return True
72
    if user is None or user.is_anonymous():
72
    if user is None or user.is_anonymous:
73 73
        return False
74 74
    if user.is_superuser:
75 75
        return True
......
448 448
    @classmethod
449 449
    def take(cls, page, request=None, comment=None, deletion=False):
450 450
        snapshot = cls(page=page, comment=comment)
451
        if request and not request.user.is_anonymous():
451
        if request and not request.user.is_anonymous:
452 452
            snapshot.user = request.user
453 453
        if not deletion:
454 454
            snapshot.serialization = page.get_serialized_page()
......
1011 1011

  
1012 1012
    def get_form(self, request):
1013 1013
        from .forms import ParametersForm
1014
        if not request.user.is_anonymous():
1014
        if not request.user.is_anonymous:
1015 1015
            groups = set(request.user.groups.values_list('name', flat=True))
1016 1016
        else:
1017 1017
            groups = set()
combo/profile/models.py
46 46
        return '{%% load combo %%}%sapi/users/{{ concerned_user|name_id }}/' % idp.get('url')
47 47

  
48 48
    def is_visible(self, user=None):
49
        if not user or user.is_anonymous():
49
        if not user or user.is_anonymous:
50 50
            return False
51 51
        return super(ProfileCell, self).is_visible(user)
52 52

  
combo/profile/utils.py
32 32
    def get_name_id(self):
33 33
        return self.name_id
34 34

  
35
    @property
35 36
    def is_authenticated(self):
36 37
        return CallableTrue
37 38

  
39
    @property
38 40
    def is_anonymous(self):
39 41
        return CallableFalse
40 42

  
combo/public/templatetags/combo.py
209 209

  
210 210
@register.filter(name='has_role')
211 211
def has_role(user, groupname):
212
    if not user or user.is_anonymous():
212
    if not user or user.is_anonymous:
213 213
        return False
214 214
    return user.groups.filter(name=groupname).exists()
215 215

  
......
253 253

  
254 254
@register.filter
255 255
def name_id(user):
256
    if user and user.is_authenticated():
256
    if user and user.is_authenticated:
257 257
        user_name_id = user.get_name_id()
258 258
        if user_name_id:
259 259
            return user_name_id
combo/public/views.py
396 396
        parts = ['index']
397 397

  
398 398
    if parts == ['index'] and settings.COMBO_INITIAL_LOGIN_PAGE_PATH and (
399
            request.user and not request.user.is_anonymous()):
399
            request.user and not request.user.is_anonymous):
400 400
        profile, created = Profile.objects.get_or_create(user=request.user)
401 401
        if not profile.initial_login_view_timestamp:
402 402
            # first connection of user, record that and redirect to welcome URL
......
405 405
            return HttpResponseRedirect(settings.COMBO_INITIAL_LOGIN_PAGE_PATH)
406 406

  
407 407
    if parts == ['index'] and settings.COMBO_WELCOME_PAGE_PATH and (
408
            not request.user or request.user.is_anonymous()):
408
            not request.user or request.user.is_anonymous):
409 409
        if not request.session.setdefault('visited', False):
410 410
            # first visit, the user is not logged in.
411 411
            request.session['visited'] = True
......
465 465
    pages = page.get_parents_and_self()
466 466

  
467 467
    if not page.is_visible(request.user):
468
        if not request.user.is_authenticated():
468
        if not request.user.is_authenticated:
469 469
            from django.contrib.auth.views import redirect_to_login
470 470
            return redirect_to_login(request.build_absolute_uri())
471 471
        raise PermissionDenied()
combo/urls_utils.py
52 52
    def check_manager(user):
53 53
        if user and user.is_staff:
54 54
            return True
55
        if user and not user.is_anonymous():
55
        if user and not user.is_anonymous:
56 56
            raise PermissionDenied()
57 57
        # As the last resort, show the login form
58 58
        return False
combo/utils/requests_wrapper.py
82 82
        if remote_service:
83 83
            if isinstance(user, dict):
84 84
                query_params = user.copy()
85
            elif not user or not user.is_authenticated():
85
            elif not user or not user.is_authenticated:
86 86
                if without_user:
87 87
                    query_params = {}
88 88
                else:
combo/utils/urls.py
43 43
        template_vars['user_email'] = ''
44 44
        template_vars['user_nameid'] = ''
45 45
        user = getattr(context.get('request'), 'user', None)
46
        if user and user.is_authenticated():
46
        if user and user.is_authenticated:
47 47
            template_vars['user_email'] = quote(user.email)
48 48
            user_nameid = user.get_name_id()
49 49
            if user_nameid:
tests/test_lingo_remote_regie.py
76 76

  
77 77
class MockUser(object):
78 78
    email = 'foo@example.net'
79
    def is_authenticated(self):
80
        return True
79
    is_authenticated = True
81 80

  
82 81
    def get_name_id(self):
83 82
        return 'r2d2'
tests/test_requests.py
10 10

  
11 11
class MockUser(object):
12 12
    email = 'foo@example.net'
13
    def is_authenticated(self):
14
        return True
13
    is_authenticated = True
15 14

  
16 15
    def get_name_id(self):
17 16
        if self.samlized:
tests/test_utils.py
11 11

  
12 12
class MockUser(object):
13 13
    email = 'foo=3@example.net'
14
    def is_authenticated(self):
15
        return True
14
    is_authenticated = True
16 15

  
17 16
    def __init__(self, samlized=True):
18 17
        self.samlized = samlized
tests/test_wcs.py
166 166

  
167 167
class MockUser(object):
168 168
    email = 'foo@example.net'
169
    def is_authenticated(self):
170
        return True
169
    is_authenticated = True
170

  
171 171
    def get_name_id(self):
172 172
        return None
173 173

  
174 174

  
175 175
class MockUserWithNameId(object):
176 176
    email = 'foo@example.net'
177
    def is_authenticated(self):
178
        return True
177
    is_authenticated = True
178

  
179 179
    def get_name_id(self):
180 180
        return 'xyz'
181 181

  
182
-