Skip to content

Commit 97ae31f

Browse files
authored
Merge pull request #289 from MongoEngine/better-documentation
Better documentation around pagination
2 parents bd44c79 + 9b2fd78 commit 97ae31f

File tree

8 files changed

+93
-7
lines changed

8 files changed

+93
-7
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
#
5050
import flask_mongoengine
5151
# The short X.Y version.
52-
version = flask_mongoengine.get_version()
52+
version = flask_mongoengine.__version__
5353
# The full version, including alpha/beta/rc tags.
54-
release = flask_mongoengine.get_version()
54+
release = flask_mongoengine.__version__
5555

5656
# The language for content autogenerated by Sphinx. Refer to documentation
5757
# for a list of supported languages.

docs/index.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,17 @@ has_prev, next_num, prev_num.
107107

108108
In the template::
109109

110-
{% macro render_pagination(pagination, endpoint) %}
110+
{# Display a page of todos #}
111+
<ul>
112+
{% for todo in paginated_todos.items %}
113+
<li>{{ todo.title }}</li>
114+
{% endfor %}
115+
</ul>
116+
117+
{# Macro for creating navigation links #}
118+
{% macro render_navigation(pagination, endpoint) %}
111119
<div class=pagination>
112-
{%- for page in pagination.iter_pages() %}
120+
{% for page in pagination.iter_pages() %}
113121
{% if page %}
114122
{% if page != pagination.page %}
115123
<a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
@@ -119,10 +127,12 @@ In the template::
119127
{% else %}
120128
<span class=ellipsis>…</span>
121129
{% endif %}
122-
{%- endfor %}
130+
{% endfor %}
123131
</div>
124132
{% endmacro %}
125133

134+
{{ render_navigation(paginated_todos, 'view_todos') }}
135+
126136

127137
MongoEngine and WTForms
128138
=======================

examples/biggerapp/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828

2929
DebugToolbarExtension(app)
3030

31-
from views import index
31+
from views import index, pagination
3232
app.add_url_rule('/', view_func=index)
33+
app.add_url_rule('/pagination', view_func=pagination)
3334

3435
if __name__ == "__main__":
3536
app.run(host="0.0.0.0", port=4000)

examples/biggerapp/templates/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ <h2>{{ todo.title }}</h2>
88
{% else %}
99
<em>Unbelievable. No todos here so far <a href="/add">Add one</a></em>
1010
{% endfor %}
11+
<br/>
12+
<a href="{{ url_for('pagination') }}">See pagination</a>
1113
{% endblock %}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{% extends "layout.html" %}
2+
3+
{# Macro for creating navigation links #}
4+
{% macro render_navigation(pagination, endpoint) %}
5+
<div class=pagination>
6+
{% for page in pagination.iter_pages() %}
7+
{% if page %}
8+
{% if page != pagination.page %}
9+
<a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
10+
{% else %}
11+
<strong>{{ page }}</strong>
12+
{% endif %}
13+
{% else %}
14+
<span class=ellipsis></span>
15+
{% endif %}
16+
{% endfor %}
17+
</div>
18+
{% endmacro %}
19+
20+
{% block body %}
21+
22+
<div class="todos">
23+
<ul>
24+
{% for todo in todos_page.items %}
25+
<li>{{ todo.title }}</li>
26+
{% endfor %}
27+
</ul>
28+
</div>
29+
30+
<div class="navigation">
31+
{{ render_navigation(todos_page, 'pagination') }}
32+
</div>
33+
34+
35+
{% endblock %}

examples/biggerapp/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ def index():
1010
todos = list(Todo.objects[:10])
1111
todos = Todo.objects.all()
1212
return flask.render_template('index.html', todos=todos)
13+
14+
def pagination():
15+
Todo.objects().delete()
16+
for i in range(10):
17+
Todo(title='Simple todo {}'.format(i), text="12345678910").save() # Insert
18+
19+
page_num = int(flask.request.args.get('page') or 1)
20+
todos_page = Todo.objects.paginate(page=page_num, per_page=3)
21+
22+
return flask.render_template('pagination.html', todos_page=todos_page)

flask_mongoengine/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
from .wtf import WtfBaseField
1717

1818

19+
VERSION = (0, 9, 0)
20+
21+
22+
def get_version():
23+
"""Return the VERSION as a string, e.g. for VERSION == (0, 9, 0),
24+
return '0.9.0'.
25+
"""
26+
return '.'.join(map(str, VERSION))
27+
28+
29+
__version__ = get_version()
30+
31+
1932
def _patch_base_field(obj, name):
2033
"""
2134
If the object submitted has a class whose base class is

setup.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,26 @@
1515
except:
1616
pass
1717

18+
19+
def get_version(version_tuple):
20+
"""Return the version tuple as a string, e.g. for (0, 10, 7),
21+
return '0.10.7'.
22+
"""
23+
return '.'.join(map(str, version_tuple))
24+
25+
26+
# Dirty hack to get version number from flask_monogengine/__init__.py - we
27+
# can't import it as it depends on PyMongo and PyMongo isn't installed until
28+
# this file is read
29+
init = os.path.join(os.path.dirname(__file__), 'flask_mongoengine', '__init__.py')
30+
version_line = list(filter(lambda l: l.startswith('VERSION'), open(init)))[0]
31+
version = get_version(eval(version_line.split('=')[-1]))
32+
1833
test_requirements = ['coverage', 'nose', 'rednose']
1934

2035
setup(
2136
name='flask-mongoengine',
22-
version='0.9.0',
37+
version=version,
2338
url='https://github.com/mongoengine/flask-mongoengine',
2439
license='BSD',
2540
author='Ross Lawley',

0 commit comments

Comments
 (0)