Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
demo-orgs: Use new wordlist to create demo organization subdomains.
Lauryn Menard8602a4bcc530
diff --git a/zerver/lib/demo_organizations.py b/zerver/lib/demo_organizations.pyindex 2b7eb633d29..a797adef6db 100644--- a/zerver/lib/demo_organizations.py+++ b/zerver/lib/demo_organizations.py@@ -1,9 +1,22 @@+import os++import orjson+from django.conf import settingsfrom django.utils.translation import gettext as _from zerver.lib.exceptions import JsonableErrorfrom zerver.models.realms import Realm+def get_demo_organization_wordlist() -> dict[str, list[str]]:+ path = os.path.join(settings.DEPLOY_ROOT, "zerver/lib", "demo_organization_words.json")+ with open(path, "rb") as reader:+ return orjson.loads(reader.read())+++DEMO_ORGANIZATION_WORD_LIST = get_demo_organization_wordlist()++def demo_organization_owner_email_exists(realm: Realm) -> bool:human_owner_emails = set(realm.get_human_owner_users().values_list("delivery_email", flat=True))return human_owner_emails != {""}diff --git a/zerver/tests/test_realm_creation.py b/zerver/tests/test_realm_creation.pyindex 1ce5a582486..cee66f9d32f 100644--- a/zerver/tests/test_realm_creation.py+++ b/zerver/tests/test_realm_creation.py@@ -49,7 +49,6 @@ class DemoCreationTest(ZulipTestCase):))- self.assertIn("demo-", realm.string_id)self.assertIn("demo test", realm.name)expected_deletion_date = realm.date_created + timedelta(days=30)self.assertEqual(realm.demo_organization_scheduled_deletion_date, expected_deletion_date)diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 7ad61cb031d..3662a5875c2 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -3549,7 +3549,6 @@ class UserSignUpTest(ZulipTestCase):)self.assertEqual(realm.demo_organization_scheduled_deletion_date, expected_deletion_date)self.assertIn("Demo organization", realm.name)- self.assertIn("demo-", realm.string_id)# Make sure the correct Welcome Bot direct message is sent.welcome_msg = Message.objects.filter(diff --git a/zerver/views/registration.py b/zerver/views/registration.pyindex c2e477524b8..3570b071bc0 100644--- a/zerver/views/registration.py+++ b/zerver/views/registration.py@@ -1,6 +1,5 @@import loggingimport random-import stringfrom collections.abc import Iterablefrom contextlib import suppressfrom typing import Annotated, Any, cast@@ -68,6 +67,7 @@ from zerver.forms import (RegistrationForm,check_subdomain_available,)+from zerver.lib.demo_organizations import DEMO_ORGANIZATION_WORD_LISTfrom zerver.lib.email_validation import email_allowed_for_realm, validate_email_not_already_in_realmfrom zerver.lib.exceptions import JsonableError, RateLimitedErrorfrom zerver.lib.i18n import (@@ -1420,11 +1420,13 @@ def create_realm(request: HttpRequest, confirmation_key: str | None = None) -> Hdef generate_demo_realm_subdomain() -> str:demo_subdomain = ""while True:- # Generate a moderate-entropy random ID for the organization;- # there are 10**4 * 26**4 ~= 4.5 billion possibilities.- letters = "".join(random.SystemRandom().choice(string.ascii_lowercase) for i in range(4))- digits = "".join(random.SystemRandom().choice(string.digits) for i in range(4))- demo_subdomain = f"demo-{letters}{digits}"+ # Generate a moderate-entropy string for the organization;+ # there are ~460 adverbs, ~1020 adjectives, ~2810 nouns in+ # the word list so ~= 1.32 billion possibilities.+ adverb = random.SystemRandom().choice(DEMO_ORGANIZATION_WORD_LIST["adverbs"])+ adjective = random.SystemRandom().choice(DEMO_ORGANIZATION_WORD_LIST["adjectives"])+ noun = random.SystemRandom().choice(DEMO_ORGANIZATION_WORD_LIST["nouns"])+ demo_subdomain = f"{adverb}-{adjective}-{noun}"try:check_subdomain_available(demo_subdomain)break