allow to hide some public domains and set their order (#1107)

This commit is contained in:
Son Nguyen Kim 2022-06-22 18:21:19 +02:00 committed by GitHub
parent db6ec2dbe6
commit 09cec0cdec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 2 deletions

View File

@ -916,10 +916,12 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
return [sl_domain.domain for sl_domain in self.get_sl_domains()]
def get_sl_domains(self) -> List["SLDomain"]:
query = SLDomain.filter_by(hidden=False).order_by(SLDomain.order)
if self.is_premium():
return SLDomain.all()
return query.all()
else:
return SLDomain.filter_by(premium_only=False).all()
return query.filter_by(premium_only=False).all()
def available_alias_domains(self) -> [str]:
"""return all domains that user can use when creating a new alias, including:
@ -2704,6 +2706,12 @@ class SLDomain(Base, ModelMixin):
sa.Boolean, nullable=False, default=False, server_default="0"
)
# if enabled, do not show this domain when user creates a custom alias
hidden = sa.Column(sa.Boolean, nullable=False, default=False, server_default="0")
# the order in which the domains are shown when user creates a custom alias
order = sa.Column(sa.Integer, nullable=False, default=0, server_default="0")
def __repr__(self):
return f"<SLDomain {self.domain} {'Premium' if self.premium_only else 'Free'}"

View File

@ -0,0 +1,31 @@
"""empty message
Revision ID: 673a074e4215
Revises: a7bcb872c12a
Create Date: 2022-06-22 17:17:24.383701
"""
import sqlalchemy_utils
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '673a074e4215'
down_revision = 'a7bcb872c12a'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('public_domain', sa.Column('hidden', sa.Boolean(), server_default='0', nullable=False))
op.add_column('public_domain', sa.Column('order', sa.Integer(), server_default='0', nullable=False))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('public_domain', 'order')
op.drop_column('public_domain', 'hidden')
# ### end Alembic commands ###

View File

@ -170,6 +170,34 @@ def test_available_suffixes_random_prefix_generation(flask_client):
assert first_suffix.suffix.startswith(".")
def test_available_suffixes_hidden_domain(flask_client):
user = login(flask_client)
nb_suffix = len(get_available_suffixes(user))
sl_domain = SLDomain.create(domain=random_domain(), commit=True)
assert len(get_available_suffixes(user)) == nb_suffix + 1
sl_domain.hidden = True
Session.commit()
assert len(get_available_suffixes(user)) == nb_suffix
def test_available_suffixes_domain_order(flask_client):
user = login(flask_client)
domain = random_domain()
# will be the last domain as other domains have order=0
sl_domain = SLDomain.create(domain=domain, order=1, commit=True)
last_suffix_info = get_available_suffixes(user)[-1]
assert last_suffix_info.suffix.endswith(domain)
# now will be the first domain
sl_domain.order = -1
Session.commit()
first_suffix_info = get_available_suffixes(user)[0]
assert first_suffix_info.suffix.endswith(domain)
def test_add_already_existed_alias(flask_client):
user = login(flask_client)
Session.commit()