Projet

Général

Profil

0001-cells-don-t-check-validity-in-is_visible-if-placehol.patch

Lauréline Guérin, 27 mars 2020 18:02

Télécharger (10,1 ko)

Voir les différences:

Subject: [PATCH 1/7] cells: don't check validity in is_visible if placeholder
 search (#40252)

 combo/apps/calendar/models.py      |  4 ++--
 combo/apps/fargo/models.py         |  5 +++--
 combo/apps/newsletters/models.py   |  5 +++--
 combo/apps/notifications/models.py |  5 +++--
 combo/apps/search/models.py        |  6 +++---
 combo/apps/wcs/models.py           |  5 +++--
 combo/data/models.py               | 17 +++++++++--------
 combo/profile/models.py            |  5 +++--
 combo/public/templatetags/combo.py |  9 +++++----
 combo/public/views.py              |  2 +-
 tests/test_notification.py         |  4 ++--
 11 files changed, 37 insertions(+), 30 deletions(-)
combo/apps/calendar/models.py
54 54
    def is_enabled(cls):
55 55
        return settings.BOOKING_CALENDAR_CELL_ENABLED and is_chrono_enabled() and is_wcs_enabled()
56 56

  
57
    def is_visible(self, user=None):
57
    def is_visible(self, **kwargs):
58 58
        return self.agenda_reference and self.formdef_reference \
59
            and super(BookingCalendar, self).is_visible(user=user)
59
            and super(BookingCalendar, self).is_visible(**kwargs)
60 60

  
61 61
    def get_cell_extra_context(self, context):
62 62
        if context.get('placeholder_search_mode'):
combo/apps/fargo/models.py
59 59
                fields=self.get_form_fields(),
60 60
                widgets=self.get_form_widgets())
61 61

  
62
    def is_visible(self, user=None):
62
    def is_visible(self, **kwargs):
63
        user = kwargs.get('user')
63 64
        if not user or user.is_anonymous:
64 65
            return False
65
        return super(RecentDocumentsCell, self).is_visible(user)
66
        return super(RecentDocumentsCell, self).is_visible(**kwargs)
66 67

  
67 68
    @classmethod
68 69
    def is_enabled(cls):
combo/apps/newsletters/models.py
141 141
            context['form'] = form
142 142
        return super(NewslettersCell, self).render(context)
143 143

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

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

  
179 180
    def get_cell_extra_context(self, context):
180 181
        extra_context = super(NotificationsCell, self).get_cell_extra_context(context)
combo/apps/search/models.py
48 48
    class Meta:
49 49
        verbose_name = _('Search')
50 50

  
51
    def is_visible(self, user=None):
51
    def is_visible(self, **kwargs):
52 52
        if not self.search_services:
53 53
            return False
54
        return super(SearchCell, self).is_visible(user=user)
54
        return super(SearchCell, self).is_visible(**kwargs)
55 55

  
56 56
    def get_default_form_class(self):
57 57
        from .forms import SearchCellForm
......
114 114
    @classmethod
115 115
    def ajax_results_view(cls, request, cell_pk, service_slug):
116 116
        cell = cls.objects.get(pk=cell_pk)
117
        if not cell.is_visible(request.user) or not cell.page.is_visible(request.user):
117
        if not cell.is_visible(user=request.user) or not cell.page.is_visible(request.user):
118 118
            raise PermissionDenied
119 119

  
120 120
        query = request.GET.get('q')
combo/apps/wcs/models.py
381 381
    class Meta:
382 382
        abstract = True
383 383

  
384
    def is_visible(self, user=None):
384
    def is_visible(self, **kwargs):
385
        user = kwargs.get('user')
385 386
        if not user or user.is_anonymous:
386 387
            return False
387
        return super(WcsUserDataBaseCell, self).is_visible(user)
388
        return super(WcsUserDataBaseCell, self).is_visible(**kwargs)
