diff --git a/app/models.py b/app/models.py index 9d6328dc..f4b26af8 100644 --- a/app/models.py +++ b/app/models.py @@ -224,6 +224,10 @@ def _expiration_1h(): return arrow.now().shift(hours=1) +def _expiration_12h(): + return arrow.now().shift(hours=12) + + def _expiration_5m(): return arrow.now().shift(minutes=5) @@ -563,3 +567,20 @@ class DeletedAlias(db.Model, ModelMixin): email = db.Column(db.String(128), unique=True, nullable=False) +class EmailChange(db.Model, ModelMixin): + """Used when user wants to update their email""" + + user_id = db.Column( + db.ForeignKey(User.id, ondelete="cascade"), + nullable=False, + unique=True, + index=True, + ) + new_email = db.Column(db.String(128), unique=True, nullable=False) + code = db.Column(db.String(128), unique=True, nullable=False) + expired = db.Column(ArrowType, nullable=False, default=_expiration_12h) + + user = db.relationship(User) + + def is_expired(self): + return self.expired < arrow.now() diff --git a/migrations/versions/3ebfbaeb76c0_.py b/migrations/versions/3ebfbaeb76c0_.py new file mode 100644 index 00000000..c5c274a5 --- /dev/null +++ b/migrations/versions/3ebfbaeb76c0_.py @@ -0,0 +1,43 @@ +"""empty message + +Revision ID: 3ebfbaeb76c0 +Revises: 0a89c670fc7a +Create Date: 2019-11-18 19:29:43.277973 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '3ebfbaeb76c0' +down_revision = '0a89c670fc7a' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('email_change', + 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('new_email', sa.String(length=128), nullable=False), + sa.Column('code', sa.String(length=128), nullable=False), + sa.Column('expired', sqlalchemy_utils.types.arrow.ArrowType(), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='cascade'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('code'), + sa.UniqueConstraint('new_email') + ) + op.create_index(op.f('ix_email_change_user_id'), 'email_change', ['user_id'], unique=True) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_email_change_user_id'), table_name='email_change') + op.drop_table('email_change') + # ### end Alembic commands ###