Pagination for contact page

This commit is contained in:
Son NK 2020-05-23 22:34:46 +02:00
parent 1d62400aac
commit c47fb44c1e
3 changed files with 46 additions and 7 deletions

View file

@ -103,6 +103,25 @@
{% endfor %}
</div>
<div class="row mt-3">
<div class="col">
<nav aria-label="Contact navigation">
<ul class="pagination">
<li class="page-item">
<a class="btn btn-outline-secondary {% if page == 0 %}disabled{% endif %}"
href="{{ url_for('dashboard.alias_contact_manager', alias_id=alias.id, page=page-1) }}">
Previous</a>
</li>
<li class="page-item">
<a class="btn btn-outline-secondary {% if last_page %}disabled{% endif %}"
href="{{ url_for('dashboard.alias_contact_manager', alias_id=alias.id, page=page+1) }}">
Next</a>
</li>
</ul>
</nav>
</div>
</div>
{% endblock %}

View file

@ -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,
)

View file

@ -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"<Alias {self.id} {self.email}>"
@ -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):