mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-16 01:27:03 +02:00
Enable domain availability checker
This commit is contained in:
parent
c2feb1fc16
commit
456cab6cee
5 changed files with 157 additions and 68 deletions
|
@ -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. Don’t 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 isn’t 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"]
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue