From 8ac1bdf6517cb0a492daab05dc49e58d3f02e2ab Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Tue, 1 Oct 2019 12:06:33 +0200 Subject: [PATCH] Paginate the list of bills We display 100 bills on each page. We only show previous/next buttons (at the top of the view) and the list of pages (at the bottom) if there are more than one pages. This uses built-in pagination support from Flask-SQLAlchemy: https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy.BaseQuery.paginate https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy.Pagination --- ihatemoney/static/css/main.css | 9 +++++++++ ihatemoney/templates/list_bills.html | 28 ++++++++++++++++++++++++++-- ihatemoney/web.py | 6 +++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/ihatemoney/static/css/main.css b/ihatemoney/static/css/main.css index 1a7dc1fb6..9c02b283d 100644 --- a/ihatemoney/static/css/main.css +++ b/ihatemoney/static/css/main.css @@ -272,6 +272,15 @@ footer .footer-left { margin-top: 30px; } +#previous-page { + margin-top: 30px; +} + +#next-page { + margin-top: 30px; + padding-left: 2em; +} + /* Avoid text color flickering when it loose focus as the modal appears */ .btn-primary[data-toggle="modal"] { color: #fff; diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index 84f729940..0f2a68a5b 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -97,13 +97,23 @@ - {% if bills.count() > 0 %} +{% if bills.pages > 1 %} + + « {{ _("Newer bills") }} + + + + {{ _("Older bills") }} » + +{% endif %} + + {% if bills.total > 0 %}
- {% for bill in bills %} + {% for bill in bills.items %}
{{ _("When?") }}{{ _("Who paid?") }}{{ _("For what?") }}{{ _("For whom?") }}{{ _("How much?") }}{{ _("Actions") }}
{{ _('Add a bill') }}
+{% if bills.pages > 1 %} + +{% endif %} + {% else %}
diff --git a/ihatemoney/web.py b/ihatemoney/web.py index d45190c8e..1b80ab622 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -586,7 +586,11 @@ def list_bills(): if "last_selected_payer" in session: bill_form.payer.data = session["last_selected_payer"] # Preload the "owers" relationship for all bills - bills = g.project.get_bills().options(orm.subqueryload(Bill.owers)) + bills = ( + g.project.get_bills() + .options(orm.subqueryload(Bill.owers)) + .paginate(per_page=100, error_out=True) + ) return render_template( "list_bills.html",