rename gen_email_id -> alias_id

This commit is contained in:
Son NK 2020-03-17 12:01:18 +01:00 committed by Son NK
parent 2ad930a141
commit e8b315b431
14 changed files with 85 additions and 33 deletions

View file

@ -220,7 +220,7 @@ def serialize_contact(fe: Contact) -> dict:
def get_alias_contacts(alias, page_id: int) -> [dict]: def get_alias_contacts(alias, page_id: int) -> [dict]:
q = ( q = (
Contact.query.filter_by(gen_email_id=alias.id) Contact.query.filter_by(alias_id=alias.id)
.order_by(Contact.id.desc()) .order_by(Contact.id.desc())
.limit(PAGE_LIMIT) .limit(PAGE_LIMIT)
.offset(page_id * PAGE_LIMIT) .offset(page_id * PAGE_LIMIT)
@ -305,11 +305,11 @@ def create_contact_route(alias_id):
_, website_email = parseaddr(contact_email) _, website_email = parseaddr(contact_email)
# already been added # already been added
if Contact.get_by(gen_email_id=alias.id, website_email=website_email): if Contact.get_by(alias_id=alias.id, website_email=website_email):
return jsonify(error="Contact already added"), 409 return jsonify(error="Contact already added"), 409
contact = Contact.create( contact = Contact.create(
gen_email_id=alias.id, alias_id=alias.id,
website_email=website_email, website_email=website_email,
website_from=contact_email, website_from=contact_email,
reply_email=reply_email, reply_email=reply_email,

View file

@ -41,7 +41,7 @@ def options():
q = ( q = (
db.session.query(AliasUsedOn, Alias, User) db.session.query(AliasUsedOn, Alias, User)
.filter( .filter(
AliasUsedOn.gen_email_id == Alias.id, AliasUsedOn.alias_id == Alias.id,
Alias.user_id == user.id, Alias.user_id == user.id,
AliasUsedOn.hostname == hostname, AliasUsedOn.hostname == hostname,
) )
@ -124,7 +124,7 @@ def options_v2():
q = ( q = (
db.session.query(AliasUsedOn, Alias, User) db.session.query(AliasUsedOn, Alias, User)
.filter( .filter(
AliasUsedOn.gen_email_id == Alias.id, AliasUsedOn.alias_id == Alias.id,
Alias.user_id == user.id, Alias.user_id == user.id,
AliasUsedOn.hostname == hostname, AliasUsedOn.hostname == hostname,
) )

View file

@ -72,7 +72,7 @@ def new_custom_alias():
db.session.commit() db.session.commit()
if hostname: if hostname:
AliasUsedOn.create(gen_email_id=alias.id, hostname=hostname) AliasUsedOn.create(alias_id=alias.id, hostname=hostname)
db.session.commit() db.session.commit()
return jsonify(alias=full_alias), 201 return jsonify(alias=full_alias), 201

View file

@ -52,7 +52,7 @@ def new_random_alias():
hostname = request.args.get("hostname") hostname = request.args.get("hostname")
if hostname: if hostname:
AliasUsedOn.create(gen_email_id=alias.id, hostname=hostname) AliasUsedOn.create(alias_id=alias.id, hostname=hostname)
db.session.commit() db.session.commit()
return jsonify(alias=alias.email), 201 return jsonify(alias=alias.email), 201

View file

@ -80,14 +80,14 @@ def alias_contact_manager(alias_id, contact_id=None):
_, website_email = parseaddr(contact_email) _, website_email = parseaddr(contact_email)
# already been added # already been added
if Contact.get_by(gen_email_id=alias.id, website_email=website_email): if Contact.get_by(alias_id=alias.id, website_email=website_email):
flash(f"{website_email} is already added", "error") flash(f"{website_email} is already added", "error")
return redirect( return redirect(
url_for("dashboard.alias_contact_manager", alias_id=alias_id) url_for("dashboard.alias_contact_manager", alias_id=alias_id)
) )
contact = Contact.create( contact = Contact.create(
gen_email_id=alias.id, alias_id=alias.id,
website_email=website_email, website_email=website_email,
website_from=contact_email, website_from=contact_email,
reply_email=reply_email, reply_email=reply_email,
@ -113,7 +113,7 @@ def alias_contact_manager(alias_id, contact_id=None):
return redirect( return redirect(
url_for("dashboard.alias_contact_manager", alias_id=alias_id) url_for("dashboard.alias_contact_manager", alias_id=alias_id)
) )
elif contact.gen_email_id != alias.id: elif contact.alias_id != alias.id:
flash("You cannot delete reverse-alias", "warning") flash("You cannot delete reverse-alias", "warning")
return redirect( return redirect(
url_for("dashboard.alias_contact_manager", alias_id=alias_id) url_for("dashboard.alias_contact_manager", alias_id=alias_id)

View file

@ -44,7 +44,7 @@ def alias_log(alias_id, page_id):
base = ( base = (
db.session.query(Contact, EmailLog) db.session.query(Contact, EmailLog)
.filter(Contact.id == EmailLog.contact_id) .filter(Contact.id == EmailLog.contact_id)
.filter(Contact.gen_email_id == alias.id) .filter(Contact.alias_id == alias.id)
) )
total = base.count() total = base.count()
email_forwarded = ( email_forwarded = (
@ -68,7 +68,7 @@ def get_alias_log(alias: Alias, page_id=0):
q = ( q = (
db.session.query(Contact, EmailLog) db.session.query(Contact, EmailLog)
.filter(Contact.id == EmailLog.contact_id) .filter(Contact.id == EmailLog.contact_id)
.filter(Contact.gen_email_id == alias.id) .filter(Contact.alias_id == alias.id)
.order_by(EmailLog.id.desc()) .order_by(EmailLog.id.desc())
.limit(PAGE_LIMIT) .limit(PAGE_LIMIT)
.offset(page_id * PAGE_LIMIT) .offset(page_id * PAGE_LIMIT)

View file

@ -194,7 +194,7 @@ def get_alias_info(
q = ( q = (
db.session.query(Alias, Contact, EmailLog, Mailbox) db.session.query(Alias, Contact, EmailLog, Mailbox)
.join(Contact, Alias.id == Contact.gen_email_id, isouter=True) .join(Contact, Alias.id == Contact.alias_id, isouter=True)
.join(EmailLog, Contact.id == EmailLog.contact_id, isouter=True) .join(EmailLog, Contact.id == EmailLog.contact_id, isouter=True)
.join(Mailbox, Alias.mailbox_id == Mailbox.id, isouter=True) .join(Mailbox, Alias.mailbox_id == Mailbox.id, isouter=True)
.filter(Alias.user_id == user.id) .filter(Alias.user_id == user.id)

View file

@ -614,7 +614,7 @@ class ClientUser(db.Model, ModelMixin):
client_id = db.Column(db.ForeignKey(Client.id, ondelete="cascade"), nullable=False) client_id = db.Column(db.ForeignKey(Client.id, ondelete="cascade"), nullable=False)
# Null means client has access to user original email # Null means client has access to user original email
gen_email_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=True) alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=True)
# user can decide to send to client another name # user can decide to send to client another name
name = db.Column( name = db.Column(
@ -632,7 +632,7 @@ class ClientUser(db.Model, ModelMixin):
client = db.relationship(Client) client = db.relationship(Client)
def get_email(self): def get_email(self):
return self.alias.email if self.gen_email_id else self.user.email return self.alias.email if self.alias_id else self.user.email
def get_user_name(self): def get_user_name(self):
if self.name: if self.name:
@ -679,7 +679,7 @@ class ClientUser(db.Model, ModelMixin):
res[Scope.AVATAR_URL.value] = None res[Scope.AVATAR_URL.value] = None
elif scope == Scope.EMAIL: elif scope == Scope.EMAIL:
# Use generated email # Use generated email
if self.gen_email_id: if self.alias_id:
LOG.debug( LOG.debug(
"Use gen email for user %s, client %s", self.user, self.client "Use gen email for user %s, client %s", self.user, self.client
) )
@ -697,12 +697,10 @@ class Contact(db.Model, ModelMixin):
""" """
__table_args__ = ( __table_args__ = (
db.UniqueConstraint("gen_email_id", "website_email", name="uq_forward_email"), db.UniqueConstraint("alias_id", "website_email", name="uq_contact"),
) )
gen_email_id = db.Column( alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False)
db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False
)
# used to be envelope header, should be mail header from instead # used to be envelope header, should be mail header from instead
website_email = db.Column(db.String(512), nullable=False) website_email = db.Column(db.String(512), nullable=False)
@ -843,12 +841,10 @@ class AliasUsedOn(db.Model, ModelMixin):
"""Used to know where an alias is created""" """Used to know where an alias is created"""
__table_args__ = ( __table_args__ = (
db.UniqueConstraint("gen_email_id", "hostname", name="uq_alias_used"), db.UniqueConstraint("alias_id", "hostname", name="uq_alias_used"),
) )
gen_email_id = db.Column( alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False)
db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False
)
hostname = db.Column(db.String(1024), nullable=False) hostname = db.Column(db.String(1024), nullable=False)

View file

@ -224,7 +224,7 @@ def authorize():
client_id=client.id, user_id=current_user.id client_id=client.id, user_id=current_user.id
) )
if alias: if alias:
client_user.gen_email_id = alias.id client_user.alias_id = alias.id
if custom_name: if custom_name:
client_user.name = custom_name client_user.name = custom_name

View file

@ -121,7 +121,7 @@ def stats():
# nb mails forwarded # nb mails forwarded
q = db.session.query(EmailLog, Contact, Alias, User).filter( q = db.session.query(EmailLog, Contact, Alias, User).filter(
EmailLog.contact_id == Contact.id, EmailLog.contact_id == Contact.id,
Contact.gen_email_id == Alias.id, Contact.alias_id == Alias.id,
Alias.user_id == User.id, Alias.user_id == User.id,
) )
for ie in IGNORED_EMAILS: for ie in IGNORED_EMAILS:

View file

@ -212,7 +212,7 @@ def get_or_create_contact(website_from_header: str, alias: Alias) -> Contact:
website_from_header can be the full-form email, i.e. "First Last <email@example.com>" website_from_header can be the full-form email, i.e. "First Last <email@example.com>"
""" """
_, website_email = parseaddr(website_from_header) _, website_email = parseaddr(website_from_header)
contact = Contact.get_by(gen_email_id=alias.id, website_email=website_email) contact = Contact.get_by(alias_id=alias.id, website_email=website_email)
if contact: if contact:
# update the website_from if needed # update the website_from if needed
if contact.website_from != website_from_header: if contact.website_from != website_from_header:
@ -236,7 +236,7 @@ def get_or_create_contact(website_from_header: str, alias: Alias) -> Contact:
reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}" reply_email = f"reply+{random_string(30)}@{EMAIL_DOMAIN}"
contact = Contact.create( contact = Contact.create(
gen_email_id=alias.id, alias_id=alias.id,
website_email=website_email, website_email=website_email,
website_from=website_from_header, website_from=website_from_header,
reply_email=reply_email, reply_email=reply_email,
@ -482,7 +482,7 @@ def handle_reply(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
add_or_replace_header(msg, "To", contact.website_email) add_or_replace_header(msg, "To", contact.website_email)
# add List-Unsubscribe header # add List-Unsubscribe header
unsubscribe_link = f"{URL}/dashboard/unsubscribe/{contact.gen_email_id}" unsubscribe_link = f"{URL}/dashboard/unsubscribe/{contact.alias_id}"
add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>") add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>")
add_or_replace_header(msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click") add_or_replace_header(msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click")

View file

@ -0,0 +1,56 @@
"""empty message
Revision ID: 0809266d08ca
Revises: e9395fe234a4
Create Date: 2020-03-17 11:56:05.392474
"""
import sqlalchemy_utils
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "0809266d08ca"
down_revision = "e9395fe234a4"
branch_labels = None
depends_on = None
def upgrade():
# alias_used_on table
op.alter_column("alias_used_on", "gen_email_id", new_column_name="alias_id")
op.drop_constraint("uq_alias_used", "alias_used_on", type_="unique")
op.create_unique_constraint(
"uq_alias_used", "alias_used_on", ["alias_id", "hostname"]
)
op.drop_constraint(
"alias_used_on_gen_email_id_fkey", "alias_used_on", type_="foreignkey"
)
op.create_foreign_key(
None, "alias_used_on", "alias", ["alias_id"], ["id"], ondelete="cascade"
)
# client_user table
op.alter_column("client_user", "gen_email_id", new_column_name="alias_id")
op.drop_constraint(
"client_user_gen_email_id_fkey", "client_user", type_="foreignkey"
)
op.create_foreign_key(
None, "client_user", "alias", ["alias_id"], ["id"], ondelete="cascade"
)
# contact table
op.alter_column("contact", "gen_email_id", new_column_name="alias_id")
op.create_unique_constraint("uq_contact", "contact", ["alias_id", "website_email"])
op.drop_constraint("uq_forward_email", "contact", type_="unique")
op.drop_constraint("forward_email_gen_email_id_fkey", "contact", type_="foreignkey")
op.create_foreign_key(
None, "contact", "alias", ["alias_id"], ["id"], ondelete="cascade"
)
def downgrade():
# One-way only
# Too complex to downgrade
raise Exception("Cannot downgrade")

View file

@ -131,7 +131,7 @@ def test_alias_activities(flask_client):
contact = Contact.create( contact = Contact.create(
website_email="marketing@example.com", website_email="marketing@example.com",
reply_email="reply@a.b", reply_email="reply@a.b",
gen_email_id=alias.id, alias_id=alias.id,
) )
db.session.commit() db.session.commit()
@ -203,7 +203,7 @@ def test_alias_contacts(flask_client):
contact = Contact.create( contact = Contact.create(
website_email=f"marketing-{i}@example.com", website_email=f"marketing-{i}@example.com",
reply_email=f"reply-{i}@a.b", reply_email=f"reply-{i}@a.b",
gen_email_id=alias.id, alias_id=alias.id,
) )
db.session.commit() db.session.commit()

View file

@ -42,7 +42,7 @@ def test_different_scenarios(flask_client):
# <<< with recommendation >>> # <<< with recommendation >>>
alias = Alias.create_new(user, prefix="test") alias = Alias.create_new(user, prefix="test")
db.session.commit() db.session.commit()
AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com") AliasUsedOn.create(alias_id=alias.id, hostname="www.test.com")
db.session.commit() db.session.commit()
r = flask_client.get( r = flask_client.get(
@ -87,7 +87,7 @@ def test_different_scenarios_v2(flask_client):
# <<< with recommendation >>> # <<< with recommendation >>>
alias = Alias.create_new(user, prefix="test") alias = Alias.create_new(user, prefix="test")
db.session.commit() db.session.commit()
AliasUsedOn.create(gen_email_id=alias.id, hostname="www.test.com") AliasUsedOn.create(alias_id=alias.id, hostname="www.test.com")
db.session.commit() db.session.commit()
r = flask_client.get( r = flask_client.get(