def get_context_data(self, *args, **kwargs): user = self.request.user all_count = self.get_base_queryset().count() queryset = self.get_queryset() result_count = queryset.count() paginator = Paginator(queryset, self.items_per_page) try: page_obj = paginator.page(self.page_num + 1) except InvalidPage: page_obj = paginator.page(1) context = { 'view': self, 'all_count': all_count, 'result_count': result_count, 'paginator': paginator, 'page_obj': page_obj, 'object_list': page_obj.object_list, 'user_can_create': self.permission_helper.user_can_create(user) } if self.is_pagemodel: models = self.model.allowed_parent_page_models() allowed_parent_types = [m._meta.verbose_name for m in models] valid_parents = self.permission_helper.get_valid_parent_pages(user) valid_parent_count = valid_parents.count() context.update({ 'no_valid_parents': not valid_parent_count, 'required_parent_types': allowed_parent_types, }) return context
def index(request): form = SearchForm(placeholder=_("Search something")) example_form = ExampleForm() messages.success(request, _("Success message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.warning(request, _("Warning message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.error(request, _("Error message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) paginator = Paginator(list(range(100)), 10) page = paginator.page(2) return render(request, 'wagtailstyleguide/base.html', { 'search_form': form, 'example_form': example_form, 'example_page': page, })
def test_no_next_on_last_page(self): paginator = Paginator(SimplePaginatedModel.objects.all(), 2) page = paginator.page(5) self.assertTrue(page.has_previous()) self.assertFalse(page.has_next()) page = paginator.page(4) self.assertTrue(page.has_previous()) self.assertTrue(page.has_next())
def test_no_previous_on_first_page(self): with sleuth.watch('djangae.db.backends.appengine.commands.datastore.Query.Run') as query: paginator = Paginator(SimplePaginatedModel.objects.all(), 2) self.assertFalse(query.called) page = paginator.page(1) self.assertFalse(page.has_previous()) page = paginator.page(2) self.assertTrue(page.has_previous())
def test_results_correct(self): paginator = Paginator(SimplePaginatedModel.objects.all(), 2) page = paginator.page(2) self.assertEqual(page[0].field1, 2) self.assertEqual(page[1].field1, 3) page = paginator.page(3) self.assertEqual(page[0].field1, 4) self.assertEqual(page[1].field1, 5)
def paginate(request, items, page_key=DEFAULT_PAGE_KEY, per_page=20): page = request.GET.get(page_key, 1) paginator = Paginator(items, per_page) try: page = paginator.page(page) except PageNotAnInteger: page = paginator.page(1) except EmptyPage: page = paginator.page(1) return paginator, page
def test_page_runs_limited_query(self): with sleuth.watch('djangae.db.backends.appengine.commands.datastore.Query.Run') as query: paginator = Paginator(SimplePaginatedModel.objects.all(), 2) self.assertFalse(query.called) page = paginator.page(1) self.assertFalse(page.has_previous()) self.assertTrue(page.has_next()) self.assertTrue(query.called) # Should've queried for 1 more than we asked to determine if there is a next # page or not self.assertEqual(query.calls[0].kwargs["limit"], 3) self.assertEqual(len(page.object_list), 2)
def test_num_pages_raises_error(self): paginator = Paginator(SimplePaginatedModel.objects.all(), 2) with self.assertRaises(NotImplementedError): paginator.num_pages
def search( request, template=None, template_ajax=None, results_per_page=10, use_json=False, json_attrs=['title', 'url'], show_unpublished=False, search_title_only=False, extra_filters={}, path=None): # Get default templates if template is None: if hasattr(settings, 'WAGTAILSEARCH_RESULTS_TEMPLATE'): template = settings.WAGTAILSEARCH_RESULTS_TEMPLATE else: template = 'wagtailsearch/search_results.html' if template_ajax is None: if hasattr(settings, 'WAGTAILSEARCH_RESULTS_TEMPLATE_AJAX'): template_ajax = settings.WAGTAILSEARCH_RESULTS_TEMPLATE_AJAX else: template_ajax = template # Get query string and page from GET paramters query_string = request.GET.get('q', '') page = request.GET.get('page', request.GET.get('p', 1)) # Search if query_string != '': pages = models.Page.objects.filter(path__startswith=(path or request.site.root_page.path)) if not show_unpublished: pages = pages.live() if extra_filters: pages = pages.filter(**extra_filters) if search_title_only: search_results = pages.search(query_string, fields=['title']) else: search_results = pages.search(query_string) # Get query object query = Query.get(query_string) # Add hit query.add_hit() # Pagination paginator = Paginator(search_results, results_per_page) try: search_results = paginator.page(page) except PageNotAnInteger: search_results = paginator.page(1) except EmptyPage: search_results = paginator.page(1) else: query = None search_results = None if use_json: # Return a json response if search_results: search_results_json = [] for result in search_results: result_specific = result.specific search_results_json.append(dict( (attr, getattr(result_specific, attr)) for attr in json_attrs if hasattr(result_specific, attr) )) return JsonResponse(search_results_json, safe=False) else: return JsonResponse([], safe=False) else: # Render a template if request.is_ajax() and template_ajax: template = template_ajax return render(request, template, dict( query_string=query_string, search_results=search_results, is_ajax=request.is_ajax(), query=query ))