Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#336 OFFSET must not be negative #343

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ Patches and Suggestions
- Ignacy Sokołowski
- Steven Harms
- David Lord @davidism
- Blake Grotewold
6 changes: 3 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ Models

.. method:: limit(limit)

Apply a LIMIT to the query and return the newly resulting query.
Apply a LIMIT to the query and return the newly resulting query.

.. method:: offset(offset)

Apply an OFFSET to the query and return the newly resulting
query.
Apply an OFFSET to the query and return the newly resulting
query. If the offset is less than 0, it will defaulted to 0.

.. method:: first()

Expand Down
12 changes: 8 additions & 4 deletions flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ def __init__(self, query, page, per_page, total, items):
#: the unlimited query object that was used to create this
#: pagination object.
self.query = query
#: the current page number (1 indexed)
self.page = page
#: the current page number (1 indexed). if it is lower than 1
#: it gets defaulted to 1.
self.page = page if page and page > 0 else 1
#: the number of items to be displayed on a page.
self.per_page = per_page
#: the total number of items matching the query
Expand Down Expand Up @@ -470,8 +471,11 @@ def paginate(self, page=None, per_page=None, error_out=True):
if per_page is None:
per_page = 20

if error_out and page < 1:
abort(404)
if page < 1:
if error_out:
abort(404)
else:
page = 1

items = self.limit(per_page).offset((page - 1) * per_page).all()

Expand Down
26 changes: 26 additions & 0 deletions test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import sessionmaker
from werkzeug.exceptions import NotFound


def make_todo_model(db):
Expand Down Expand Up @@ -352,6 +353,14 @@ def test_pagination_pages_when_0_items_per_page(self):
p = sqlalchemy.Pagination(None, 1, 0, 500, [])
self.assertEqual(p.pages, 0)

def test_pagination_page_when_page_number_is_none(self):
p = sqlalchemy.Pagination(None, None, 20, 500, [])
self.assertEqual(p.page, 1)

def test_pagination_page_when_page_number_is_negative(self):
p = sqlalchemy.Pagination(None, -1, 20, 500, [])
self.assertEqual(p.page, 1)

def test_query_paginate(self):
app = flask.Flask(__name__)
db = sqlalchemy.SQLAlchemy(app)
Expand Down Expand Up @@ -380,6 +389,23 @@ def index():
p = Todo.query.paginate()
self.assertEqual(p.total, 100)

def test_query_paginate_with_negative_page(self):
app = flask.Flask(__name__)
db = sqlalchemy.SQLAlchemy(app)
Todo = make_todo_model(db)
db.create_all()

with app.app_context():
db.session.add_all([Todo('', '') for _ in range(100)])
db.session.commit()

# with error_out
self.assertRaises(NotFound, lambda: Todo.query.paginate(page=-1))

# without error_out
p = Todo.query.paginate(page=-1, error_out=False)
self.assertEqual(p.page, 1)


class BindsTestCase(unittest.TestCase):

Expand Down