mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	forms: Use RealmDetailsForm as subclass for RegistrationForm.
We now use RealmDetailsForm as subclass for RegistrationForm such that we can avoid duplication of the checks for realm_subdomain field.
This commit is contained in:
		
							
								
								
									
										116
									
								
								zerver/forms.py
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								zerver/forms.py
									
									
									
									
									
								
							@@ -105,14 +105,59 @@ def check_subdomain_available(subdomain: str, allow_reserved_subdomain: bool = F
 | 
				
			|||||||
        raise ValidationError(error_strings["unavailable"])
 | 
					        raise ValidationError(error_strings["unavailable"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class RegistrationForm(forms.Form):
 | 
					def email_not_system_bot(email: str) -> None:
 | 
				
			||||||
 | 
					    if is_cross_realm_bot_email(email):
 | 
				
			||||||
 | 
					        msg = email_reserved_for_system_bots_error(email)
 | 
				
			||||||
 | 
					        code = msg
 | 
				
			||||||
 | 
					        raise ValidationError(
 | 
				
			||||||
 | 
					            msg,
 | 
				
			||||||
 | 
					            code=code,
 | 
				
			||||||
 | 
					            params=dict(deactivated=False),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def email_is_not_disposable(email: str) -> None:
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        domain = Address(addr_spec=email).domain
 | 
				
			||||||
 | 
					    except (HeaderParseError, ValueError):
 | 
				
			||||||
 | 
					        raise ValidationError(_("Please use your real email address."))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if is_disposable_domain(domain):
 | 
				
			||||||
 | 
					        raise ValidationError(_("Please use your real email address."))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class RealmDetailsForm(forms.Form):
 | 
				
			||||||
 | 
					    realm_subdomain = forms.CharField(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH, required=False)
 | 
				
			||||||
 | 
					    realm_type = forms.TypedChoiceField(
 | 
				
			||||||
 | 
					        coerce=int, choices=[(t["id"], t["name"]) for t in Realm.ORG_TYPES.values()]
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    realm_name = forms.CharField(max_length=Realm.MAX_REALM_NAME_LENGTH)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self, *args: Any, **kwargs: Any) -> None:
 | 
				
			||||||
 | 
					        self.realm_creation = kwargs["realm_creation"]
 | 
				
			||||||
 | 
					        del kwargs["realm_creation"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        super().__init__(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def clean_realm_subdomain(self) -> str:
 | 
				
			||||||
 | 
					        if not self.realm_creation:
 | 
				
			||||||
 | 
					            # This field is only used if realm_creation
 | 
				
			||||||
 | 
					            return ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        subdomain = self.cleaned_data["realm_subdomain"]
 | 
				
			||||||
 | 
					        if "realm_in_root_domain" in self.data:
 | 
				
			||||||
 | 
					            subdomain = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        check_subdomain_available(subdomain)
 | 
				
			||||||
 | 
					        return subdomain
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class RegistrationForm(RealmDetailsForm):
 | 
				
			||||||
    MAX_PASSWORD_LENGTH = 100
 | 
					    MAX_PASSWORD_LENGTH = 100
 | 
				
			||||||
    full_name = forms.CharField(max_length=UserProfile.MAX_NAME_LENGTH)
 | 
					    full_name = forms.CharField(max_length=UserProfile.MAX_NAME_LENGTH)
 | 
				
			||||||
    # The required-ness of the password field gets overridden if it isn't
 | 
					    # The required-ness of the password field gets overridden if it isn't
 | 
				
			||||||
    # actually required for a realm
 | 
					    # actually required for a realm
 | 
				
			||||||
    password = forms.CharField(widget=forms.PasswordInput, max_length=MAX_PASSWORD_LENGTH)
 | 
					    password = forms.CharField(widget=forms.PasswordInput, max_length=MAX_PASSWORD_LENGTH)
 | 
				
			||||||
    realm_subdomain = forms.CharField(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH, required=False)
 | 
					 | 
				
			||||||
    realm_type = forms.IntegerField(required=False)
 | 
					 | 
				
			||||||
    is_demo_organization = forms.BooleanField(required=False)
 | 
					    is_demo_organization = forms.BooleanField(required=False)
 | 
				
			||||||
    enable_marketing_emails = forms.BooleanField(required=False)
 | 
					    enable_marketing_emails = forms.BooleanField(required=False)
 | 
				
			||||||
    email_address_visibility = forms.TypedChoiceField(
 | 
					    email_address_visibility = forms.TypedChoiceField(
 | 
				
			||||||
@@ -129,7 +174,6 @@ class RegistrationForm(forms.Form):
 | 
				
			|||||||
        # Since the superclass doesn't except random extra kwargs, we
 | 
					        # Since the superclass doesn't except random extra kwargs, we
 | 
				
			||||||
        # remove it from the kwargs dict before initializing.
 | 
					        # remove it from the kwargs dict before initializing.
 | 
				
			||||||
        self.realm_creation = kwargs["realm_creation"]
 | 
					        self.realm_creation = kwargs["realm_creation"]
 | 
				
			||||||
        del kwargs["realm_creation"]
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        super().__init__(*args, **kwargs)
 | 
					        super().__init__(*args, **kwargs)
 | 
				
			||||||
        if settings.TERMS_OF_SERVICE_VERSION is not None:
 | 
					        if settings.TERMS_OF_SERVICE_VERSION is not None:
 | 
				
			||||||
@@ -137,6 +181,11 @@ class RegistrationForm(forms.Form):
 | 
				
			|||||||
        self.fields["realm_name"] = forms.CharField(
 | 
					        self.fields["realm_name"] = forms.CharField(
 | 
				
			||||||
            max_length=Realm.MAX_REALM_NAME_LENGTH, required=self.realm_creation
 | 
					            max_length=Realm.MAX_REALM_NAME_LENGTH, required=self.realm_creation
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					        self.fields["realm_type"] = forms.TypedChoiceField(
 | 
				
			||||||
 | 
					            coerce=int,
 | 
				
			||||||
 | 
					            choices=[(t["id"], t["name"]) for t in Realm.ORG_TYPES.values()],
 | 
				
			||||||
 | 
					            required=self.realm_creation,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def clean_full_name(self) -> str:
 | 
					    def clean_full_name(self) -> str:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
@@ -153,18 +202,6 @@ class RegistrationForm(forms.Form):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return password
 | 
					        return password
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def clean_realm_subdomain(self) -> str:
 | 
					 | 
				
			||||||
        if not self.realm_creation:
 | 
					 | 
				
			||||||
            # This field is only used if realm_creation
 | 
					 | 
				
			||||||
            return ""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        subdomain = self.cleaned_data["realm_subdomain"]
 | 
					 | 
				
			||||||
        if "realm_in_root_domain" in self.data:
 | 
					 | 
				
			||||||
            subdomain = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        check_subdomain_available(subdomain)
 | 
					 | 
				
			||||||
        return subdomain
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ToSForm(forms.Form):
 | 
					class ToSForm(forms.Form):
 | 
				
			||||||
    terms = forms.BooleanField(required=True)
 | 
					    terms = forms.BooleanField(required=True)
 | 
				
			||||||
@@ -238,53 +275,6 @@ class HomepageForm(forms.Form):
 | 
				
			|||||||
        return email
 | 
					        return email
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def email_not_system_bot(email: str) -> None:
 | 
					 | 
				
			||||||
    if is_cross_realm_bot_email(email):
 | 
					 | 
				
			||||||
        msg = email_reserved_for_system_bots_error(email)
 | 
					 | 
				
			||||||
        code = msg
 | 
					 | 
				
			||||||
        raise ValidationError(
 | 
					 | 
				
			||||||
            msg,
 | 
					 | 
				
			||||||
            code=code,
 | 
					 | 
				
			||||||
            params=dict(deactivated=False),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def email_is_not_disposable(email: str) -> None:
 | 
					 | 
				
			||||||
    try:
 | 
					 | 
				
			||||||
        domain = Address(addr_spec=email).domain
 | 
					 | 
				
			||||||
    except (HeaderParseError, ValueError):
 | 
					 | 
				
			||||||
        raise ValidationError(_("Please use your real email address."))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if is_disposable_domain(domain):
 | 
					 | 
				
			||||||
        raise ValidationError(_("Please use your real email address."))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class RealmDetailsForm(forms.Form):
 | 
					 | 
				
			||||||
    realm_subdomain = forms.CharField(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH, required=False)
 | 
					 | 
				
			||||||
    realm_type = forms.TypedChoiceField(
 | 
					 | 
				
			||||||
        coerce=int, choices=[(t["id"], t["name"]) for t in Realm.ORG_TYPES.values()]
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
    realm_name = forms.CharField(max_length=Realm.MAX_REALM_NAME_LENGTH)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def __init__(self, *args: Any, **kwargs: Any) -> None:
 | 
					 | 
				
			||||||
        self.realm_creation = kwargs["realm_creation"]
 | 
					 | 
				
			||||||
        del kwargs["realm_creation"]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        super().__init__(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def clean_realm_subdomain(self) -> str:
 | 
					 | 
				
			||||||
        if not self.realm_creation:
 | 
					 | 
				
			||||||
            # This field is only used if realm_creation
 | 
					 | 
				
			||||||
            return ""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        subdomain = self.cleaned_data["realm_subdomain"]
 | 
					 | 
				
			||||||
        if "realm_in_root_domain" in self.data:
 | 
					 | 
				
			||||||
            subdomain = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        check_subdomain_available(subdomain)
 | 
					 | 
				
			||||||
        return subdomain
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class RealmCreationForm(RealmDetailsForm):
 | 
					class RealmCreationForm(RealmDetailsForm):
 | 
				
			||||||
    # This form determines whether users can create a new realm.
 | 
					    # This form determines whether users can create a new realm.
 | 
				
			||||||
    email = forms.EmailField(validators=[email_not_system_bot, email_is_not_disposable])
 | 
					    email = forms.EmailField(validators=[email_not_system_bot, email_is_not_disposable])
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user