mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-19 02:49:21 +02:00
Name refactor
This commit is contained in:
parent
5cdbafeeb6
commit
1b292ce640
4 changed files with 20 additions and 17 deletions
|
@ -5,7 +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.enums import ValidationReturnType
|
||||||
from registrar.utility.errors import GenericError, GenericErrorCodes
|
from registrar.utility.errors import GenericError, GenericErrorCodes
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
@ -93,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=ValidationErrorReturnType.JSON_RESPONSE,
|
return_type=ValidationReturnType.JSON_RESPONSE,
|
||||||
)
|
)
|
||||||
return json_response
|
return json_response
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
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.enums import ValidationErrorReturnType
|
from registrar.utility.enums import ValidationReturnType
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -384,7 +384,9 @@ class AlternativeDomainForm(RegistrarForm):
|
||||||
"""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(
|
validated, _ = DraftDomain.validate_and_handle_errors(
|
||||||
requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR, blank_ok=True
|
domain=requested,
|
||||||
|
return_type=ValidationReturnType.FORM_VALIDATION_ERROR,
|
||||||
|
blank_ok=True,
|
||||||
)
|
)
|
||||||
return validated
|
return validated
|
||||||
|
|
||||||
|
@ -462,7 +464,8 @@ class DotGovDomainForm(RegistrarForm):
|
||||||
"""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(
|
validated, _ = DraftDomain.validate_and_handle_errors(
|
||||||
requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR
|
domain=requested,
|
||||||
|
return_type=ValidationReturnType.FORM_VALIDATION_ERROR,
|
||||||
)
|
)
|
||||||
return validated
|
return validated
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,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
|
from registrar.utility.enums import ValidationReturnType
|
||||||
|
|
||||||
|
|
||||||
class DomainHelper:
|
class DomainHelper:
|
||||||
|
@ -56,16 +56,16 @@ class DomainHelper:
|
||||||
return domain
|
return domain
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_and_handle_errors(cls, domain, error_return_type, blank_ok=False):
|
def validate_and_handle_errors(cls, domain, return_type, blank_ok=False):
|
||||||
"""
|
"""
|
||||||
Validates a domain and returns an appropriate response based on the validation result.
|
Validates a domain and returns an appropriate response based on the validation result.
|
||||||
|
|
||||||
This method uses the `validate` method to validate the domain. If validation fails, it catches the exception,
|
This method uses the `validate` method to validate the domain. If validation fails, it catches the exception,
|
||||||
maps it to a corresponding error code, and returns a response based on the `error_return_type` parameter.
|
maps it to a corresponding error code, and returns a response based on the `return_type` parameter.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
domain (str): The domain to validate.
|
domain (str): The domain to validate.
|
||||||
error_return_type (ValidationErrorReturnType): Determines the type of response (JSON or form validation error).
|
return_type (ValidationReturnType): Determines the type of response (JSON or form validation error).
|
||||||
blank_ok (bool, optional): If True, blank input does not raise an exception. Defaults to False.
|
blank_ok (bool, optional): If True, blank input does not raise an exception. Defaults to False.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
@ -95,20 +95,20 @@ class DomainHelper:
|
||||||
|
|
||||||
# Generate the response based on the error code and return type
|
# Generate the response based on the error code and return type
|
||||||
response = DomainHelper._return_form_error_or_json_response(
|
response = DomainHelper._return_form_error_or_json_response(
|
||||||
error_return_type, code=error_map.get(error_type)
|
return_type, code=error_map.get(error_type)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# For form validation, we do not need to display the success message
|
# For form validation, we do not need to display the success message
|
||||||
if error_return_type != ValidationErrorReturnType.FORM_VALIDATION_ERROR:
|
if return_type != ValidationReturnType.FORM_VALIDATION_ERROR:
|
||||||
response = DomainHelper._return_form_error_or_json_response(
|
response = DomainHelper._return_form_error_or_json_response(
|
||||||
error_return_type, code="success", available=True
|
return_type, code="success", available=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Return the validated domain and the response (either error or success)
|
# Return the validated domain and the response (either error or success)
|
||||||
return (validated, response)
|
return (validated, response)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _return_form_error_or_json_response(return_type: ValidationErrorReturnType, code, available=False):
|
def _return_form_error_or_json_response(return_type: ValidationReturnType, code, available=False):
|
||||||
"""
|
"""
|
||||||
Returns an error response based on the `return_type`.
|
Returns an error response based on the `return_type`.
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ class DomainHelper:
|
||||||
If `return_type` is neither, raises a ValueError.
|
If `return_type` is neither, raises a ValueError.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
return_type (ValidationErrorReturnType): The type of error response.
|
return_type (ValidationReturnType): The type of error response.
|
||||||
code (str): The error code for the error message.
|
code (str): The error code for the error message.
|
||||||
available (bool, optional): Availability, only used for JSON responses. Defaults to False.
|
available (bool, optional): Availability, only used for JSON responses. Defaults to False.
|
||||||
|
|
||||||
|
@ -128,9 +128,9 @@ class DomainHelper:
|
||||||
ValueError: If `return_type` is neither `FORM_VALIDATION_ERROR` nor `JSON_RESPONSE`.
|
ValueError: If `return_type` is neither `FORM_VALIDATION_ERROR` nor `JSON_RESPONSE`.
|
||||||
""" # noqa
|
""" # noqa
|
||||||
match return_type:
|
match return_type:
|
||||||
case ValidationErrorReturnType.FORM_VALIDATION_ERROR:
|
case ValidationReturnType.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:
|
case ValidationReturnType.JSON_RESPONSE:
|
||||||
return JsonResponse({"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]})
|
return JsonResponse({"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]})
|
||||||
case _:
|
case _:
|
||||||
raise ValueError("Invalid return type specified")
|
raise ValueError("Invalid return type specified")
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class ValidationErrorReturnType(Enum):
|
class ValidationReturnType(Enum):
|
||||||
"""Determines the return value of the validate_and_handle_errors class"""
|
"""Determines the return value of the validate_and_handle_errors class"""
|
||||||
|
|
||||||
JSON_RESPONSE = "JSON_RESPONSE"
|
JSON_RESPONSE = "JSON_RESPONSE"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue