diff --git a/app/api/serializer.py b/app/api/serializer.py index 0a9d34a1..f02c4dc8 100644 --- a/app/api/serializer.py +++ b/app/api/serializer.py @@ -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: diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index f6aa8fa8..2d03b940 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -63,8 +63,19 @@
+ +
@@ -286,11 +297,11 @@ diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index ca5c1941..520d5d8e 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -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, )