Browse Source

More API magic

Sebastian Lohff 3 years ago
parent
commit
2bda17a6e1
5 changed files with 118 additions and 0 deletions
  1. 4
    0
      api/urls.py
  2. 76
    0
      api/views.py
  3. 3
    0
      dnmgmt/settings.py
  4. 6
    0
      domains/models.py
  5. 29
    0
      templates/domains/obj_create.html

+ 4
- 0
api/urls.py View File

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

+ 76
- 0
api/views.py View File

@@ -2,8 +2,11 @@
2 2
 from django.http import JsonResponse
3 3
 from django.core.exceptions import ValidationError
4 4
 from django.contrib.auth.views import login_required
5
+from django.db.models import Q
5 6
 
6 7
 from whoisdb.models import ASBlock, ASNumber, InetNum
8
+from domains.models import Domain, ReverseZone
9
+from dnmgmt.settings import TLD_NAMESERVERS
7 10
 
8 11
 @login_required
9 12
 def asblockFreeAS(request):
@@ -46,6 +49,8 @@ def asblockFreeAS(request):
46 49
 	except ValidationError as e:
47 50
 		ret["errorMsg"] = e.message
48 51
 	return JsonResponse(ret)
52
+
53
+
49 54
 @login_required
50 55
 def freeSubnet(request):
51 56
 
@@ -120,4 +125,75 @@ def freeSubnet(request):
120 125
 		ret["success"] = True
121 126
 	except ValidationError as e:
122 127
 		ret["errorMsg"] = e.message
128
+
129
+	return JsonResponse(ret)
130
+
131
+
132
+@login_required
133
+def getSubnet(request):
134
+
135
+	ret = {
136
+		"success": False,
137
+		"errorMsg": None,
138
+		"network": None,
139
+	}
140
+
141
+	try:
142
+		netName = request.GET.get('net', None)
143
+		mnts = request.user.maintainer_set.all()
144
+		nets = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
145
+		net = nets.get(handle=netName)
146
+
147
+		ret["success"] = True
148
+		ret["network"] = net.prefix()
149
+	except InetNum.DoesNotExist:
150
+		ret["errorMsg"] = "Chosen network does not exist"
151
+
152
+	return JsonResponse(ret)
153
+
154
+
155
+@login_required
156
+def checkDomain(request):
157
+	ret = {
158
+		"success": False,
159
+		"errorMsg": None,
160
+		"domain": None,
161
+		"result": None,
162
+	}
163
+
164
+	try:
165
+		domainName = Domain.fixName(request.GET.get('domain', ''))
166
+		domain = Domain.objects.get(name=domainName)
167
+		if not domain.canEdit(request.user):
168
+			raise Domain.DoesNotExist()
169
+
170
+		ret["success"] = True
171
+		ret["domain"] = domain.name
172
+		ret["result"] = checkDomain(domain.name, TLD_NAMESERVERS, domain.nameservers.all())
173
+	except Domain.DoesNotExist:
174
+		ret["errorMsg"] = "Domain does not exist"
175
+
176
+	return JsonResponse(ret)
177
+
178
+@login_required
179
+def checkRzone(request):
180
+	ret = {
181
+		"success": False,
182
+		"errorMsg": None,
183
+		"domain": None,
184
+		"result": None,
185
+	}
186
+
187
+	try:
188
+		rzonePk = ReverseZone.fixName(request.GET.get('rzone', ''))
189
+		rzone = ReverseZone.objects.get(pk=rzonePk)
190
+		if not rzone.canEdit(request.user):
191
+			raise ReverseZone.DoesNotExist()
192
+
193
+		ret["success"] = True
194
+		ret["result"] = checkDomain(rzone.name, TLD_NAMESERVERS, rzone.nameservers.all())
195
+	except Domain.DoesNotExist:
196
+		ret["errorMsg"] = "ReverseZone does not exist"
197
+
123 198
 	return JsonResponse(ret)
199
+

+ 3
- 0
dnmgmt/settings.py View File

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

+ 6
- 0
domains/models.py View File

@@ -68,6 +68,12 @@ class Domain(MntdObject):
68 68
 
69 69
 		return reasons
70 70
 
71
+	@classmethod
72
+	def fixName(clazz, name):
73
+		if not name.endswith("."):
74
+			name += "."
75
+		return name.lower()
76
+
71 77
 
72 78
 class ReverseZone(WhoisObject):
73 79
 	handle = None

+ 29
- 0
templates/domains/obj_create.html View File

@@ -17,5 +17,34 @@
17 17
 		</div>
18 18
 	</div>
19 19
 </div>
20
+
21
+<script>
22
+function getSubnet() {
23
+	var netInp = $('#div_id_prefix input');
24
+	netInp.val("");
25
+	netInp.attr("placeholder", "Getting network...");
26
+
27
+	var parentNetOpt = $('#id_parentNet option:selected');
28
+	if(parentNetOpt.val()) {
29
+		$.get("{% url "api:inetnum-get-subnet" %}", {net: parentNetOpt.text()}, function(data) {
30
+			if(data.success) {
31
+				netInp.attr("placeholder", "")
32
+				netInp.val(data.network);
33
+			} else {
34
+				errorMsg = "Nothing returned via API";
35
+				if(data.errorMsg)
36
+					errorMsg = data.errorMsg;
37
+				netInp.attr("placeholder", data.errorMsg);
38
+			}
39
+		});
40
+	} else {
41
+		netInp.attr("placeholder", "No Parent Range selected!");
42
+	}
43
+}
44
+
45
+$(document).ready(function() {
46
+	$('#div_id_prefix label').append(' <small>(<a onClick="getSubnet();">autofill network</a>)</small>');
47
+});
48
+</script>
20 49
 {% endblock %}
21 50
 

Loading…
Cancel
Save