Code

Opened 2 years ago

Closed 23 months ago

#17159 closed New feature (fixed)

Paginator.Page() should throw an exception for invalid [next|previous]_page_number()

Reported by: mehta.apurva@… Owned by: neaf
Component: Core (Other) Version: 1.3
Severity: Normal Keywords: paginator core
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by julien)

The next_page_number() and previous_page_number() currently return an incremented or decremented number without verifying if the page is in the range or not. They should instead throw an exception.

Repro Steps
=======

>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo', 'bill', 'gates', 'steve', 'jobs']
>>> p = Paginator(objects, 2)
>>> p.count
8
>>> p.num_pages
4
>>> p.page_range
[1, 2, 3, 4]
>>> p.page(1).has_previous()
False
>>> p.page(4).has_next()
False
>>> p.page(1).previous_page_number()  ### Should throw Exception
0   
>>> p.page(4).next_page_number() ### Should throw Exception
5

Attachments (2)

ticket17159.diff (576 bytes) - added by amehta 2 years ago.
Patch for the issue
ticket17159-v2.diff (3.9 KB) - added by neaf 2 years ago.
Previous patch with tests and documentation update.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 2 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to mehta.apurva@…
  • Patch needs improvement unset

comment:2 Changed 2 years ago by julien

  • Description modified (diff)
  • Triage Stage changed from Unreviewed to Accepted

That makes sense. It would be a matter of calling Page.paginator.validate_number().

comment:3 Changed 2 years ago by julien

  • Type changed from Bug to New feature

Changed 2 years ago by amehta

Patch for the issue

comment:4 Changed 2 years ago by amehta

  • Has patch set
  • Resolution set to fixed
  • Status changed from new to closed

Updated the functions to call Page.paginator.validate_number()

comment:5 Changed 2 years ago by aaugustin

  • Resolution fixed deleted
  • Status changed from closed to reopened

Please don't mark tickets as fixed until the fix is actually committed to the code base.

comment:6 Changed 2 years ago by julien

  • Needs documentation set
  • Needs tests set

Thank you. This would now need some tests and documentation (https://docs.djangoproject.com/en/dev/topics/pagination/).

comment:7 Changed 2 years ago by neaf

  • Owner changed from mehta.apurva@… to neaf
  • Status changed from reopened to new

Changed 2 years ago by neaf

Previous patch with tests and documentation update.

comment:8 Changed 2 years ago by neaf

Attached another patch with tests and documentation.

I'm not sure if it isn't compatibility breaking change. I'm pretty sure no-one wants to display 0 and out-of-range pages but I don't know if we should throw exceptions where we haven't in the ast.

comment:9 Changed 2 years ago by neaf

  • Status changed from new to assigned

comment:10 Changed 2 years ago by jezdez

  • Triage Stage changed from Accepted to Ready for checkin

comment:11 Changed 2 years ago by julien

Quick note: This is slightly backward-compatible and some release notes should be added.

comment:12 Changed 23 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In [fc40a6504b78e604a6ba90a3b85b1ed54ee238a2]:

Fixed #17159 -- Validated returned number of next|previous_page_number

Thanks mehta.apurva at gmail.com for the report and the initial patch
and neaf for the complete patch.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.