From 95b71435f92e15e0e93cf4d5c704b63cc1b3d007 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sat, 25 Apr 2020 11:27:27 +0200 Subject: [PATCH] refactoring: use get_alias_infos_with_pagination_v2 --- app/dashboard/views/index.py | 72 +----------------------------------- 1 file changed, 2 insertions(+), 70 deletions(-) diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index d4942cdb..b00bc1c3 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -8,6 +8,7 @@ from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import joinedload from app import email_utils +from app.api.serializer import get_alias_infos_with_pagination_v2 from app.config import PAGE_LIMIT from app.dashboard.base import dashboard_bp from app.extensions import db @@ -188,7 +189,7 @@ def index(): return render_template( "dashboard/index.html", client_users=client_users, - alias_infos=get_alias_infos_with_pagination(current_user, page, query), + alias_infos=get_alias_infos_with_pagination_v2(current_user, page, query), highlight_alias_id=highlight_alias_id, query=query, AliasGeneratorEnum=AliasGeneratorEnum, @@ -196,72 +197,3 @@ def index(): show_intro=show_intro, page=page, ) - - -def get_alias_infos_with_pagination(user, page: int, query=None) -> [AliasInfo]: - ret: [AliasInfo] = [] - latest_activity = func.max( - case( - [ - (Alias.created_at > EmailLog.created_at, Alias.created_at), - (Alias.created_at < EmailLog.created_at, EmailLog.created_at), - ], - else_=Alias.created_at, - ) - ).label("latest") - - q = ( - db.session.query(Alias, latest_activity) - .options(joinedload(Alias.mailbox)) - .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()) - ) - - if query: - q = q.filter( - or_(Alias.email.ilike(f"%{query}%"), Alias.note.ilike(f"%{query}%")) - ) - - q = q.limit(PAGE_LIMIT).offset(page * PAGE_LIMIT) - - for alias, latest_activity in q: - ret.append(get_alias_info(alias)) - - return ret - - -def get_alias_info(alias: Alias) -> AliasInfo: - q = ( - db.session.query(Contact, EmailLog) - .filter(Contact.alias_id == alias.id) - .filter(EmailLog.contact_id == Contact.id) - ) - - latest_activity: Arrow = alias.created_at - latest_email_log = None - latest_contact = None - - alias_info = AliasInfo( - alias=alias, nb_blocked=0, nb_forward=0, nb_reply=0, mailbox=alias.mailbox - ) - - for contact, email_log in q: - if email_log.is_reply: - alias_info.nb_reply += 1 - elif email_log.blocked: - alias_info.nb_blocked += 1 - else: - alias_info.nb_forward += 1 - - if email_log.created_at > latest_activity: - latest_activity = email_log.created_at - latest_email_log = email_log - latest_contact = contact - - alias_info.latest_contact = latest_contact - alias_info.latest_email_log = latest_email_log - - return alias_info