def test_in_range(self): # Ensure the correct page number is returned when the requested # negative index is in range. page_numbers = [-1, -2, -3, -4] expected_results = reversed(self.page_range) for page_number, expected in zip(page_numbers, expected_results): result = utils.normalize_page_number(page_number, self.page_range) self.assertEqual(expected, result)
def render(self, context): # Handle page number when it is not specified in querystring. if self.page_number_variable is None: default_number = self.page_number else: default_number = int(self.page_number_variable.resolve(context)) # Calculate the number of items to show on each page. if self.per_page_variable is None: per_page = self.per_page else: per_page = int(self.per_page_variable.resolve(context)) # Calculate the number of items to show in the first page. if self.first_page_variable is None: first_page = self.first_page or per_page else: first_page = int(self.first_page_variable.resolve(context)) # User can override the querystring key to use in the template. # The default value is defined in the settings file. if self.querystring_key_variable is None: querystring_key = self.querystring_key else: querystring_key = self.querystring_key_variable.resolve(context) # Retrieve the override path if used. if self.override_path_variable is None: override_path = self.override_path else: override_path = self.override_path_variable.resolve(context) # Retrieve the queryset and create the paginator object. objects = self.objects.resolve(context) paginator = self.paginator(objects, per_page, first_page=first_page, orphans=settings.ORPHANS) # Normalize the default page number if a negative one is provided. if default_number < 0: default_number = utils.normalize_page_number( default_number, paginator.page_range) # The current request is used to get the requested page number. page_number = utils.get_page_number_from_request( context['request'], querystring_key, default=default_number) # Get the page. try: page = paginator.page(page_number) except EmptyPage: page = paginator.page(1) if settings.PAGE_OUT_OF_RANGE_404: raise Http404('Page out of range') # CUSTOMIZATION: Add last object on previous page if page_number > 1: try: paginator_prev = self.paginator(objects, per_page, first_page=first_page, orphans=settings.ORPHANS) previous_page = paginator_prev.page(page_number - 1) last_object = previous_page.object_list[-1] except EmptyPage: last_object = None else: last_object = None # Populate the context with required data. data = { 'default_number': default_number, 'override_path': override_path, 'page': page, 'querystring_key': querystring_key, 'last_object': last_object } context.update({'endless': data, self.var_name: page.object_list}) return ''
def render(self, context): # Handle page number when it is not specified in querystring. if self.page_number_variable is None: default_number = self.page_number else: default_number = int(self.page_number_variable.resolve(context)) # Calculate the number of items to show on each page. if self.per_page_variable is None: per_page = self.per_page else: per_page = int(self.per_page_variable.resolve(context)) # Calculate the number of items to show in the first page. if self.first_page_variable is None: first_page = self.first_page or per_page else: first_page = int(self.first_page_variable.resolve(context)) # User can override the querystring key to use in the template. # The default value is defined in the settings file. if self.querystring_key_variable is None: querystring_key = self.querystring_key else: querystring_key = self.querystring_key_variable.resolve(context) # Retrieve the override path if used. if self.override_path_variable is None: override_path = self.override_path else: override_path = self.override_path_variable.resolve(context) # Retrieve the queryset and create the paginator object. objects = self.objects.resolve(context) paginator = self.paginator( objects, per_page, first_page=first_page, orphans=settings.ORPHANS) # Normalize the default page number if a negative one is provided. if default_number < 0: default_number = utils.normalize_page_number( default_number, paginator.page_range) # The current request is used to get the requested page number. page_number = utils.get_page_number_from_request( context['request'], querystring_key, default=default_number) # Get the page. try: page = paginator.page(page_number) except EmptyPage: page = paginator.page(1) if settings.PAGE_OUT_OF_RANGE_404: raise Http404('Page out of range') # Populate the context with required data. data = { 'default_number': default_number, 'override_path': override_path, 'page': page, 'querystring_key': querystring_key, } context.update({'endless': data, self.var_name: page.object_list}) return ''
def test_out_of_range(self): # Ensure the page number 1 returned when the requested negative index # is out of range. result = utils.normalize_page_number(-5, self.page_range) self.assertEqual(self.page_range[0], result)
def render(self, context): # Handle page number when it is not specified in querystring. if self.page_number_variable is None: default_number = self.page_number else: default_number = int(self.page_number_variable.resolve(context)) # Calculate the number of items to show on each page. if self.per_page_variable is None: per_page = self.per_page else: per_page = int(self.per_page_variable.resolve(context)) # Calculate the number of items to show in the first page. if self.first_page_variable is None: first_page = self.first_page or per_page else: first_page = int(self.first_page_variable.resolve(context)) # User can override the querystring key to use in the template. # The default value is defined in the settings file. if self.querystring_key_variable is None: querystring_key = self.querystring_key else: querystring_key = self.querystring_key_variable.resolve(context) # Retrieve the override path if used. if self.override_path_variable is None: override_path = self.override_path else: override_path = self.override_path_variable.resolve(context) # Retrieve the queryset and create the paginator object. objects = self.objects.resolve(context) # objects = objects.order_by('-created_at') paginator = self.paginator(objects, per_page, first_page=first_page, orphans=settings.ORPHANS) # Normalize the default page number if a negative one is provided. if default_number < 0: default_number = utils.normalize_page_number( default_number, paginator.page_range) # The current request is used to get the requested page number. page_number = utils.get_page_number_from_request( context['request'], querystring_key, default=default_number) # Get the page. try: page = paginator.page(page_number) except EmptyPage: page = paginator.page(1) # Populate the context with required data. data = { 'default_number': default_number, 'override_path': override_path, 'page': page, 'querystring_key': querystring_key, } context.update({'endless': data, self.var_name: page.object_list}) return ''