From d2c13763a2e677095a68dad4d9a93f96dc7cd63c Mon Sep 17 00:00:00 2001 From: Son NK Date: Tue, 17 Mar 2020 11:10:50 +0100 Subject: [PATCH] rename ForwardEmailLog to EmailLog --- app/api/views/alias.py | 4 +-- app/dashboard/views/alias_log.py | 20 +++++++-------- app/dashboard/views/index.py | 6 ++--- app/dashboard/views/refused_email.py | 6 ++--- app/models.py | 8 +++--- cron.py | 6 ++--- email_handler.py | 10 ++++---- .../versions/2020_031711_6e061eb84167_.py | 25 +++++++++++++++++++ tests/api/test_alias.py | 8 +++--- 9 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 migrations/versions/2020_031711_6e061eb84167_.py diff --git a/app/api/views/alias.py b/app/api/views/alias.py index f4f127b0..12b479c5 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -12,7 +12,7 @@ from app.dashboard.views.alias_log import get_alias_log from app.dashboard.views.index import get_alias_info, AliasInfo from app.extensions import db from app.log import LOG -from app.models import ForwardEmailLog +from app.models import EmailLog from app.models import GenEmail, Contact from app.utils import random_string @@ -210,7 +210,7 @@ def serialize_contact(fe: Contact) -> dict: "reverse_alias": fe.website_send_to(), } - fel: ForwardEmailLog = fe.last_reply() + fel: EmailLog = fe.last_reply() if fel: res["last_email_sent_date"] = fel.created_at.format() res["last_email_sent_timestamp"] = fel.created_at.timestamp diff --git a/app/dashboard/views/alias_log.py b/app/dashboard/views/alias_log.py index 19cc1b72..27856b4b 100644 --- a/app/dashboard/views/alias_log.py +++ b/app/dashboard/views/alias_log.py @@ -5,7 +5,7 @@ from flask_login import login_required, current_user from app.config import PAGE_LIMIT from app.dashboard.base import dashboard_bp from app.extensions import db -from app.models import GenEmail, ForwardEmailLog, Contact +from app.models import GenEmail, EmailLog, Contact class AliasLog: @@ -42,18 +42,18 @@ def alias_log(alias_id, page_id): logs = get_alias_log(gen_email, page_id) base = ( - db.session.query(Contact, ForwardEmailLog) - .filter(Contact.id == ForwardEmailLog.contact_id) + db.session.query(Contact, EmailLog) + .filter(Contact.id == EmailLog.contact_id) .filter(Contact.gen_email_id == gen_email.id) ) total = base.count() email_forwarded = ( - base.filter(ForwardEmailLog.is_reply == False) - .filter(ForwardEmailLog.blocked == False) + base.filter(EmailLog.is_reply == False) + .filter(EmailLog.blocked == False) .count() ) - email_replied = base.filter(ForwardEmailLog.is_reply == True).count() - email_blocked = base.filter(ForwardEmailLog.blocked == True).count() + email_replied = base.filter(EmailLog.is_reply == True).count() + email_blocked = base.filter(EmailLog.blocked == True).count() last_page = ( len(logs) < PAGE_LIMIT ) # lightweight pagination without counting all objects @@ -66,10 +66,10 @@ def get_alias_log(gen_email: GenEmail, page_id=0): mailbox = gen_email.mailbox_email() q = ( - db.session.query(Contact, ForwardEmailLog) - .filter(Contact.id == ForwardEmailLog.contact_id) + db.session.query(Contact, EmailLog) + .filter(Contact.id == EmailLog.contact_id) .filter(Contact.gen_email_id == gen_email.id) - .order_by(ForwardEmailLog.id.desc()) + .order_by(EmailLog.id.desc()) .limit(PAGE_LIMIT) .offset(page_id * PAGE_LIMIT) ) diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index 936b1be8..96ae6c49 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -13,7 +13,7 @@ from app.models import ( GenEmail, ClientUser, Contact, - ForwardEmailLog, + EmailLog, DeletedAlias, AliasGeneratorEnum, Mailbox, @@ -202,9 +202,9 @@ def get_alias_info( aliases = {} # dict of alias and AliasInfo q = ( - db.session.query(GenEmail, Contact, ForwardEmailLog, Mailbox) + db.session.query(GenEmail, Contact, EmailLog, Mailbox) .join(Contact, GenEmail.id == Contact.gen_email_id, isouter=True) - .join(ForwardEmailLog, Contact.id == ForwardEmailLog.contact_id, isouter=True) + .join(EmailLog, Contact.id == EmailLog.contact_id, isouter=True) .join(Mailbox, GenEmail.mailbox_id == Mailbox.id, isouter=True) .filter(GenEmail.user_id == user.id) .order_by(GenEmail.created_at.desc()) diff --git a/app/dashboard/views/refused_email.py b/app/dashboard/views/refused_email.py index 4b5e84db..43d56340 100644 --- a/app/dashboard/views/refused_email.py +++ b/app/dashboard/views/refused_email.py @@ -2,7 +2,7 @@ from flask import render_template, request from flask_login import login_required from app.dashboard.base import dashboard_bp -from app.models import ForwardEmailLog +from app.models import EmailLog @dashboard_bp.route("/refused_email", methods=["GET", "POST"]) @@ -13,9 +13,7 @@ def refused_email_route(): if highlight_fel_id: highlight_fel_id = int(highlight_fel_id) - fels: [ForwardEmailLog] = ForwardEmailLog.query.filter( - ForwardEmailLog.refused_email_id != None - ).all() + fels: [EmailLog] = EmailLog.query.filter(EmailLog.refused_email_id != None).all() # make sure the highlighted fel is the first fel highlight_index = None diff --git a/app/models.py b/app/models.py index d2204158..8646bdda 100644 --- a/app/models.py +++ b/app/models.py @@ -739,16 +739,16 @@ class Contact(db.Model, ModelMixin): # cannot use formataddr here as this field is for email client, not for MTA # return formataddr((self.website_email.replace("@", " at "), self.reply_email)) - def last_reply(self) -> "ForwardEmailLog": + def last_reply(self) -> "EmailLog": """return the most recent reply""" return ( - ForwardEmailLog.query.filter_by(contact_id=self.id, is_reply=True) - .order_by(desc(ForwardEmailLog.created_at)) + EmailLog.query.filter_by(contact_id=self.id, is_reply=True) + .order_by(desc(EmailLog.created_at)) .first() ) -class ForwardEmailLog(db.Model, ModelMixin): +class EmailLog(db.Model, ModelMixin): contact_id = db.Column( db.ForeignKey(Contact.id, ondelete="cascade"), nullable=False ) diff --git a/cron.py b/cron.py index b0fa6569..d625fae3 100644 --- a/cron.py +++ b/cron.py @@ -11,7 +11,7 @@ from app.models import ( Subscription, User, GenEmail, - ForwardEmailLog, + EmailLog, Contact, CustomDomain, Client, @@ -119,8 +119,8 @@ def stats(): LOG.d("total number alias %s", nb_gen_email) # nb mails forwarded - q = db.session.query(ForwardEmailLog, Contact, GenEmail, User).filter( - ForwardEmailLog.contact_id == Contact.id, + q = db.session.query(EmailLog, Contact, GenEmail, User).filter( + EmailLog.contact_id == Contact.id, Contact.gen_email_id == GenEmail.id, GenEmail.user_id == User.id, ) diff --git a/email_handler.py b/email_handler.py index 8b5a7aa4..f2aa87cb 100644 --- a/email_handler.py +++ b/email_handler.py @@ -71,7 +71,7 @@ from app.log import LOG from app.models import ( GenEmail, Contact, - ForwardEmailLog, + EmailLog, CustomDomain, Directory, User, @@ -314,7 +314,7 @@ def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str: msg = prepare_pgp_message(msg, mailbox.pgp_finger_print) contact = get_or_create_contact(msg["From"], gen_email) - forward_log = ForwardEmailLog.create(contact_id=contact.id) + forward_log = EmailLog.create(contact_id=contact.id) if gen_email.enabled: # add custom header @@ -516,17 +516,17 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str: envelope.rcpt_options, ) - ForwardEmailLog.create(contact_id=contact.id, is_reply=True) + EmailLog.create(contact_id=contact.id, is_reply=True) db.session.commit() return "250 Message accepted for delivery" def handle_bounce(alias, envelope, contact, gen_email, msg, smtp, user, mailbox_email): - fel: ForwardEmailLog = ForwardEmailLog.create(contact_id=contact.id, bounced=True) + fel: EmailLog = EmailLog.create(contact_id=contact.id, bounced=True) db.session.commit() - nb_bounced = ForwardEmailLog.filter_by(contact_id=contact.id, bounced=True).count() + nb_bounced = EmailLog.filter_by(contact_id=contact.id, bounced=True).count() disable_alias_link = f"{URL}/dashboard/unsubscribe/{gen_email.id}" # Store the bounced email diff --git a/migrations/versions/2020_031711_6e061eb84167_.py b/migrations/versions/2020_031711_6e061eb84167_.py new file mode 100644 index 00000000..60aba418 --- /dev/null +++ b/migrations/versions/2020_031711_6e061eb84167_.py @@ -0,0 +1,25 @@ +"""empty message + +Revision ID: 6e061eb84167 +Revises: 14167121af69 +Create Date: 2020-03-17 11:08:02.004125 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "6e061eb84167" +down_revision = "14167121af69" +branch_labels = None +depends_on = None + + +def upgrade(): + op.rename_table("forward_email_log", "email_log") + + +def downgrade(): + op.rename_table("email_log", "forward_email_log") diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index 1670a303..57282458 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -4,7 +4,7 @@ from flask import url_for from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN, PAGE_LIMIT from app.extensions import db -from app.models import User, ApiKey, GenEmail, Contact, ForwardEmailLog +from app.models import User, ApiKey, GenEmail, Contact, EmailLog from app.utils import random_word @@ -136,10 +136,10 @@ def test_alias_activities(flask_client): db.session.commit() for _ in range(int(PAGE_LIMIT / 2)): - ForwardEmailLog.create(contact_id=contact.id, is_reply=True) + EmailLog.create(contact_id=contact.id, is_reply=True) for _ in range(int(PAGE_LIMIT / 2) + 2): - ForwardEmailLog.create(contact_id=contact.id, blocked=True) + EmailLog.create(contact_id=contact.id, blocked=True) r = flask_client.get( url_for("api.get_alias_activities", alias_id=gen_email.id, page_id=0), @@ -207,7 +207,7 @@ def test_alias_contacts(flask_client): ) db.session.commit() - ForwardEmailLog.create(contact_id=contact.id, is_reply=True) + EmailLog.create(contact_id=contact.id, is_reply=True) db.session.commit() r = flask_client.get(