From 75d1b090cd97e9c0a760e447ed696fd544d08c81 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Thu, 31 Dec 2020 14:14:56 +0100 Subject: [PATCH] rename default_random_alias_public_domain_id -> default_alias_public_domain_id --- app/api/views/setting.py | 7 ++--- app/dashboard/views/custom_alias.py | 4 +-- app/dashboard/views/setting.py | 9 +++---- app/models.py | 16 +++++------- ..._rename_default_alias_public_domain_id_.py | 26 +++++++++++++++++++ tests/dashboard/test_custom_alias.py | 8 +++--- 6 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 migrations/versions/2020_123114_rename_default_alias_public_domain_id_.py diff --git a/app/api/views/setting.py b/app/api/views/setting.py index ea7d4405..a3515835 100644 --- a/app/api/views/setting.py +++ b/app/api/views/setting.py @@ -76,8 +76,7 @@ def update_setting(): if sl_domain.premium_only and not user.is_premium(): return jsonify(error="You cannot use this domain"), 400 - # make sure only default_random_alias_domain_id or default_random_alias_public_domain_id is set - user.default_random_alias_public_domain_id = sl_domain.id + user.default_alias_public_domain_id = sl_domain.id user.default_alias_custom_domain_id = None else: custom_domain = CustomDomain.get_by(domain=default_domain) @@ -89,10 +88,8 @@ def update_setting(): LOG.exception("%s cannot use domain %s", user, default_domain) return jsonify(error="invalid domain"), 400 else: - # make sure only default_random_alias_domain_id or - # default_random_alias_public_domain_id is set user.default_alias_custom_domain_id = custom_domain.id - user.default_random_alias_public_domain_id = None + user.default_alias_public_domain_id = None db.session.commit() return jsonify(setting_to_dict(user)) diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index 328390aa..578cf0c5 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -58,7 +58,7 @@ def available_suffixes(user: User) -> [bool, str, str]: suffix_info = (False, suffix, signer.sign(suffix).decode()) # put the default domain to top - if user.default_random_alias_public_domain_id == domain.id: + if user.default_alias_public_domain_id == domain.id: suffixes.insert(0, suffix_info) else: suffixes.append(suffix_info) @@ -113,7 +113,7 @@ def available_suffixes_more_info(user: User) -> [SuffixInfo]: False, suffix, signer.sign(suffix).decode(), sl_domain.premium_only ) # put the default domain to top - if user.default_random_alias_public_domain_id == sl_domain.id: + if user.default_alias_public_domain_id == sl_domain.id: suffixes.insert(0, suffix_info) else: suffixes.append(suffix_info) diff --git a/app/dashboard/views/setting.py b/app/dashboard/views/setting.py index 578419ed..9560c4ef 100644 --- a/app/dashboard/views/setting.py +++ b/app/dashboard/views/setting.py @@ -207,8 +207,7 @@ def setting(): flash("You cannot use this domain", "error") return redirect(url_for("dashboard.setting")) - # make sure only default_random_alias_domain_id or default_random_alias_public_domain_id is set - current_user.default_random_alias_public_domain_id = sl_domain.id + current_user.default_alias_public_domain_id = sl_domain.id current_user.default_alias_custom_domain_id = None else: custom_domain = CustomDomain.get_by(domain=default_domain) @@ -222,16 +221,14 @@ def setting(): "%s cannot use domain %s", current_user, default_domain ) else: - # make sure only default_random_alias_domain_id or - # default_random_alias_public_domain_id is set current_user.default_alias_custom_domain_id = ( custom_domain.id ) - current_user.default_random_alias_public_domain_id = None + current_user.default_alias_public_domain_id = None else: current_user.default_alias_custom_domain_id = None - current_user.default_random_alias_public_domain_id = None + current_user.default_alias_public_domain_id = None db.session.commit() flash("Your preference has been updated", "success") diff --git a/app/models.py b/app/models.py index ba707f6c..b94cb757 100644 --- a/app/models.py +++ b/app/models.py @@ -204,14 +204,14 @@ class User(db.Model, ModelMixin, UserMixin): fido_uuid = db.Column(db.String(), nullable=True, unique=True) # the default domain that's used when user creates a new random alias - # default_random_alias_domain_id XOR default_random_alias_public_domain_id + # default_alias_custom_domain_id XOR default_alias_public_domain_id default_alias_custom_domain_id = db.Column( db.ForeignKey("custom_domain.id", ondelete="SET NULL"), nullable=True, default=None, ) - default_random_alias_public_domain_id = db.Column( + default_alias_public_domain_id = db.Column( db.ForeignKey("public_domain.id", ondelete="SET NULL"), nullable=True, default=None, @@ -559,8 +559,8 @@ class User(db.Model, ModelMixin, UserMixin): return custom_domain.domain - if self.default_random_alias_public_domain_id: - sl_domain = SLDomain.get(self.default_random_alias_public_domain_id) + if self.default_alias_public_domain_id: + sl_domain = SLDomain.get(self.default_alias_public_domain_id) # sanity check if not sl_domain: LOG.exception("Problem with %s public random alias domain", self) @@ -573,7 +573,7 @@ class User(db.Model, ModelMixin, UserMixin): sl_domain, ) self.default_alias_custom_domain_id = None - self.default_random_alias_public_domain_id = None + self.default_alias_public_domain_id = None db.session.commit() return FIRST_ALIAS_DOMAIN @@ -1050,10 +1050,8 @@ class Alias(db.Model, ModelMixin): random_email = generate_email( scheme=scheme, in_hex=in_hex, alias_domain=custom_domain.domain ) - elif user.default_random_alias_public_domain_id: - sl_domain: SLDomain = SLDomain.get( - user.default_random_alias_public_domain_id - ) + elif user.default_alias_public_domain_id: + sl_domain: SLDomain = SLDomain.get(user.default_alias_public_domain_id) if sl_domain.premium_only and not user.is_premium(): LOG.warning("%s not premium, cannot use %s", user, sl_domain) else: diff --git a/migrations/versions/2020_123114_rename_default_alias_public_domain_id_.py b/migrations/versions/2020_123114_rename_default_alias_public_domain_id_.py new file mode 100644 index 00000000..430bfbbc --- /dev/null +++ b/migrations/versions/2020_123114_rename_default_alias_public_domain_id_.py @@ -0,0 +1,26 @@ +"""empty message + +Revision ID: rename_default_alias_public_domain_id +Revises: f66ca777f409 +Create Date: 2020-12-31 14:11:45.429299 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'rename_default_alias_public_domain_id' +down_revision = 'f66ca777f409' +branch_labels = None +depends_on = None + + +def upgrade(): + op.alter_column('users', 'default_random_alias_public_domain_id', new_column_name='default_alias_public_domain_id') + + + +def downgrade(): + op.alter_column('users', 'default_alias_public_domain_id', new_column_name='default_random_alias_public_domain_id') diff --git a/tests/dashboard/test_custom_alias.py b/tests/dashboard/test_custom_alias.py index d5ae63df..826cb9fa 100644 --- a/tests/dashboard/test_custom_alias.py +++ b/tests/dashboard/test_custom_alias.py @@ -121,17 +121,15 @@ def test_available_suffixes_default_domain(flask_client): user = login(flask_client) sl_domain = SLDomain.query.first() - CustomDomain.create( - user_id=user.id, domain="test.com", verified=True, commit=True - ) + CustomDomain.create(user_id=user.id, domain="test.com", verified=True, commit=True) - user.default_random_alias_public_domain_id = sl_domain.id + user.default_alias_public_domain_id = sl_domain.id # first suffix is SL Domain first_suffix = available_suffixes(user)[0] assert first_suffix[1].endswith(f"@{sl_domain.domain}") - user.default_random_alias_public_domain_id = None + user.default_alias_public_domain_id = None # first suffix is custom domain first_suffix = available_suffixes(user)[0] assert first_suffix[1] == "@test.com"