Project

General

Profile

0002-change-is_visible-signature-everywhere.patch

Valentin Deniaud, 17 Jul 2019 05:18 PM

Download (19.1 KB)

View differences:

Subject: [PATCH 2/2] change is_visible signature everywhere

 combo/apps/calendar/models.py                 |  4 ++--
 combo/apps/dashboard/views.py                 |  4 ++--
 combo/apps/fargo/models.py                    |  6 ++---
 combo/apps/maps/views.py                      |  2 +-
 combo/apps/newsletters/models.py              |  6 ++---
 combo/apps/notifications/models.py            |  6 ++---
 .../management/commands/update_index.py       |  4 ++--
 combo/apps/search/models.py                   |  6 ++---
 combo/apps/usersearch/models.py               |  4 ++--
 combo/apps/usersearch/views.py                |  2 +-
 combo/apps/wcs/models.py                      |  6 ++---
 combo/data/models.py                          | 24 +++++++++----------
 combo/data/search_indexes.py                  |  2 +-
 combo/profile/models.py                       |  6 ++---
 combo/public/menu.py                          |  2 +-
 combo/public/templatetags/combo.py            |  2 +-
 combo/public/views.py                         | 12 +++++-----
 tests/test_notification.py                    |  4 ++--
 tests/test_pages.py                           | 19 ++++++++++-----
 19 files changed, 64 insertions(+), 57 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, request=None):
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(request=request)
60 60

  
61 61
    def get_cell_extra_context(self, context):
62 62
        if context.get('placeholder_search_mode'):
combo/apps/dashboard/views.py
54 54

  
55 55
        dashboard = DashboardCell.objects.all()[0]
56 56
        cell = CellBase.get_cell(kwargs['cell_reference'])
57
        if not cell.page.is_visible(request.user):
57
        if not cell.page.is_visible(request):
58 58
            raise PermissionDenied()
59
        if not cell.is_visible(request.user):
59
        if not cell.is_visible(request):
60 60
            raise PermissionDenied()
61 61
        cell.pk = None
62 62
        cell.page = dashboard.page
combo/apps/fargo/models.py
52 52
                fields=self.get_form_fields(),
53 53
                widgets=self.get_form_widgets())
54 54

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

  
60 60
    @classmethod
61 61
    def is_enabled(cls):
combo/apps/maps/views.py
29 29
            cell = Map.objects.get(pk=kwargs['cell_id'])
30 30
        except Map.DoesNotExist:
31 31
            raise Http404()
32
        if cell.page.is_visible(request.user) and cell.is_visible(request.user):
32
        if cell.page.is_visible(request) and cell.is_visible(request):
33 33
            geojson = cell.get_geojson(request)
34 34
            content_type = 'application/json'
35 35
            return HttpResponse(json.dumps(geojson), content_type=content_type)
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):
145
        if user is None or not user.is_authenticated():
144
    def is_visible(self, request=None):
145
        if request.user is None or not request.user.is_authenticated():
146 146
            return False
147
        return super(NewslettersCell, self).is_visible(user)
147
        return super(NewslettersCell, self).is_visible(request)
combo/apps/notifications/models.py
171 171
    class Meta:
172 172
        verbose_name = _('User Notifications')
173 173

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

  
179 179
    def get_cell_extra_context(self, context):
180 180
        extra_context = super(NotificationsCell, self).get_cell_extra_context(context)
combo/apps/search/management/commands/update_index.py
47 47
        # assemble external links data
48 48
        links = {}
49 49
        for page in Page.objects.filter(sub_slug=''):
50
            if not page.is_visible(user=None):
50
            if not page.is_visible(request=None):
51 51
                continue
52 52
            for cell in page.get_cells():
53
                if not cell.is_visible(user=None):
53
                if not cell.is_visible(request=None):
54 54
                    continue
55 55
                for link_data in cell.get_external_links_data():
56 56
                    if not link_data['url'] in links:
combo/apps/search/models.py
46 46
    class Meta:
47 47
        verbose_name = _('Search')
48 48

  
49
    def is_visible(self, user=None):
49
    def is_visible(self, request=None):
50 50
        if not self.search_services:
51 51
            return False
52
        return super(SearchCell, self).is_visible(user=user)
52
        return super(SearchCell, self).is_visible(request=request)
53 53

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

  
118 118
        query = request.GET.get('q')
combo/apps/usersearch/models.py
36 36
    def is_enabled(cls):
37 37
        return settings.USERSEARCH_CELL_ENABLED
38 38

  
39
    def is_visible(self, user=None):
40
        return super(UserSearchCell, self).is_visible(user=user)
