diff --git a/src/api/views.py b/src/api/views.py index 4bec29b80..b89e2629d 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -5,6 +5,7 @@ from django.http import HttpResponse from django.utils.safestring import mark_safe from registrar.templatetags.url_helpers import public_site_url +from registrar.utility.enums import ValidationErrorReturnType from registrar.utility.errors import GenericError, GenericErrorCodes import requests @@ -92,7 +93,7 @@ def available(request, domain=""): json_response = Domain.validate_and_handle_errors( domain=domain, - error_return_type="JSON_RESPONSE", + error_return_type=ValidationErrorReturnType.JSON_RESPONSE, display_success=True, ) return json_response diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py index 00f832d59..aa583a10c 100644 --- a/src/registrar/forms/application_wizard.py +++ b/src/registrar/forms/application_wizard.py @@ -13,6 +13,7 @@ from api.views import DOMAIN_API_MESSAGES from registrar.models import Contact, DomainApplication, DraftDomain, Domain from registrar.templatetags.url_helpers import public_site_url from registrar.utility import errors +from registrar.utility.enums import ValidationErrorReturnType logger = logging.getLogger(__name__) @@ -385,7 +386,7 @@ class AlternativeDomainForm(RegistrarForm): def clean_alternative_domain(self): """Validation code for domain names.""" requested = self.cleaned_data.get("alternative_domain", None) - validated = DraftDomain.validate_and_handle_errors(requested, "FORM_VALIDATION_ERROR") + validated = DraftDomain.validate_and_handle_errors(requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR) return validated alternative_domain = forms.CharField( @@ -461,7 +462,7 @@ class DotGovDomainForm(RegistrarForm): def clean_requested_domain(self): """Validation code for domain names.""" requested = self.cleaned_data.get("requested_domain", None) - validated = DraftDomain.validate_and_handle_errors(requested, "FORM_VALIDATION_ERROR") + validated = DraftDomain.validate_and_handle_errors(requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR) return validated requested_domain = forms.CharField(label="What .gov domain do you want?") diff --git a/src/registrar/models/utility/domain_helper.py b/src/registrar/models/utility/domain_helper.py index 28eaa391e..cf2369567 100644 --- a/src/registrar/models/utility/domain_helper.py +++ b/src/registrar/models/utility/domain_helper.py @@ -7,6 +7,7 @@ from django.http import JsonResponse from api.views import DOMAIN_API_MESSAGES, check_domain_available from registrar.utility import errors from epplibwrapper.errors import RegistryError +from registrar.utility.enums import ValidationErrorReturnType class DomainHelper: @@ -56,7 +57,7 @@ class DomainHelper: return domain @classmethod - def validate_and_handle_errors(cls, domain: str, error_return_type: str, display_success: bool = False): + def validate_and_handle_errors(cls, domain, error_return_type, display_success = False): """Runs validate() and catches possible exceptions.""" try: validated = cls.validate(domain) @@ -89,20 +90,11 @@ class DomainHelper: return validated @staticmethod - def _return_form_error_or_json_response(return_type, code, available=False): - if return_type == "JSON_RESPONSE": - return JsonResponse( - {"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]} - ) - - if return_type == "FORM_VALIDATION_ERROR": - raise forms.ValidationError(DOMAIN_API_MESSAGES[code], code=code) - - # Why is this not working?? + def _return_form_error_or_json_response(return_type: ValidationErrorReturnType, code, available=False): match return_type: - case ValidationErrorReturnType.FORM_VALIDATION_ERROR.value: + case ValidationErrorReturnType.FORM_VALIDATION_ERROR: raise forms.ValidationError(DOMAIN_API_MESSAGES[code], code=code) - case ValidationErrorReturnType.JSON_RESPONSE.value: + case ValidationErrorReturnType.JSON_RESPONSE: return JsonResponse( {"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]} )