388 389

  
389 390

  
390 391
class CategoriesValidityMixin(object):
combo/data/models.py
856 856
        return self.invalid_reason_codes.get(
857 857
            validity_info.invalid_reason_code, validity_info.invalid_reason_code)
858 858

  
859
    def is_visible(self, user=None):
860
        validity_info = self.get_validity_info()
861
        if validity_info is not None and validity_info.invalid_since and validity_info.invalid_since < now() - datetime.timedelta(days=2):
862
            return False
859
    def is_visible(self, user=None, check_validity_info=True):
860
        if check_validity_info:
861
            validity_info = self.get_validity_info()
862
            if validity_info is not None and validity_info.invalid_since and validity_info.invalid_since < now() - datetime.timedelta(days=2):
863
                return False
863 864
        return element_is_visible(self, user=user)
864 865

  
865 866
    def is_relevant(self, context):
......
1293 1294
    class Meta:
1294 1295
        verbose_name = _('RSS/Atom Feed')
1295 1296

  
1296
    def is_visible(self, user=None):
1297
        return bool(self.url) and super(FeedCell, self).is_visible(user=user)
1297
    def is_visible(self, **kwargs):
1298
        return bool(self.url) and super(FeedCell, self).is_visible(**kwargs)
1298 1299

  
1299 1300
    def get_cell_extra_context(self, context):
1300 1301
        extra_context = super(FeedCell, self).get_cell_extra_context(context)
......
1406 1407
    class Meta:
1407 1408
        abstract = True
1408 1409

  
1409
    def is_visible(self, user=None):
1410
        return bool(self.url) and super(JsonCellBase, self).is_visible(user=user)
1410
    def is_visible(self, **kwargs):
1411
        return bool(self.url) and super(JsonCellBase, self).is_visible(**kwargs)
1411 1412

  
1412 1413
    def is_user_dependant(self, context=None):
1413 1414
        urls = [self.url] + [x['url'] for x in self.additional_data or []]
combo/profile/models.py
45 45
        idp = list(settings.KNOWN_SERVICES.get('authentic').values())[0]
46 46
        return '%sapi/users/{{ concerned_user|name_id }}/' % idp.get('url')
47 47

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

  
53 54
    def get_cell_extra_context(self, context):
54 55
        extra_context = super(ProfileCell, self).get_cell_extra_context(context)
combo/public/templatetags/combo.py
73 73
        page_cells = context['page'].prefetched_cells
74 74
    elif not context.get('render_skeleton'):
75 75
        page_cells = context['page'].get_cells() if 'page' in context else []
76
    context['cells'] = [x for x in page_cells if
77
            x.placeholder == placeholder_name and
78
            (context.get('render_skeleton') or x.is_relevant(context) and
79
            x.is_visible(context['request'].user))]
76
    context['cells'] = [
77
        x for x in page_cells if
78
        x.placeholder == placeholder_name and
79
        (context.get('render_skeleton') or x.is_relevant(context) and
80
         x.is_visible(user=context['request'].user, check_validity_info=False))]
80 81
    if context.get('render_skeleton'):
81 82
        context['skeleton'] = skeleton_text(context, placeholder_name)
82 83
    else:
combo/public/views.py
109 109
    except ObjectDoesNotExist:
110 110
        raise Http404()
111 111

  
112
    if not cell.is_visible(request.user):
112
    if not cell.is_visible(user=request.user):
113 113
        raise PermissionDenied()
114 114

  
115 115
    exception = None
tests/test_notification.py
95 95
    context['synchronous'] = True # to get fresh content
96 96

  
97 97
    context['request'].user = None
98
    assert cell.is_visible(context['request'].user) is False
98
    assert cell.is_visible(user=context['request'].user) is False
99 99
    context['request'].user = john_doe
100
    assert cell.is_visible(context['request'].user) is True
100
    assert cell.is_visible(user=context['request'].user) is True
101 101
    assert cell.get_badge(context) is None
102 102

  
103 103
    notification1 = Notification.notify(john_doe, 'notibar')
104
-