def test_normalize_page_number(self): page_number = 1 page_range = range(2) val = normalize_page_number(page_number, page_range) self.assertTrue(bool(val)) page_range = range(1) val = normalize_page_number(page_number, page_range) self.assertFalse(bool(val))
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) # 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 ''
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) # 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 ''