Hook up enum

This commit is contained in:
zandercymatics 2024-01-09 12:30:23 -07:00
parent 5b2eeee547
commit 91ed4a598c
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 10 additions and 16 deletions

View file

@ -5,6 +5,7 @@ from django.http import HttpResponse
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from registrar.templatetags.url_helpers import public_site_url from registrar.templatetags.url_helpers import public_site_url
from registrar.utility.enums import ValidationErrorReturnType
from registrar.utility.errors import GenericError, GenericErrorCodes from registrar.utility.errors import GenericError, GenericErrorCodes
import requests import requests
@ -92,7 +93,7 @@ def available(request, domain=""):
json_response = Domain.validate_and_handle_errors( json_response = Domain.validate_and_handle_errors(
domain=domain, domain=domain,
error_return_type="JSON_RESPONSE", error_return_type=ValidationErrorReturnType.JSON_RESPONSE,
display_success=True, display_success=True,
) )
return json_response return json_response

View file

@ -13,6 +13,7 @@ from api.views import DOMAIN_API_MESSAGES
from registrar.models import Contact, DomainApplication, DraftDomain, Domain from registrar.models import Contact, DomainApplication, DraftDomain, Domain
from registrar.templatetags.url_helpers import public_site_url from registrar.templatetags.url_helpers import public_site_url
from registrar.utility import errors from registrar.utility import errors
from registrar.utility.enums import ValidationErrorReturnType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -385,7 +386,7 @@ class AlternativeDomainForm(RegistrarForm):
def clean_alternative_domain(self): def clean_alternative_domain(self):
"""Validation code for domain names.""" """Validation code for domain names."""
requested = self.cleaned_data.get("alternative_domain", None) 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 return validated
alternative_domain = forms.CharField( alternative_domain = forms.CharField(
@ -461,7 +462,7 @@ class DotGovDomainForm(RegistrarForm):
def clean_requested_domain(self): def clean_requested_domain(self):
"""Validation code for domain names.""" """Validation code for domain names."""
requested = self.cleaned_data.get("requested_domain", None) 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 return validated
requested_domain = forms.CharField(label="What .gov domain do you want?") requested_domain = forms.CharField(label="What .gov domain do you want?")

View file

@ -7,6 +7,7 @@ from django.http import JsonResponse
from api.views import DOMAIN_API_MESSAGES, check_domain_available from api.views import DOMAIN_API_MESSAGES, check_domain_available
from registrar.utility import errors from registrar.utility import errors
from epplibwrapper.errors import RegistryError from epplibwrapper.errors import RegistryError
from registrar.utility.enums import ValidationErrorReturnType
class DomainHelper: class DomainHelper:
@ -56,7 +57,7 @@ class DomainHelper:
return domain return domain
@classmethod @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.""" """Runs validate() and catches possible exceptions."""
try: try:
validated = cls.validate(domain) validated = cls.validate(domain)
@ -89,20 +90,11 @@ class DomainHelper:
return validated return validated
@staticmethod @staticmethod
def _return_form_error_or_json_response(return_type, code, available=False): def _return_form_error_or_json_response(return_type: ValidationErrorReturnType, 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??
match return_type: match return_type:
case ValidationErrorReturnType.FORM_VALIDATION_ERROR.value: case ValidationErrorReturnType.FORM_VALIDATION_ERROR:
raise forms.ValidationError(DOMAIN_API_MESSAGES[code], code=code) raise forms.ValidationError(DOMAIN_API_MESSAGES[code], code=code)
case ValidationErrorReturnType.JSON_RESPONSE.value: case ValidationErrorReturnType.JSON_RESPONSE:
return JsonResponse( return JsonResponse(
{"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]} {"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]}
) )