浏览代码

Add Mailbox model, GenEmail.mailbox_id column

Son NK 5 年之前
父节点
当前提交
eca2422be4
共有 2 个文件被更改,包括 66 次插入1 次删除
  1. 23 1
      app/models.py
  2. 43 0
      migrations/versions/2020_021023_6664d75ce3d4_.py

+ 23 - 1
app/models.py

@@ -17,7 +17,7 @@ from app.config import (
     AVATAR_URL_EXPIRATION,
     JOB_ONBOARDING_1,
 )
-from app.email_utils import get_email_name
+
 from app.extensions import db
 from app.log import LOG
 from app.oauth_models import Scope
@@ -478,7 +478,13 @@ class GenEmail(db.Model, ModelMixin):
 
     note = db.Column(db.Text, default=None, nullable=True)
 
+    # an alias can be owned by another mailbox
+    mailbox_id = db.Column(
+        db.ForeignKey("mailbox.id", ondelete="cascade"), nullable=True, default=None
+    )
+
     user = db.relationship(User)
+    mailbox = db.relationship('Mailbox')
 
     @classmethod
     def create_new(cls, user_id, prefix, note=None):
@@ -626,6 +632,8 @@ class ForwardEmail(db.Model, ModelMixin):
     def website_send_to(self):
         """return the email address with name.
         to use when user wants to send an email from the alias"""
+        from app.email_utils import get_email_name
+
         if self.website_from:
             name = get_email_name(self.website_from)
             if name:
@@ -799,3 +807,17 @@ class Job(db.Model, ModelMixin):
 
     def __repr__(self):
         return f"<Job {self.id} {self.name} {self.payload}>"
+
+
+class Mailbox(db.Model, ModelMixin):
+    user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False)
+    email = db.Column(db.String(256), unique=True, nullable=False)
+    verified = db.Column(db.Boolean, default=False, nullable=False)
+
+    user = db.relationship(User)
+
+    def nb_alias(self):
+        return GenEmail.filter_by(mailbox_id=self.id).count()
+
+    def __repr__(self):
+        return f"<Mailbox {self.email}>"

+ 43 - 0
migrations/versions/2020_021023_6664d75ce3d4_.py

@@ -0,0 +1,43 @@
+"""empty message
+
+Revision ID: 6664d75ce3d4
+Revises: b9f849432543
+Create Date: 2020-02-10 23:10:09.134369
+
+"""
+import sqlalchemy_utils
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '6664d75ce3d4'
+down_revision = 'b9f849432543'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_table('mailbox',
+    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
+    sa.Column('created_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=False),
+    sa.Column('updated_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=False),
+    sa.Column('email', sa.String(length=256), nullable=False),
+    sa.Column('verified', sa.Boolean(), nullable=False),
+    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='cascade'),
+    sa.PrimaryKeyConstraint('id'),
+    sa.UniqueConstraint('email')
+    )
+    op.add_column('gen_email', sa.Column('mailbox_id', sa.Integer(), nullable=True))
+    op.create_foreign_key(None, 'gen_email', 'mailbox', ['mailbox_id'], ['id'], ondelete='cascade')
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_constraint(None, 'gen_email', type_='foreignkey')
+    op.drop_column('gen_email', 'mailbox_id')
+    op.drop_table('mailbox')
+    # ### end Alembic commands ###