forked from MongoEngine/flask-mongoengine
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
185 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
## Current maintainers: | ||
|
||
- Ido Shraga | ||
- Andrey Shpak | ||
|
||
This project is made by incredible authors: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# Pagination | ||
|
||
flask-mongoengine attaches the following methods to Mongoengine's default QuerySet: | ||
|
||
* **paginate**: paginates the QuerySet. Takes two required arguments, *page* and *per_page*. | ||
And two optional arguments: *max_depth*, *first_page_index*. | ||
* **paginate_by_keyset**: paginates the QuerySet. Takes two required arguments, | ||
*per_page* and *field_filter_by*. | ||
from the second page you need also the last id of the previous page. | ||
Arguments: *per_page*, *field_filter_by*, *last_field_value*. | ||
* **paginate_field**: paginates a field from one document in the QuerySet. | ||
Arguments: *field_name*, *doc_id*, *page*, *per_page*. | ||
And two optional arguments: *total*, *first_page_index* | ||
|
||
## Offset pagination | ||
|
||
Offset API pagination is the most common form of API pagination. | ||
It’s also sometimes called page-based pagination. | ||
An API endpoint accepts a parameter for a page number and per page then | ||
returns that page values. | ||
|
||
### Example 1 | ||
|
||
```python | ||
from flask import request | ||
|
||
page = request.args.get('page', 1, type=int) | ||
per_page = request.args.get('per_page', 10, type=int) | ||
|
||
paginated_todos = Todo.objects.paginate(page=page, per_page=per_page) | ||
|
||
``` | ||
|
||
### Example 2 | ||
|
||
```python | ||
from flask import request | ||
from flask_mongoengine import Pagination | ||
|
||
page = request.args.get('page', 1, type=int) | ||
per_page = request.args.get('per_page', 10, type=int) | ||
|
||
Pagination(Todo.objects, page=page, per_page=10) | ||
``` | ||
|
||
## Keyset pagination | ||
|
||
Keyset-based API pagination is when an API provides a key parameter that | ||
delineates the query results. | ||
One example is if an API is sorted by ID, one key parameter could be since_id. | ||
|
||
Keyset pagination is a great way to paginate through a large dataset | ||
without having to worry about the page number. | ||
It also allows for a more consistent user experience as the user can | ||
always see the same results when they go to the next page. | ||
|
||
### Example 1 | ||
|
||
```python | ||
from flask import request | ||
|
||
field_filter_by = '_id' | ||
per_page = request.args.get('per_page', 10, type=int) | ||
|
||
paginated_todos = Todo.objects.paginate_by_keyset(per_page=per_page, field_filter_by=field_filter_by) | ||
|
||
# PAGE 2 | ||
paginated_todos_2 = Todo.objects.paginate_by_keyset(per_page=per_page, field_filter_by=field_filter_by, | ||
last_field_value='value_of last page') | ||
``` | ||
|
||
### Example 2 | ||
|
||
```python | ||
from flask import request | ||
from flask_mongoengine import KeysetPagination | ||
|
||
field_filter_by = '_id' | ||
per_page = request.args.get('per_page', 10, type=int) | ||
|
||
|
||
KeysetPagination(Todo.objects, per_page=per_page, field_filter_by=field_filter_by) | ||
``` | ||
|
||
## List field pagination | ||
|
||
List field pagination is when an API provides a key parameter that is | ||
the field name that is list of values and paginate on this list. | ||
|
||
## Example 1 | ||
|
||
```python | ||
from flask import request | ||
|
||
page = request.args.get('page', 1, type=int) | ||
per_page = request.args.get('per_page', 10, type=int) | ||
todo_id = request.args.get('todo_id', 10) | ||
|
||
todo = Todo.objects.get_or_404(_id=todo_id) | ||
paginated_tags = todo.paginate_field(field_name='tags', page=page, per_page=per_page) | ||
``` | ||
|
||
## Example 2 | ||
|
||
```python | ||
from flask import request | ||
|
||
page = request.args.get('page', 1, type=int) | ||
per_page = request.args.get('per_page', 10, type=int) | ||
todo_id = request.args.get('todo_id', 10) | ||
|
||
|
||
paginated_tags = Todo.paginate_field(Todo.objects, doc_id=todo_id, field_name='tags', | ||
page=page, per_page=per_page) | ||
``` | ||
|
||
## Example 3 | ||
|
||
```python | ||
from flask import request | ||
from flask_mongoengine import ListFieldPagination | ||
|
||
page = request.args.get('page', 1, type=int) | ||
per_page = request.args.get('per_page', 10, type=int) | ||
todo_id = request.args.get('todo_id', 10) | ||
|
||
|
||
ListFieldPagination(Todo.objects, doc_id=todo_id, field_name='tags', | ||
page=page, per_page=per_page) | ||
``` | ||
|
||
## Render page with pagination | ||
|
||
Properties of the pagination object include: iter_pages, next, prev, has_next, | ||
has_prev, next_num, prev_num. | ||
|
||
In the template: | ||
|
||
```html | ||
{# Display a page of todos #} | ||
<ul> | ||
{% for todo in paginated_todos.items %} | ||
<li>{{ todo.title }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
{# Macro for creating navigation links #} | ||
{% macro render_navigation(pagination, endpoint) %} | ||
<div class=pagination> | ||
{% for page in pagination.iter_pages() %} | ||
{% if page %} | ||
{% if page != pagination.page %} | ||
<a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a> | ||
{% else %} | ||
<strong>{{ page }}</strong> | ||
{% endif %} | ||
{% else %} | ||
<span class=ellipsis>…</span> | ||
{% endif %} | ||
{% endfor %} | ||
</div> | ||
{% endmacro %} | ||
|
||
{{ render_navigation(paginated_todos, 'view_todos') }} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[project] | ||
name = "flask-mongoengine" | ||
name = "flask-mongoengin-2" | ||
description = "Flask extension that provides integration with MongoEngine and WTF model forms." | ||
readme = "README.md" | ||
requires-python = ">=3.8" | ||
|
@@ -42,6 +42,7 @@ authors = [ | |
{name = "Ross Lawley", email = "[email protected]"} | ||
] | ||
maintainers = [ | ||
{name = "Ido Shraga", email = "[email protected]"}, | ||
{name = "Andrey Shpak", email = "[email protected]"} | ||
] | ||
dynamic = ["version"] | ||
|
@@ -73,10 +74,10 @@ legacy-dev = [ | |
] | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/MongoEngine/flask-mongoengine" | ||
Homepage = "https://github.com/idoshr/flask-mongoengine" | ||
Documentation = "http://docs.mongoengine.org/projects/flask-mongoengine/en/latest/" | ||
Repository = "https://github.com/MongoEngine/flask-mongoengine" | ||
Changelog = "https://github.com/MongoEngine/flask-mongoengine/releases" | ||
Repository = "https://github.com/idoshr/flask-mongoengine" | ||
Changelog = "https://github.com/idoshr/flask-mongoengine/releases" | ||
|
||
[build-system] | ||
requires = [ | ||
|
@@ -99,7 +100,7 @@ write_to = "flask_mongoengine/_version.py" | |
|
||
[tool.black] | ||
line-length = 88 | ||
target-version = ['py37'] | ||
target-version = ['py38'] | ||
exclude = ''' | ||
/( | ||
\.eggs | ||
|