mirror of
https://github.com/simple-login/app.git
synced 2024-11-18 06:31:27 +08:00
refactoring: use get_alias_infos_with_pagination_v2
This commit is contained in:
parent
72f3e47c3c
commit
95b71435f9
1 changed files with 2 additions and 70 deletions
|
@ -8,6 +8,7 @@ from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
|
|
||||||
from app import email_utils
|
from app import email_utils
|
||||||
|
from app.api.serializer import get_alias_infos_with_pagination_v2
|
||||||
from app.config import PAGE_LIMIT
|
from app.config import PAGE_LIMIT
|
||||||
from app.dashboard.base import dashboard_bp
|
from app.dashboard.base import dashboard_bp
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
|
@ -188,7 +189,7 @@ def index():
|
||||||
return render_template(
|
return render_template(
|
||||||
"dashboard/index.html",
|
"dashboard/index.html",
|
||||||
client_users=client_users,
|
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,
|
highlight_alias_id=highlight_alias_id,
|
||||||
query=query,
|
query=query,
|
||||||
AliasGeneratorEnum=AliasGeneratorEnum,
|
AliasGeneratorEnum=AliasGeneratorEnum,
|
||||||
|
@ -196,72 +197,3 @@ def index():
|
||||||
show_intro=show_intro,
|
show_intro=show_intro,
|
||||||
page=page,
|
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
|
|
||||||
|
|
Loading…
Reference in a new issue