From e64e705ee7f6632dbc5f03d805446beff990e7cf Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 17 Jul 2019 16:50:39 +0200 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(-) diff --git a/combo/apps/calendar/models.py b/combo/apps/calendar/models.py index a2a12bc..ab76186 100644 --- a/combo/apps/calendar/models.py +++ b/combo/apps/calendar/models.py @@ -54,9 +54,9 @@ class BookingCalendar(CellBase): def is_enabled(cls): return settings.BOOKING_CALENDAR_CELL_ENABLED and is_chrono_enabled() and is_wcs_enabled() - def is_visible(self, user=None): + def is_visible(self, request=None): return self.agenda_reference and self.formdef_reference \ - and super(BookingCalendar, self).is_visible(user=user) + and super(BookingCalendar, self).is_visible(request=request) def get_cell_extra_context(self, context): if context.get('placeholder_search_mode'): diff --git a/combo/apps/dashboard/views.py b/combo/apps/dashboard/views.py index d0cc107..c5c5f3d 100644 --- a/combo/apps/dashboard/views.py +++ b/combo/apps/dashboard/views.py @@ -54,9 +54,9 @@ class DashboardAddTileView(View): dashboard = DashboardCell.objects.all()[0] cell = CellBase.get_cell(kwargs['cell_reference']) - if not cell.page.is_visible(request.user): + if not cell.page.is_visible(request): raise PermissionDenied() - if not cell.is_visible(request.user): + if not cell.is_visible(request): raise PermissionDenied() cell.pk = None cell.page = dashboard.page diff --git a/combo/apps/fargo/models.py b/combo/apps/fargo/models.py index 9801341..4e42d7a 100644 --- a/combo/apps/fargo/models.py +++ b/combo/apps/fargo/models.py @@ -52,10 +52,10 @@ class RecentDocumentsCell(CellBase): fields=self.get_form_fields(), widgets=self.get_form_widgets()) - def is_visible(self, user=None): - if not user or user.is_anonymous(): + def is_visible(self, request=None): + if not request or request.user.is_anonymous(): return False - return super(RecentDocumentsCell, self).is_visible(user) + return super(RecentDocumentsCell, self).is_visible(request) @classmethod def is_enabled(cls): diff --git a/combo/apps/maps/views.py b/combo/apps/maps/views.py index 6877e22..831996f 100644 --- a/combo/apps/maps/views.py +++ b/combo/apps/maps/views.py @@ -29,7 +29,7 @@ class GeojsonView(View): cell = Map.objects.get(pk=kwargs['cell_id']) except Map.DoesNotExist: raise Http404() - if cell.page.is_visible(request.user) and cell.is_visible(request.user): + if cell.page.is_visible(request) and cell.is_visible(request): geojson = cell.get_geojson(request) content_type = 'application/json' return HttpResponse(json.dumps(geojson), content_type=content_type) diff --git a/combo/apps/newsletters/models.py b/combo/apps/newsletters/models.py index 6a9feee..86906e0 100644 --- a/combo/apps/newsletters/models.py +++ b/combo/apps/newsletters/models.py @@ -141,7 +141,7 @@ class NewslettersCell(CellBase): context['form'] = form return super(NewslettersCell, self).render(context) - def is_visible(self, user=None): - if user is None or not user.is_authenticated(): + def is_visible(self, request=None): + if request.user is None or not request.user.is_authenticated(): return False - return super(NewslettersCell, self).is_visible(user) + return super(NewslettersCell, self).is_visible(request) diff --git a/combo/apps/notifications/models.py b/combo/apps/notifications/models.py index e3bf7a2..a483083 100644 --- a/combo/apps/notifications/models.py +++ b/combo/apps/notifications/models.py @@ -171,10 +171,10 @@ class NotificationsCell(CellBase): class Meta: verbose_name = _('User Notifications') - def is_visible(self, user=None): - if user is None or not user.is_authenticated(): + def is_visible(self, request=None): + if request is None or not request.user.is_authenticated(): return False - return super(NotificationsCell, self).is_visible(user) + return super(NotificationsCell, self).is_visible(request) def get_cell_extra_context(self, context): extra_context = super(NotificationsCell, self).get_cell_extra_context(context) diff --git a/combo/apps/search/management/commands/update_index.py b/combo/apps/search/management/commands/update_index.py index 37eb6ff..e372c6d 100644 --- a/combo/apps/search/management/commands/update_index.py +++ b/combo/apps/search/management/commands/update_index.py @@ -47,10 +47,10 @@ class Command(UpdateIndexCommand): # assemble external links data links = {} for page in Page.objects.filter(sub_slug=''): - if not page.is_visible(user=None): + if not page.is_visible(request=None): continue for cell in page.get_cells(): - if not cell.is_visible(user=None): + if not cell.is_visible(request=None): continue for link_data in cell.get_external_links_data(): if not link_data['url'] in links: diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py index f35e086..fd5a496 100644 --- a/combo/apps/search/models.py +++ b/combo/apps/search/models.py @@ -46,10 +46,10 @@ class SearchCell(CellBase): class Meta: verbose_name = _('Search') - def is_visible(self, user=None): + def is_visible(self, request=None): if not self.search_services: return False - return super(SearchCell, self).is_visible(user=user) + return super(SearchCell, self).is_visible(request=request) def get_default_form_class(self): from .forms import SearchCellForm @@ -112,7 +112,7 @@ class SearchCell(CellBase): @classmethod def ajax_results_view(cls, request, cell_pk, service_slug): cell = cls.objects.get(pk=cell_pk) - if not cell.is_visible(request.user) or not cell.page.is_visible(request.user): + if not cell.is_visible(request) or not cell.page.is_visible(request): raise PermissionDenied query = request.GET.get('q') diff --git a/combo/apps/usersearch/models.py b/combo/apps/usersearch/models.py index f1f38c1..e709316 100644 --- a/combo/apps/usersearch/models.py +++ b/combo/apps/usersearch/models.py @@ -36,8 +36,8 @@ class UserSearchCell(CellBase): def is_enabled(cls): return settings.USERSEARCH_CELL_ENABLED - def is_visible(self, user=None): - return super(UserSearchCell, self).is_visible(user=user) + def is_visible(self, request=None): + return super(UserSearchCell, self).is_visible(request=request) def modify_global_context(self, context, request): if 'selected_user_id' in request.GET: diff --git a/combo/apps/usersearch/views.py b/combo/apps/usersearch/views.py index 81d7286..705b47c 100644 --- a/combo/apps/usersearch/views.py +++ b/combo/apps/usersearch/views.py @@ -28,7 +28,7 @@ from .models import UserSearchCell def ajax_usersearch(request): # allow access if the user can see at least one UserSearch cell for cell in UserSearchCell.objects.all(): - if cell.is_visible(request.user) and cell.page.is_visible(request.user): + if cell.is_visible(request) and cell.page.is_visible(request): break else: raise PermissionDenied diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py index 7d4e4ff..26b8765 100644 --- a/combo/apps/wcs/models.py +++ b/combo/apps/wcs/models.py @@ -294,10 +294,10 @@ class WcsUserDataBaseCell(WcsDataBaseCell): class Meta: abstract = True - def is_visible(self, user=None): - if not user or user.is_anonymous(): + def is_visible(self, request=None): + if not request or request.user.is_anonymous(): return False - return super(WcsUserDataBaseCell, self).is_visible(user) + return super(WcsUserDataBaseCell, self).is_visible(request) @register_cell_class diff --git a/combo/data/models.py b/combo/data/models.py index fd9d416..94ed2b2 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -286,22 +286,22 @@ class Page(models.Model): tmpl.render(context, request) return placeholders - def get_next_page(self, user=None): + def get_next_page(self, request=None): pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all()) this_page = [x for x in pages if x.id == self.id][0] pages = pages[pages.index(this_page)+1:] for page in pages: - if page.is_visible(user): + if page.is_visible(request): return page return None - def get_previous_page(self, user=None): + def get_previous_page(self, request=None): pages = Page.get_as_reordered_flat_hierarchy(Page.objects.all()) pages.reverse() this_page = [x for x in pages if x.id == self.id][0] pages = pages[pages.index(this_page)+1:] for page in pages: - if page.is_visible(user): + if page.is_visible(request): return page return None @@ -722,9 +722,9 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)): return '' if self.user_dependant: return '' - if not self.page.is_visible(user=None): + if not self.page.is_visible(request=None): return '' - if not self.is_visible(user=None): + if not self.is_visible(request=None): return '' request = RequestFactory().get(self.page.get_online_url()) request.user = None # compat @@ -935,8 +935,8 @@ class FeedCell(CellBase): class Meta: verbose_name = _('RSS/Atom Feed') - def is_visible(self, user=None): - return bool(self.url) and super(FeedCell, self).is_visible(user=user) + def is_visible(self, request=None): + return bool(self.url) and super(FeedCell, self).is_visible(request=request) def get_cell_extra_context(self, context): extra_context = super(FeedCell, self).get_cell_extra_context(context) @@ -988,8 +988,8 @@ class ParametersCell(CellBase): def get_additional_label(self): return self.title - def is_visible(self, user=None): - return bool(self.parameters) and super(ParametersCell, self).is_visible(user=user) + def is_visible(self, request=None): + return bool(self.parameters) and super(ParametersCell, self).is_visible(request=request) def validate_schema(self, value): if not isinstance(value, list): @@ -1120,8 +1120,8 @@ class JsonCellBase(CellBase): class Meta: abstract = True - def is_visible(self, user=None): - return bool(self.url) and super(JsonCellBase, self).is_visible(user=user) + def is_visible(self, request=None): + return bool(self.url) and super(JsonCellBase, self).is_visible(request=request) def get_cell_parameters_context(self): return {} diff --git a/combo/data/search_indexes.py b/combo/data/search_indexes.py index 6b58a08..6e683d0 100644 --- a/combo/data/search_indexes.py +++ b/combo/data/search_indexes.py @@ -32,7 +32,7 @@ class PageIndex(indexes.SearchIndex, indexes.Indexable): return obj.get_online_url() def prepare(self, obj): - if not obj.is_visible(user=None): + if not obj.is_visible(request=None): raise SkipDocument() return super(PageIndex, self).prepare(obj) diff --git a/combo/profile/models.py b/combo/profile/models.py index 842adfa..cc8b514 100644 --- a/combo/profile/models.py +++ b/combo/profile/models.py @@ -45,10 +45,10 @@ class ProfileCell(JsonCellBase): idp = settings.KNOWN_SERVICES.get('authentic').values()[0] 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(): + def is_visible(self, request=None): + if not request or request.user.is_anonymous(): return False - return super(ProfileCell, self).is_visible(user) + return super(ProfileCell, self).is_visible(request) def get_cell_extra_context(self, context): extra_context = super(ProfileCell, self).get_cell_extra_context(context) diff --git a/combo/public/menu.py b/combo/public/menu.py index 4badefb..6ec2a3c 100644 --- a/combo/public/menu.py +++ b/combo/public/menu.py @@ -76,7 +76,7 @@ def get_menu_context(context, level=0, current_page=None, depth=1, ignore_visibi if not element.public: # mark the menu cell for asynchronous rendering. raise NothingInCacheException() - if not element.is_visible(context['request'].user): + if not element.is_visible(context['request']): continue menuitem = {'page': element} menuitem['selected'] = bool(element == page_of_level) diff --git a/combo/public/templatetags/combo.py b/combo/public/templatetags/combo.py index 6730229..a867987 100644 --- a/combo/public/templatetags/combo.py +++ b/combo/public/templatetags/combo.py @@ -66,7 +66,7 @@ def placeholder(context, placeholder_name, **options): context['cells'] = [x for x in page_cells if x.placeholder == placeholder_name and (context.get('render_skeleton') or x.is_relevant(context) and - x.is_visible(context['request'].user))] + x.is_visible(context['request']))] if context.get('render_skeleton') and not context['cells']: context['skeleton'] = skeleton_text(context, placeholder_name) else: diff --git a/combo/public/views.py b/combo/public/views.py index 9089e84..af9fdf9 100644 --- a/combo/public/views.py +++ b/combo/public/views.py @@ -100,14 +100,14 @@ def ajax_page_cell(request, page_pk, cell_reference): if not (request.user and request.user.is_staff): raise PermissionDenied() - if not page.is_visible(request.user): + if not page.is_visible(request): raise PermissionDenied() try: cell = CellBase.get_cell(cell_reference, page_id=page_pk) except ObjectDoesNotExist: raise Http404() - if not cell.is_visible(request.user): + if not cell.is_visible(request): raise PermissionDenied() exception = None @@ -155,7 +155,7 @@ def render_cell(request, cell): other_cells = [] for klass in CellBase.get_cell_classes(lambda x: bool(x.modify_global_context)): other_cells.extend(klass.objects.filter(page_id=cell.page_id)) - other_cells = [x for x in other_cells if x.is_visible(user=request.user)] + other_cells = [x for x in other_cells if x.is_visible(request=request)] other_cells.sort(key=lambda x: x.order) for other_cell in other_cells: if other_cell.get_reference() != cell.get_reference(): @@ -441,7 +441,7 @@ def page(request): 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 page.is_visible(request): if not request.user.is_authenticated(): from django.contrib.auth.views import redirect_to_login return redirect_to_login(request.build_absolute_uri()) @@ -459,7 +459,7 @@ def publish_page(request, page, status=200, template_name=None): cells = CellBase.get_cells(page=page) extend_with_parent_cells(cells, hierarchy=pages) - cells = [x for x in cells if x.is_visible(user=request.user)] + cells = [x for x in cells if x.is_visible(request=request)] # mark duplicated slugs to avoid using them in HTML id attributes. cell_by_slugs = {} @@ -529,7 +529,7 @@ menu_badges.mellon_no_passive = True def api_search(request): for cell in SearchCell.get_cells_by_search_service('_text'): - if not cell.is_visible(request.user): + if not cell.is_visible(request): continue break else: diff --git a/tests/test_notification.py b/tests/test_notification.py index ae5ea2c..5719a5e 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -91,9 +91,9 @@ def test_notification_cell(john_doe, jane_doe): context['synchronous'] = True # to get fresh content context['request'].user = None - assert cell.is_visible(context['request'].user) is False + assert cell.is_visible(context['request']) is False context['request'].user = john_doe - assert cell.is_visible(context['request'].user) is True + assert cell.is_visible(context['request']) is True assert cell.get_badge(context) is None notification1 = Notification.notify(john_doe, 'notibar') diff --git a/tests/test_pages.py b/tests/test_pages.py index b8f40a9..143dcb9 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -114,14 +114,19 @@ def test_page_visibility(): user1.groups = [group] user2 = User(username='bar') user2.save() + request = RequestFactory() - assert page.is_visible(user1) - assert page.is_visible(user2) + request.user = user1 + assert page.is_visible(request) + request.user = user2 + assert page.is_visible(request) page.groups = [group] assert not page.is_visible() - assert page.is_visible(user1) - assert not page.is_visible(user2) + request.user = user1 + assert page.is_visible(request) + request.user = user2 + assert not page.is_visible(request) def test_import_export_pages(): page = Page(title=u'foo', slug='foo', order=0, description="Foo's page") @@ -203,8 +208,10 @@ def test_next_previous(): user1 = User(username='foo') user1.save() - assert page.get_next_page(user1).id == page2.id - assert page3.get_previous_page(user1).id == page2.id + request = RequestFactory() + request.user = user1 + assert page.get_next_page(request).id == page2.id + assert page3.get_previous_page(request).id == page2.id page3.public = False page3.save() -- 2.20.1