From 91e38a744be7919145ce41d463fbf6c9fc2fbae3 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 22 Dec 2019 16:32:55 +0000 Subject: [PATCH] remove GenEmail.custom column --- app/api/views/new_custom_alias.py | 2 +- app/config.py | 2 +- app/dashboard/views/custom_alias.py | 3 +-- app/models.py | 10 ++-------- app/oauth/views/authorize.py | 4 +--- migrations/versions/18e934d58f55_.py | 29 ++++++++++++++++++++++++++++ server.py | 16 +++++++-------- 7 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 migrations/versions/18e934d58f55_.py diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py index b2ce5387..de23bdd4 100644 --- a/app/api/views/new_custom_alias.py +++ b/app/api/views/new_custom_alias.py @@ -74,7 +74,7 @@ def new_custom_alias(): LOG.d("full alias already used %s", full_alias) return jsonify(error=f"alias {full_alias} already exists"), 409 - gen_email = GenEmail.create(user_id=user.id, email=full_alias, custom=True) + gen_email = GenEmail.create(user_id=user.id, email=full_alias) db.session.commit() if hostname: diff --git a/app/config.py b/app/config.py index e1684eaf..18e2d3af 100644 --- a/app/config.py +++ b/app/config.py @@ -63,7 +63,7 @@ DKIM_SELECTOR = b"dkim" with open(DKIM_PRIVATE_KEY_PATH) as f: DKIM_PRIVATE_KEY = f.read() -DKIM_HEADERS = [b'from', b'to', b'subject'] +DKIM_HEADERS = [b"from", b"to", b"subject"] # Database DB_URI = os.environ["DB_URI"] diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index 90d34cbc..034746a1 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -42,7 +42,7 @@ def custom_alias(): "create custom alias %s for user %s", full_email, current_user ) gen_email = GenEmail.create( - email=full_email, user_id=current_user.id, custom=True + email=full_email, user_id=current_user.id ) db.session.commit() @@ -79,7 +79,6 @@ def custom_alias(): gen_email = GenEmail.create( email=full_email, user_id=current_user.id, - custom=True, custom_domain_id=custom_domain.id, ) db.session.commit() diff --git a/app/models.py b/app/models.py index 7dc438d7..1f506a38 100644 --- a/app/models.py +++ b/app/models.py @@ -134,10 +134,7 @@ class User(db.Model, ModelMixin, UserMixin): if self.is_premium(): return True - return ( - GenEmail.filter_by(user_id=self.id, custom=True).count() - < MAX_NB_EMAIL_FREE_PLAN - ) + return GenEmail.filter_by(user_id=self.id).count() < MAX_NB_EMAIL_FREE_PLAN def set_password(self, password): salt = bcrypt.gensalt() @@ -389,9 +386,6 @@ class GenEmail(db.Model, ModelMixin): enabled = db.Column(db.Boolean(), default=True, nullable=False) - # this email has been customized by user, i.e. not generated randomly - custom = db.Column(db.Boolean(), default=False, nullable=False, server_default="0") - custom_domain_id = db.Column( db.ForeignKey("custom_domain.id", ondelete="cascade"), nullable=True ) @@ -411,7 +405,7 @@ class GenEmail(db.Model, ModelMixin): if not cls.get_by(email=email): break - return GenEmail.create(user_id=user_id, email=email, custom=True) + return GenEmail.create(user_id=user_id, email=email) def __repr__(self): return f"" diff --git a/app/oauth/views/authorize.py b/app/oauth/views/authorize.py index 4adde860..7ba3a49a 100644 --- a/app/oauth/views/authorize.py +++ b/app/oauth/views/authorize.py @@ -163,9 +163,7 @@ def authorize(): flash(f"alias {email} already used", "error") return redirect(request.url) - gen_email = GenEmail.create( - email=email, user_id=current_user.id, custom=True - ) + gen_email = GenEmail.create(email=email, user_id=current_user.id) db.session.flush() else: # user picks an email from suggestion if chosen_email != current_user.email: diff --git a/migrations/versions/18e934d58f55_.py b/migrations/versions/18e934d58f55_.py new file mode 100644 index 00000000..0ff96645 --- /dev/null +++ b/migrations/versions/18e934d58f55_.py @@ -0,0 +1,29 @@ +"""empty message + +Revision ID: 18e934d58f55 +Revises: 0c7f1a48aac9 +Create Date: 2019-12-22 16:31:33.531138 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '18e934d58f55' +down_revision = '0c7f1a48aac9' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('gen_email', 'custom') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('gen_email', sa.Column('custom', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False)) + # ### end Alembic commands ### diff --git a/server.py b/server.py index c908acb3..b2bae7af 100644 --- a/server.py +++ b/server.py @@ -38,16 +38,14 @@ from app.models import ( Subscription, PlanEnum, ApiKey, - CustomDomain) + CustomDomain, +) from app.monitor.base import monitor_bp from app.oauth.base import oauth_bp if SENTRY_DSN: LOG.d("enable sentry") - sentry_sdk.init( - dsn=SENTRY_DSN, - integrations=[FlaskIntegration()], - ) + sentry_sdk.init(dsn=SENTRY_DSN, integrations=[FlaskIntegration()]) # the app is served behin nginx which uses http and not https os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" @@ -122,7 +120,9 @@ def fake_data(): GenEmail.create_custom_alias(user.id, "e3@") CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True) - CustomDomain.create(user_id=user.id, domain="very-long-domain.com.net.org", verified=True) + CustomDomain.create( + user_id=user.id, domain="very-long-domain.com.net.org", verified=True + ) db.session.commit() # Create a client @@ -263,9 +263,7 @@ def jinja2_filter(app): @app.context_processor def inject_stage_and_region(): - return dict( - YEAR=arrow.now().year, URL=URL, SENTRY_DSN=SENTRY_DSN, VERSION=SHA1 - ) + return dict(YEAR=arrow.now().year, URL=URL, SENTRY_DSN=SENTRY_DSN, VERSION=SHA1) def setup_paddle_callback(app: Flask):