39
    def is_visible(self, request=None):
40
        return super(UserSearchCell, self).is_visible(request=request)
41 41

  
42 42
    def modify_global_context(self, context, request):
43 43
        if 'selected_user_id' in request.GET:
combo/apps/usersearch/views.py
28 28
def ajax_usersearch(request):
29 29
    # allow access if the user can see at least one UserSearch cell
30 30
    for cell in UserSearchCell.objects.all():
31
        if cell.is_visible(request.user) and cell.page.is_visible(request.user):
31
        if cell.is_visible(request) and cell.page.is_visible(request):
32 32
            break
33 33
    else:
34 34
        raise PermissionDenied
combo/apps/wcs/models.py
294 294
    class Meta:
295 295
        abstract = True
296 296

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

  
302 302

  
303 303
@register_cell_class
combo/data/models.py
286 286
        tmpl.render(context, request)
287 287
        return placeholders
288 288

  
289
    def get_next_page(self, user=None):
289
    def get_next_page(self, request=None):
290 290
        pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all())
291 291
        this_page = [x for x in pages if x.id == self.id][0]
292 292
        pages = pages[pages.index(this_page)+1:]
293 293
        for page in pages:
294
            if page.is_visible(user):
294
            if page.is_visible(request):
295 295
                return page
296 296
        return None
297 297

  
298
    def get_previous_page(self, user=None):
298
    def get_previous_page(self, request=None):
299 299
        pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all())
300 300
        pages.reverse()
301 301
        this_page = [x for x in pages if x.id == self.id][0]
302 302
        pages = pages[pages.index(this_page)+1:]
303 303
        for page in pages:
304
            if page.is_visible(user):
304
            if page.is_visible(request):
305 305
                return page
306 306
        return None
307 307

  
......
722 722
            return ''
723 723
        if self.user_dependant:
724 724
            return ''
725
        if not self.page.is_visible(user=None):
725
        if not self.page.is_visible(request=None):
726 726
            return ''
727
        if not self.is_visible(user=None):
727
        if not self.is_visible(request=None):
728 728
            return ''
729 729
        request = RequestFactory().get(self.page.get_online_url())
730 730
        request.user = None  # compat
......
935 935
    class Meta:
936 936
        verbose_name = _('RSS/Atom Feed')
937 937

  
938
    def is_visible(self, user=None):
939
        return bool(self.url) and super(FeedCell, self).is_visible(user=user)
938
    def is_visible(self, request=None):
939
        return bool(self.url) and super(FeedCell, self).is_visible(request=request)
940 940

  
941 941
    def get_cell_extra_context(self, context):
942 942
        extra_context = super(FeedCell, self).get_cell_extra_context(context)
......
988 988
    def get_additional_label(self):
989 989
        return self.title
990 990

  
991
    def is_visible(self, user=None):
992
        return bool(self.parameters) and super(ParametersCell, self).is_visible(user=user)
991
    def is_visible(self, request=None):
992
        return bool(self.parameters) and super(ParametersCell, self).is_visible(request=request)
993 993

  
994 994
    def validate_schema(self, value):
995 995
        if not isinstance(value, list):
......
1120 1120
    class Meta:
1121 1121
        abstract = True
1122 1122

  
1123
    def is_visible(self, user=None):
1124
        return bool(self.url) and super(JsonCellBase, self).is_visible(user=user)
1123
    def is_visible(self, request=None):
1124
        return bool(self.url) and super(JsonCellBase, self).is_visible(request=request)
1125 1125

  
1126 1126
    def get_cell_parameters_context(self):
1127 1127
        return {}
combo/data/search_indexes.py
32 32
        return obj.get_online_url()
33 33

  
34 34
    def prepare(self, obj):
35
        if not obj.is_visible(user=None):
35
        if not obj.is_visible(request=None):
36 36
            raise SkipDocument()
37 37
        return super(PageIndex, self).prepare(obj)
38 38

  
combo/profile/models.py
45 45
        idp = settings.KNOWN_SERVICES.get('authentic').values()[0]
46 46
        return '{%% load combo %%}%sapi/users/{{ concerned_user|name_id }}/' % idp.get('url')
47 47

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

  
53 53
    def get_cell_extra_context(self, context):
54 54
        extra_context = super(ProfileCell, self).get_cell_extra_context(context)
combo/public/menu.py
76 76
                if not element.public:
77 77
                    # mark the menu cell for asynchronous rendering.
78 78
                    raise NothingInCacheException()
79
            if not element.is_visible(context['request'].user):
79
            if not element.is_visible(context['request']):
80 80
                continue
81 81
        menuitem = {'page': element}
