support sorting: Oldest Alias to Newest,

This commit is contained in:
Son NK 2020-04-25 12:46:20 +02:00
parent ca6350cc27
commit 36aee86590
3 changed files with 35 additions and 13 deletions

View file

@ -109,7 +109,9 @@ def get_alias_infos_with_pagination(user, page_id=0, query=None) -> [AliasInfo]:
return ret
def get_alias_infos_with_pagination_v2(user, page_id=0, query=None) -> [AliasInfo]:
def get_alias_infos_with_pagination_v2(
user, page_id=0, query=None, sort=None
) -> [AliasInfo]:
ret = []
latest_activity = func.max(
case(
@ -122,13 +124,11 @@ def get_alias_infos_with_pagination_v2(user, page_id=0, query=None) -> [AliasInf
).label("latest")
q = (
db.session.query(Alias, latest_activity)
.options(joinedload(Alias.mailbox))
db.session.query(Alias, Mailbox, latest_activity)
.join(Contact, Alias.id == Contact.alias_id, isouter=True)
.join(EmailLog, Contact.id == EmailLog.contact_id, isouter=True)
.filter(Alias.user_id == user.id)
.group_by(Alias.id)
.order_by(latest_activity.desc())
.filter(Alias.mailbox_id == Mailbox.id)
)
if query:
@ -136,10 +136,18 @@ def get_alias_infos_with_pagination_v2(user, page_id=0, query=None) -> [AliasInf
or_(Alias.email.ilike(f"%{query}%"), Alias.note.ilike(f"%{query}%"))
)
if sort == "old2new":
q = q.order_by(Alias.created_at)
else:
# default sorting
q = q.order_by(latest_activity.desc())
q = q.group_by(Alias.id, Mailbox.id)
q = q.limit(PAGE_LIMIT).offset(page_id * PAGE_LIMIT)
for alias, latest_activity in q:
ret.append(get_alias_info_v2(alias))
for alias, mailbox, latest_activity in q:
ret.append(get_alias_info_v2(alias, mailbox))
return ret
@ -166,7 +174,7 @@ def get_alias_info(alias: Alias) -> AliasInfo:
return alias_info
def get_alias_info_v2(alias: Alias) -> AliasInfo:
def get_alias_info_v2(alias: Alias, mailbox) -> AliasInfo:
q = (
db.session.query(Contact, EmailLog)
.filter(Contact.alias_id == alias.id)
@ -178,7 +186,7 @@ def get_alias_info_v2(alias: Alias) -> AliasInfo:
latest_contact = None
alias_info = AliasInfo(
alias=alias, nb_blocked=0, nb_forward=0, nb_reply=0, mailbox=alias.mailbox
alias=alias, nb_blocked=0, nb_forward=0, nb_reply=0, mailbox=mailbox
)
for contact, email_log in q:

View file

@ -63,8 +63,19 @@
<div class="col-lg-6 pt-1">
<div class="float-right">
<form method="get" class="form-inline">
<select name="sort"
onchange="this.form.submit()"
class="form-control custom-select mr-3">
<option value="" {% if sort == "" %} selected {% endif %}>
Sort by most recent activity
</option>
<option value="old2new" {% if sort == "old2new" %} selected {% endif %}>
Oldest Alias to Newest
</option>
</select>
<input type="search" name="query" placeholder="Enter to search for alias"
class="form-control shadow text-right"
class="form-control shadow"
style="max-width: 15em"
value="{{ query }}">
</form>
@ -286,11 +297,11 @@
<ul class="pagination">
<li class="page-item {% if page == 0 %}disabled{% endif %}">
<a class="page-link"
href="{{ url_for('dashboard.index', page=page-1, query=query) }}">Previous</a>
href="{{ url_for('dashboard.index', page=page-1, query=query, sort=sort) }}">Previous</a>
</li>
<li class="page-item {% if last_page %}disabled{% endif %}">
<a class="page-link"
href="{{ url_for('dashboard.index', page=page+1, query=query) }}">Next</a>
href="{{ url_for('dashboard.index', page=page+1, query=query, sort=sort) }}">Next</a>
</li>
</ul>
</nav>

View file

@ -21,6 +21,8 @@ from app.models import (
@login_required
def index():
query = request.args.get("query") or ""
sort = request.args.get("sort") or ""
page = 0
if request.args.get("page"):
page = int(request.args.get("page"))
@ -169,11 +171,12 @@ def index():
return render_template(
"dashboard/index.html",
client_users=client_users,
alias_infos=get_alias_infos_with_pagination_v2(current_user, page, query),
alias_infos=get_alias_infos_with_pagination_v2(current_user, page, query, sort),
highlight_alias_id=highlight_alias_id,
query=query,
AliasGeneratorEnum=AliasGeneratorEnum,
mailboxes=mailboxes,
show_intro=show_intro,
page=page,
sort=sort,
)