Unify error messages under one banner

This commit is contained in:
zandercymatics 2024-01-09 11:41:55 -07:00
parent 02456e9297
commit 89431b111d
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 107 additions and 58 deletions

View file

@ -1,7 +1,7 @@
"""Internal API views"""
from django.apps import apps
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 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.
"""
Domain = apps.get_model("registrar.Domain")
if domain.endswith(".gov"):
return Domain.available(domain)
else:
@ -86,29 +87,15 @@ def available(request, domain=""):
Response is a JSON dictionary with the key "available" and value true or
false.
"""
Domain = apps.get_model("registrar.Domain")
domain = request.GET.get("domain", "")
DraftDomain = apps.get_model("registrar.DraftDomain")
if domain is None or domain.strip() == "":
# TODO - change this... should it be the regular required?
return JsonResponse({"available": False, "code": "invalid", "message": "This field is required"})
# validate that the given domain could be a domain name and fail early if
# not.
if not (DraftDomain.string_could_be_domain(domain) or DraftDomain.string_could_be_domain(domain + ".gov")):
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"]})
json_response = Domain.validate_and_handle_errors(
domain=domain,
error_return_type="JSON_RESPONSE",
display_success=True,
)
return json_response
@require_http_methods(["GET"])