From aec71d1c174fb9dafe0a00807934843c2f18ab25 Mon Sep 17 00:00:00 2001 From: zorun Date: Thu, 20 Feb 2020 09:43:00 +0100 Subject: [PATCH] Paginate the list of bills (#480) 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",