82 82
        menuitem['selected'] = bool(element == page_of_level)
combo/public/templatetags/combo.py
66 66
    context['cells'] = [x for x in page_cells if
67 67
            x.placeholder == placeholder_name and
68 68
            (context.get('render_skeleton') or x.is_relevant(context) and
69
            x.is_visible(context['request'].user))]
69
            x.is_visible(context['request']))]
70 70
    if context.get('render_skeleton') and not context['cells']:
71 71
        context['skeleton'] = skeleton_text(context, placeholder_name)
72 72
    else:
combo/public/views.py
100 100
        if not (request.user and request.user.is_staff):
101 101
            raise PermissionDenied()
102 102

  
103
    if not page.is_visible(request.user):
103
    if not page.is_visible(request):
104 104
        raise PermissionDenied()
105 105
    try:
106 106
        cell = CellBase.get_cell(cell_reference, page_id=page_pk)
107 107
    except ObjectDoesNotExist:
108 108
        raise Http404()
109 109

  
110
    if not cell.is_visible(request.user):
110
    if not cell.is_visible(request):
111 111
        raise PermissionDenied()
112 112

  
113 113
    exception = None
......
155 155
        other_cells = []
156 156
        for klass in CellBase.get_cell_classes(lambda x: bool(x.modify_global_context)):
157 157
            other_cells.extend(klass.objects.filter(page_id=cell.page_id))
158
        other_cells = [x for x in other_cells if x.is_visible(user=request.user)]
158
        other_cells = [x for x in other_cells if x.is_visible(request=request)]
159 159
        other_cells.sort(key=lambda x: x.order)
160 160
        for other_cell in other_cells:
161 161
            if other_cell.get_reference() != cell.get_reference():
......
441 441
def publish_page(request, page, status=200, template_name=None):
442 442
    pages = page.get_parents_and_self()
443 443

  
444
    if not page.is_visible(request.user):
444
    if not page.is_visible(request):
445 445
        if not request.user.is_authenticated():
446 446
            from django.contrib.auth.views import redirect_to_login
447 447
            return redirect_to_login(request.build_absolute_uri())
......
459 459

  
460 460
    cells = CellBase.get_cells(page=page)
461 461
    extend_with_parent_cells(cells, hierarchy=pages)
462
    cells = [x for x in cells if x.is_visible(user=request.user)]
462
    cells = [x for x in cells if x.is_visible(request=request)]
463 463

  
464 464
    # mark duplicated slugs to avoid using them in HTML id attributes.
465 465
    cell_by_slugs = {}
......
529 529

  
530 530
def api_search(request):
531 531
    for cell in SearchCell.get_cells_by_search_service('_text'):
532
        if not cell.is_visible(request.user):
532
        if not cell.is_visible(request):
533 533
            continue
534 534
        break
535 535
    else:
tests/test_notification.py
91 91
    context['synchronous'] = True # to get fresh content
92 92

  
93 93
    context['request'].user = None
94
    assert cell.is_visible(context['request'].user) is False
94
    assert cell.is_visible(context['request']) is False
95 95
    context['request'].user = john_doe
96
    assert cell.is_visible(context['request'].user) is True
96
    assert cell.is_visible(context['request']) is True
97 97
    assert cell.get_badge(context) is None
98 98

  
99 99
    notification1 = Notification.notify(john_doe, 'notibar')
tests/test_pages.py
114 114
    user1.groups = [group]
115 115
    user2 = User(username='bar')
116 116
    user2.save()
117
    request = RequestFactory()
117 118

  
118
    assert page.is_visible(user1)
119
    assert page.is_visible(user2)
119
    request.user = user1
120
    assert page.is_visible(request)
121
    request.user = user2
122
    assert page.is_visible(request)
120 123

  
121 124
    page.groups = [group]
122 125
    assert not page.is_visible()
123
    assert page.is_visible(user1)
124
    assert not page.is_visible(user2)
126
    request.user = user1
127
    assert page.is_visible(request)
128
    request.user = user2
129
    assert not page.is_visible(request)
125 130

  
126 131
def test_import_export_pages():
127 132
    page = Page(title=u'foo', slug='foo', order=0, description="Foo's page")
......
203 208

  
204 209
    user1 = User(username='foo')
205 210
    user1.save()
206
    assert page.get_next_page(user1).id == page2.id
207
    assert page3.get_previous_page(user1).id == page2.id
211
    request = RequestFactory()
212
    request.user = user1
213
    assert page.get_next_page(request).id == page2.id
214
    assert page3.get_previous_page(request).id == page2.id
208 215

  
209 216
    page3.public = False
210 217
    page3.save()
211
-