From f15488f96b1538b009a3c41d273ae9edf2ba52f7 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sun, 5 Apr 2020 16:21:13 +0200 Subject: [PATCH] refactor: move get_alias_infos_with_pagination, get_alias_info to alias.py --- app/api/views/alias.py | 56 ++++++++++++++++++++++++++++--- app/api/views/new_custom_alias.py | 3 +- app/api/views/new_random_alias.py | 3 +- app/dashboard/views/index.py | 52 +--------------------------- 4 files changed, 55 insertions(+), 59 deletions(-) diff --git a/app/api/views/alias.py b/app/api/views/alias.py index e2319012..d91643df 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -1,9 +1,9 @@ -from email.utils import parseaddr - from flask import g from flask import jsonify from flask import request from flask_cors import cross_origin +from sqlalchemy import or_ +from sqlalchemy.orm import joinedload from app.api.base import api_bp, verify_api_key from app.config import EMAIL_DOMAIN @@ -11,8 +11,6 @@ from app.config import PAGE_LIMIT from app.dashboard.views.alias_log import get_alias_log from app.dashboard.views.index import ( AliasInfo, - get_alias_infos_with_pagination, - get_alias_info, ) from app.email_utils import parseaddr_unicode from app.extensions import db @@ -384,3 +382,53 @@ def delete_contact(contact_id): db.session.commit() return jsonify(deleted=True), 200 + + +def get_alias_infos_with_pagination(user, page_id=0, query=None) -> [AliasInfo]: + ret = [] + q = ( + db.session.query(Alias) + .options(joinedload(Alias.mailbox)) + .filter(Alias.user_id == user.id) + .order_by(Alias.created_at.desc()) + ) + + if query: + q = q.filter( + or_(Alias.email.ilike(f"%{query}%"), Alias.note.ilike(f"%{query}%")) + ) + + q = q.limit(PAGE_LIMIT).offset(page_id * PAGE_LIMIT) + + for alias 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) + ) + + alias_info = AliasInfo( + id=alias.id, + alias=alias, + mailbox=alias.mailbox, + note=alias.note, + nb_blocked=0, + nb_forward=0, + nb_reply=0, + ) + + for _, el in q: + if el.is_reply: + alias_info.nb_reply += 1 + elif el.blocked: + alias_info.nb_blocked += 1 + else: + alias_info.nb_forward += 1 + + return alias_info \ No newline at end of file diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py index 2f52fb45..3c9bab25 100644 --- a/app/api/views/new_custom_alias.py +++ b/app/api/views/new_custom_alias.py @@ -3,10 +3,9 @@ from flask import jsonify, request from flask_cors import cross_origin from app.api.base import api_bp, verify_api_key -from app.api.views.alias import serialize_alias_info +from app.api.views.alias import serialize_alias_info, get_alias_info from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_DOMAINS from app.dashboard.views.custom_alias import verify_prefix_suffix -from app.dashboard.views.index import get_alias_info from app.extensions import db from app.log import LOG from app.models import Alias, AliasUsedOn, User, CustomDomain diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index a29d1bd1..08231ecc 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -3,9 +3,8 @@ from flask import jsonify, request from flask_cors import cross_origin from app.api.base import api_bp, verify_api_key -from app.api.views.alias import serialize_alias_info +from app.api.views.alias import serialize_alias_info, get_alias_info from app.config import MAX_NB_EMAIL_FREE_PLAN -from app.dashboard.views.index import get_alias_info from app.extensions import db from app.log import LOG from app.models import Alias, AliasUsedOn, AliasGeneratorEnum diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index 4f4f1dcf..117e5e79 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -184,61 +184,11 @@ def index(): ) -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) - ) - - alias_info = AliasInfo( - id=alias.id, - alias=alias, - mailbox=alias.mailbox, - note=alias.note, - nb_blocked=0, - nb_forward=0, - nb_reply=0, - ) - - for _, el in q: - if el.is_reply: - alias_info.nb_reply += 1 - elif el.blocked: - alias_info.nb_blocked += 1 - else: - alias_info.nb_forward += 1 - - return alias_info - - -def get_alias_infos_with_pagination(user, page_id=0, query=None) -> [AliasInfo]: - ret = [] - q = ( - db.session.query(Alias) - .options(joinedload(Alias.mailbox)) - .filter(Alias.user_id == user.id) - .order_by(Alias.created_at.desc()) - ) - - if query: - q = q.filter( - or_(Alias.email.ilike(f"%{query}%"), Alias.note.ilike(f"%{query}%")) - ) - - q = q.limit(PAGE_LIMIT).offset(page_id * PAGE_LIMIT) - - for alias in q: - ret.append(get_alias_info(alias)) - - return ret - - def get_alias_infos(user, query=None, highlight_alias_id=None) -> [AliasInfo]: if query: query = query.strip().lower() - aliases = {} # dict of alias and AliasInfo + aliases = {} # dict of alias email and AliasInfo q = ( db.session.query(Alias, Contact, EmailLog, Mailbox)