From c47fb44c1e0e6bd6a08f864b28f6e503372a0fa4 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 23 May 2020 22:34:46 +0200 Subject: [PATCH] Pagination for contact page --- .../dashboard/alias_contact_manager.html | 19 +++++++++++++++++ app/dashboard/views/alias_contact_manager.py | 21 +++++++++++++------ app/models.py | 13 +++++++++++- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/dashboard/templates/dashboard/alias_contact_manager.html b/app/dashboard/templates/dashboard/alias_contact_manager.html index b27071a3..f81b3a59 100644 --- a/app/dashboard/templates/dashboard/alias_contact_manager.html +++ b/app/dashboard/templates/dashboard/alias_contact_manager.html @@ -103,6 +103,25 @@ {% endfor %} +
+
+ +
+
+ {% endblock %} diff --git a/app/dashboard/views/alias_contact_manager.py b/app/dashboard/views/alias_contact_manager.py index 8f3f4752..3e65fd18 100644 --- a/app/dashboard/views/alias_contact_manager.py +++ b/app/dashboard/views/alias_contact_manager.py @@ -5,7 +5,7 @@ from flask_login import login_required, current_user from flask_wtf import FlaskForm from wtforms import StringField, validators, ValidationError -from app.config import EMAIL_DOMAIN +from app.config import EMAIL_DOMAIN, PAGE_LIMIT from app.dashboard.base import dashboard_bp from app.email_utils import parseaddr_unicode from app.extensions import db @@ -54,6 +54,10 @@ def alias_contact_manager(alias_id): alias = Alias.get(alias_id) + page = 0 + if request.args.get("page"): + page = int(request.args.get("page")) + # sanity check if not alias: flash("You do not have access to this page", "warning") @@ -146,12 +150,15 @@ def alias_contact_manager(alias_id): ) # make sure highlighted contact is at array start - contacts = alias.contacts + contacts = alias.get_contacts(page) + contact_ids = [contact.id for contact in contacts] - if highlight_contact_id: - contacts = sorted( - contacts, key=lambda fe: fe.id == highlight_contact_id, reverse=True - ) + last_page = len(contacts) < PAGE_LIMIT + + if highlight_contact_id not in contact_ids: + contact = Contact.get(highlight_contact_id) + if contact and contact.alias_id == alias.id: + contacts.insert(0, contact) return render_template( "dashboard/alias_contact_manager.html", @@ -159,4 +166,6 @@ def alias_contact_manager(alias_id): alias=alias, new_contact_form=new_contact_form, highlight_contact_id=highlight_contact_id, + page=page, + last_page=last_page, ) diff --git a/app/models.py b/app/models.py index 68f473d4..eaba9ded 100644 --- a/app/models.py +++ b/app/models.py @@ -26,6 +26,7 @@ from app.config import ( LANDING_PAGE_URL, FIRST_ALIAS_DOMAIN, DISABLE_ONBOARDING, + PAGE_LIMIT, ) from app.errors import AliasInTrashError from app.extensions import db @@ -750,6 +751,16 @@ class Alias(db.Model, ModelMixin): else: return self.user.email + def get_contacts(self, page=0): + contacts = ( + Contact.filter_by(alias_id=self.id) + .order_by(Contact.created_at) + .limit(PAGE_LIMIT) + .offset(page * PAGE_LIMIT) + .all() + ) + return contacts + def __repr__(self): return f"" @@ -871,7 +882,7 @@ class Contact(db.Model, ModelMixin): # whether a contact is created via CC is_cc = db.Column(db.Boolean, nullable=False, default=False, server_default="0") - alias = db.relationship(Alias, backref="contacts") + alias = db.relationship(Alias) user = db.relationship(User) def website_send_to(self):