mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-03 09:43:33 +02:00
Unify error messages under one banner
This commit is contained in:
parent
02456e9297
commit
89431b111d
4 changed files with 107 additions and 58 deletions
|
@ -1,7 +1,7 @@
|
||||||
"""Internal API views"""
|
"""Internal API views"""
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.views.decorators.http import require_http_methods
|
from django.views.decorators.http import require_http_methods
|
||||||
from django.http import HttpResponse, JsonResponse
|
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
|
||||||
|
@ -71,6 +71,7 @@ def check_domain_available(domain):
|
||||||
a match. If check fails, throws a RegistryError.
|
a match. If check fails, throws a RegistryError.
|
||||||
"""
|
"""
|
||||||
Domain = apps.get_model("registrar.Domain")
|
Domain = apps.get_model("registrar.Domain")
|
||||||
|
|
||||||
if domain.endswith(".gov"):
|
if domain.endswith(".gov"):
|
||||||
return Domain.available(domain)
|
return Domain.available(domain)
|
||||||
else:
|
else:
|
||||||
|
@ -86,29 +87,15 @@ def available(request, domain=""):
|
||||||
Response is a JSON dictionary with the key "available" and value true or
|
Response is a JSON dictionary with the key "available" and value true or
|
||||||
false.
|
false.
|
||||||
"""
|
"""
|
||||||
|
Domain = apps.get_model("registrar.Domain")
|
||||||
domain = request.GET.get("domain", "")
|
domain = request.GET.get("domain", "")
|
||||||
DraftDomain = apps.get_model("registrar.DraftDomain")
|
|
||||||
if domain is None or domain.strip() == "":
|
json_response = Domain.validate_and_handle_errors(
|
||||||
# TODO - change this... should it be the regular required?
|
domain=domain,
|
||||||
return JsonResponse({"available": False, "code": "invalid", "message": "This field is required"})
|
error_return_type="JSON_RESPONSE",
|
||||||
# validate that the given domain could be a domain name and fail early if
|
display_success=True,
|
||||||
# not.
|
)
|
||||||
if not (DraftDomain.string_could_be_domain(domain) or DraftDomain.string_could_be_domain(domain + ".gov")):
|
return json_response
|
||||||
print(f"What is the domain at this point? {domain}")
|
|
||||||
if "." in domain:
|
|
||||||
return JsonResponse({"available": False, "code": "invalid", "message": DOMAIN_API_MESSAGES["extra_dots"]})
|
|
||||||
else:
|
|
||||||
return JsonResponse({"available": False, "code": "invalid", "message": DOMAIN_API_MESSAGES["invalid"]})
|
|
||||||
# a domain is available if it is NOT in the list of current domains
|
|
||||||
try:
|
|
||||||
if check_domain_available(domain):
|
|
||||||
return JsonResponse({"available": True, "code": "success", "message": DOMAIN_API_MESSAGES["success"]})
|
|
||||||
else:
|
|
||||||
return JsonResponse(
|
|
||||||
{"available": False, "code": "unavailable", "message": DOMAIN_API_MESSAGES["unavailable"]}
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
return JsonResponse({"available": False, "code": "error", "message": DOMAIN_API_MESSAGES["error"]})
|
|
||||||
|
|
||||||
|
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
|
|
|
@ -384,17 +384,8 @@ CurrentSitesFormSet = forms.formset_factory(
|
||||||
class AlternativeDomainForm(RegistrarForm):
|
class AlternativeDomainForm(RegistrarForm):
|
||||||
def clean_alternative_domain(self):
|
def clean_alternative_domain(self):
|
||||||
"""Validation code for domain names."""
|
"""Validation code for domain names."""
|
||||||
try:
|
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(requested, blank_ok=True)
|
|
||||||
except errors.ExtraDotsError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["extra_dots"], code="extra_dots")
|
|
||||||
except errors.DomainUnavailableError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["unavailable"], code="unavailable")
|
|
||||||
except errors.RegistrySystemError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["error"], code="error")
|
|
||||||
except ValueError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["invalid"], code="invalid")
|
|
||||||
return validated
|
return validated
|
||||||
|
|
||||||
alternative_domain = forms.CharField(
|
alternative_domain = forms.CharField(
|
||||||
|
@ -469,19 +460,8 @@ class DotGovDomainForm(RegistrarForm):
|
||||||
|
|
||||||
def clean_requested_domain(self):
|
def clean_requested_domain(self):
|
||||||
"""Validation code for domain names."""
|
"""Validation code for domain names."""
|
||||||
try:
|
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(requested)
|
|
||||||
except errors.BlankValueError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["required"], code="required")
|
|
||||||
except errors.ExtraDotsError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["extra_dots"], code="extra_dots")
|
|
||||||
except errors.DomainUnavailableError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["unavailable"], code="unavailable")
|
|
||||||
except errors.RegistrySystemError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["error"], code="error")
|
|
||||||
except ValueError:
|
|
||||||
raise forms.ValidationError(DOMAIN_API_MESSAGES["invalid"], code="invalid")
|
|
||||||
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?")
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
|
from enum import Enum
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from api.views import check_domain_available
|
from django import forms
|
||||||
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
class ValidationErrorReturnType(Enum):
|
||||||
|
JSON_RESPONSE = "JSON_RESPONSE"
|
||||||
|
FORM_VALIDATION_ERROR = "FORM_VALIDATION_ERROR"
|
||||||
|
|
||||||
class DomainHelper:
|
class DomainHelper:
|
||||||
"""Utility functions and constants for domain names."""
|
"""Utility functions and constants for domain names."""
|
||||||
|
@ -28,16 +35,10 @@ class DomainHelper:
|
||||||
if domain is None:
|
if domain is None:
|
||||||
raise errors.BlankValueError()
|
raise errors.BlankValueError()
|
||||||
if not isinstance(domain, str):
|
if not isinstance(domain, str):
|
||||||
raise ValueError("Domain name must be a string")
|
raise errors.InvalidDomainError("Domain name must be a string")
|
||||||
domain = domain.lower().strip()
|
|
||||||
if domain == "" and not blank_ok:
|
domain = DomainHelper._parse_domain_string(domain, blank_ok)
|
||||||
raise errors.BlankValueError()
|
|
||||||
if domain.endswith(".gov"):
|
|
||||||
domain = domain[:-4]
|
|
||||||
if "." in domain:
|
|
||||||
raise errors.ExtraDotsError()
|
|
||||||
if not DomainHelper.string_could_be_domain(domain + ".gov"):
|
|
||||||
raise ValueError()
|
|
||||||
try:
|
try:
|
||||||
if not check_domain_available(domain):
|
if not check_domain_available(domain):
|
||||||
raise errors.DomainUnavailableError()
|
raise errors.DomainUnavailableError()
|
||||||
|
@ -45,6 +46,85 @@ class DomainHelper:
|
||||||
raise errors.RegistrySystemError() from err
|
raise errors.RegistrySystemError() from err
|
||||||
return domain
|
return domain
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_domain_string(domain: str, blank_ok) -> str:
|
||||||
|
"""Parses '.gov' out of the domain_name string, and does some validation on it"""
|
||||||
|
domain = domain.lower().strip()
|
||||||
|
|
||||||
|
if domain == "" and not blank_ok:
|
||||||
|
raise errors.BlankValueError()
|
||||||
|
|
||||||
|
if domain.endswith(".gov"):
|
||||||
|
domain = domain[:-4]
|
||||||
|
|
||||||
|
if "." in domain:
|
||||||
|
raise errors.ExtraDotsError()
|
||||||
|
|
||||||
|
if not DomainHelper.string_could_be_domain(domain + ".gov"):
|
||||||
|
raise errors.InvalidDomainError()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def validate_and_handle_errors(cls, domain: str, error_return_type: str, display_success: bool = False):
|
||||||
|
"""Runs validate() and catches possible exceptions."""
|
||||||
|
try:
|
||||||
|
validated = cls.validate(domain)
|
||||||
|
except errors.BlankValueError:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="required"
|
||||||
|
)
|
||||||
|
except errors.ExtraDotsError:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="extra_dots"
|
||||||
|
)
|
||||||
|
except errors.DomainUnavailableError:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="unavailable"
|
||||||
|
)
|
||||||
|
except errors.RegistrySystemError:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="error"
|
||||||
|
)
|
||||||
|
except errors.InvalidDomainError:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="invalid"
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="error"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if display_success:
|
||||||
|
return DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="success", available=True
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return validated
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _return_form_error_or_json_response(return_type, code, available=False):
|
||||||
|
print(f"What is the code? {code}")
|
||||||
|
if return_type == "JSON_RESPONSE":
|
||||||
|
print("in the return context")
|
||||||
|
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:
|
||||||
|
case ValidationErrorReturnType.FORM_VALIDATION_ERROR:
|
||||||
|
raise forms.ValidationError(DOMAIN_API_MESSAGES[code], code=code)
|
||||||
|
case ValidationErrorReturnType.JSON_RESPONSE:
|
||||||
|
return JsonResponse(
|
||||||
|
{"available": available, "code": code, "message": DOMAIN_API_MESSAGES[code]}
|
||||||
|
)
|
||||||
|
case _:
|
||||||
|
raise ValueError("Invalid return type specified")
|
||||||
|
"""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def sld(cls, domain: str):
|
def sld(cls, domain: str):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -16,6 +16,8 @@ class DomainUnavailableError(ValueError):
|
||||||
class RegistrySystemError(ValueError):
|
class RegistrySystemError(ValueError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class InvalidDomainError(ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
class ActionNotAllowed(Exception):
|
class ActionNotAllowed(Exception):
|
||||||
"""User accessed an action that is not
|
"""User accessed an action that is not
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue