Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domains: put a limit of 2 custom domains per project #11629

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions readthedocs/domains/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from readthedocs.subscriptions.constants import TYPE_CNAME
from readthedocs.subscriptions.products import get_feature


def check_domains_limit(project, error_class=ValidationError):
"""Check if the project has reached the limit on the number of domains."""
feature = get_feature(project, TYPE_CNAME)
if feature.unlimited:
return

if project.domains.count() >= feature.value:
msg = _(
f"This project has reached the limit of {feature.value} domains."
" Consider removing unused domains."
)
if settings.ALLOW_PRIVATE_REPOS:
msg = _(
f"Your organization has reached the limit of {feature.value} domains."
" Consider removing unused domains or upgrading your plan."
)
raise error_class(msg)
4 changes: 4 additions & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from readthedocs.core.utils import extract_valid_attributes_for_model, slugify
from readthedocs.core.utils.url import unsafe_join_url_path
from readthedocs.domains.querysets import DomainQueryset
from readthedocs.domains.validators import check_domains_limit
from readthedocs.notifications.models import Notification as NewNotification
from readthedocs.projects import constants
from readthedocs.projects.exceptions import ProjectConfigurationError
Expand Down Expand Up @@ -1809,6 +1810,9 @@ def restart_validation_process(self):
self.validation_process_start = timezone.now()
self.save()

def clean(self):
check_domains_limit(self.project)

def save(self, *args, **kwargs):
parsed = urlparse(self.domain)
if parsed.scheme or parsed.netloc:
Expand Down
52 changes: 52 additions & 0 deletions readthedocs/rtd_tests/tests/test_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from readthedocs.projects.forms import DomainForm
from readthedocs.projects.models import Domain, Project
from readthedocs.subscriptions.constants import TYPE_CNAME
from readthedocs.subscriptions.products import get_feature


class ModelTests(TestCase):
Expand Down Expand Up @@ -205,3 +207,53 @@ def test_dont_allow_changin_https_to_http(self):
self.assertTrue(form.is_valid())
domain = form.save()
self.assertTrue(domain.https)

@override_settings(
RTD_DEFAULT_FEATURES=dict(
[RTDProductFeature(type=TYPE_CNAME, value=2).to_item()]
),
)
def test_domains_limit(self):
feature = get_feature(self.project, TYPE_CNAME)
form = DomainForm(
{
"domain": "docs.user.example.com",
"canonical": True,
},
project=self.project,
)
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(self.project.domains.all().count(), 1)

form = DomainForm(
{
"domain": "docs.dev.example.com",
"canonical": False,
},
project=self.project,
)
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(self.project.domains.all().count(), 2)

# Creating the third (3) domain should fail the validation form
form = DomainForm(
{
"domain": "docs.customer.example.com",
"canonical": False,
},
project=self.project,
)
self.assertFalse(form.is_valid())

msg = (
f"This project has reached the limit of {feature.value} domains. "
"Consider removing unused domains."
)
if settings.RTD_ALLOW_ORGANIZATIONS:
msg = (
f"Your organization has reached the limit of {feature.value} domains. "
"Consider removing unused domains or upgrading your plan."
)
self.assertEqual(form.errors["__all__"][0], msg)
3 changes: 2 additions & 1 deletion readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def RTD_DEFAULT_FEATURES(self):

return dict(
(
RTDProductFeature(type=constants.TYPE_CNAME).to_item(),
# Max number of domains allowed per project.
RTDProductFeature(type=constants.TYPE_CNAME, value=2).to_item(),
RTDProductFeature(type=constants.TYPE_EMBED_API).to_item(),
# Retention days for search analytics.
RTDProductFeature(
Expand Down