More API magic

This commit is contained in:
Sebastian Lohff 2017-04-04 02:53:03 +02:00
parent 7927fbdf96
commit 2bda17a6e1
5 changed files with 118 additions and 0 deletions

View File

@ -5,5 +5,9 @@ from . import views as api_views
urlpatterns = [ urlpatterns = [
url(r'asblock/free-as/$', api_views.asblockFreeAS, name='asblock-free-as'), url(r'asblock/free-as/$', api_views.asblockFreeAS, name='asblock-free-as'),
url(r'asblock/free-subnet/$', api_views.freeSubnet, name='inetnum-free-subnet'), url(r'asblock/free-subnet/$', api_views.freeSubnet, name='inetnum-free-subnet'),
url(r'inetnum/get-subnet/$', api_views.getSubnet, name='inetnum-get-subnet'),
url(r'domain/check/$', api_views.checkDomain, name='domain-check'),
url(r'rzone/check/$', api_views.checkRzone, name='rzone-check'),
] ]

View File

@ -2,8 +2,11 @@
from django.http import JsonResponse from django.http import JsonResponse
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.contrib.auth.views import login_required from django.contrib.auth.views import login_required
from django.db.models import Q
from whoisdb.models import ASBlock, ASNumber, InetNum from whoisdb.models import ASBlock, ASNumber, InetNum
from domains.models import Domain, ReverseZone
from dnmgmt.settings import TLD_NAMESERVERS
@login_required @login_required
def asblockFreeAS(request): def asblockFreeAS(request):
@ -46,6 +49,8 @@ def asblockFreeAS(request):
except ValidationError as e: except ValidationError as e:
ret["errorMsg"] = e.message ret["errorMsg"] = e.message
return JsonResponse(ret) return JsonResponse(ret)
@login_required @login_required
def freeSubnet(request): def freeSubnet(request):
@ -120,4 +125,75 @@ def freeSubnet(request):
ret["success"] = True ret["success"] = True
except ValidationError as e: except ValidationError as e:
ret["errorMsg"] = e.message ret["errorMsg"] = e.message
return JsonResponse(ret) return JsonResponse(ret)
@login_required
def getSubnet(request):
ret = {
"success": False,
"errorMsg": None,
"network": None,
}
try:
netName = request.GET.get('net', None)
mnts = request.user.maintainer_set.all()
nets = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
net = nets.get(handle=netName)
ret["success"] = True
ret["network"] = net.prefix()
except InetNum.DoesNotExist:
ret["errorMsg"] = "Chosen network does not exist"
return JsonResponse(ret)
@login_required
def checkDomain(request):
ret = {
"success": False,
"errorMsg": None,
"domain": None,
"result": None,
}
try:
domainName = Domain.fixName(request.GET.get('domain', ''))
domain = Domain.objects.get(name=domainName)
if not domain.canEdit(request.user):
raise Domain.DoesNotExist()
ret["success"] = True
ret["domain"] = domain.name
ret["result"] = checkDomain(domain.name, TLD_NAMESERVERS, domain.nameservers.all())
except Domain.DoesNotExist:
ret["errorMsg"] = "Domain does not exist"
return JsonResponse(ret)
@login_required
def checkRzone(request):
ret = {
"success": False,
"errorMsg": None,
"domain": None,
"result": None,
}
try:
rzonePk = ReverseZone.fixName(request.GET.get('rzone', ''))
rzone = ReverseZone.objects.get(pk=rzonePk)
if not rzone.canEdit(request.user):
raise ReverseZone.DoesNotExist()
ret["success"] = True
ret["result"] = checkDomain(rzone.name, TLD_NAMESERVERS, rzone.nameservers.all())
except Domain.DoesNotExist:
ret["errorMsg"] = "ReverseZone does not exist"
return JsonResponse(ret)

View File

@ -16,6 +16,9 @@ from django.contrib import messages
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TLD_NAMESERVERS = [
'10.100.100.100',
]
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

View File

@ -68,6 +68,12 @@ class Domain(MntdObject):
return reasons return reasons
@classmethod
def fixName(clazz, name):
if not name.endswith("."):
name += "."
return name.lower()
class ReverseZone(WhoisObject): class ReverseZone(WhoisObject):
handle = None handle = None

View File

@ -17,5 +17,34 @@
</div> </div>
</div> </div>
</div> </div>
<script>
function getSubnet() {
var netInp = $('#div_id_prefix input');
netInp.val("");
netInp.attr("placeholder", "Getting network...");
var parentNetOpt = $('#id_parentNet option:selected');
if(parentNetOpt.val()) {
$.get("{% url "api:inetnum-get-subnet" %}", {net: parentNetOpt.text()}, function(data) {
if(data.success) {
netInp.attr("placeholder", "")
netInp.val(data.network);
} else {
errorMsg = "Nothing returned via API";
if(data.errorMsg)
errorMsg = data.errorMsg;
netInp.attr("placeholder", data.errorMsg);
}
});
} else {
netInp.attr("placeholder", "No Parent Range selected!");
}
}
$(document).ready(function() {
$('#div_id_prefix label').append(' <small>(<a onClick="getSubnet();">autofill network</a>)</small>');
});
</script>
{% endblock %} {% endblock %}