Enable domain availability checker

This commit is contained in:
Seamus Johnston 2023-02-03 14:22:01 -06:00
parent c2feb1fc16
commit 456cab6cee
No known key found for this signature in database
GPG key ID: 2F21225985069105
5 changed files with 157 additions and 68 deletions

View file

@ -1,7 +1,5 @@
"""Internal API views"""
from django.apps import apps
from django.core.exceptions import BadRequest
from django.views.decorators.http import require_http_methods
from django.http import JsonResponse
@ -17,6 +15,19 @@ DOMAIN_FILE_URL = (
)
DOMAIN_API_MESSAGES = {
"required": "Enter the .gov domain you want. Dont include “www” or “.gov.”"
" For example, if you want www.city.gov, you would enter “city”"
" (without the quotes).",
"extra_dots": "Enter the .gov domain you want without any periods.",
"unavailable": "That domain isnt available. Try entering another one."
" Contact us if you need help coming up with a domain.",
"invalid": "Enter a domain using only letters,"
" numbers, or hyphens (though we don't recommend using hyphens).",
"success": "That domain is available!",
}
# this file doesn't change that often, nor is it that big, so cache the result
# in memory for ten minutes
@ttl_cache(ttl=600)
@ -72,6 +83,18 @@ def available(request, domain=""):
Domain.string_could_be_domain(domain)
or Domain.string_could_be_domain(domain + ".gov")
):
raise BadRequest("Invalid request.")
return JsonResponse({
"available": False,
"message": DOMAIN_API_MESSAGES["invalid"]
})
# a domain is available if it is NOT in the list of current domains
return JsonResponse({"available": not in_domains(domain)})
if in_domains(domain):
return JsonResponse({
"available": False,
"message": DOMAIN_API_MESSAGES["unavailable"]
})
else:
return JsonResponse({
"available": True,
"message": DOMAIN_API_MESSAGES["success"]
})