[Django #10] Putting things together
This commit is contained in:
parent
e7c1b3c12a
commit
572fe59719
8 changed files with 123 additions and 7 deletions
|
@ -25,7 +25,7 @@ SECRET_KEY = 'fj+%jfo4xd5rb(7w!7feda03q)y4md)*)e_rjw8i_gyab!6#-h'
|
|||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = ["192.168.0.12",]
|
||||
ALLOWED_HOSTS = ["192.168.0.12", "testjg.django.lab.ungleich.ch", ]
|
||||
|
||||
|
||||
# Application definition
|
||||
|
|
|
@ -4,3 +4,9 @@ from netfields import CidrAddressField, NetManager
|
|||
class ip(forms.Form):
|
||||
inputIP = forms.CharField()
|
||||
#inputIP = CidrAddressField()
|
||||
|
||||
class ip_random(forms.Form):
|
||||
inputNetwork = forms.CharField()
|
||||
NetworkName = forms.CharField()
|
||||
NetworkDescription = forms.CharField()
|
||||
|
||||
|
|
23
kjg/IPv6/ula/ularegistry/migrations/0002_ips2.py
Normal file
23
kjg/IPv6/ula/ularegistry/migrations/0002_ips2.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.0.8 on 2020-08-03 14:30
|
||||
|
||||
from django.db import migrations, models
|
||||
import netfields.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ularegistry', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ips2',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('inet2', netfields.fields.CidrAddressField(max_length=43)),
|
||||
('inet2_name', models.CharField(max_length=200)),
|
||||
('inet2_descrip', models.CharField(max_length=200)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -10,4 +10,8 @@ class ips(models.Model):
|
|||
st = str(self.inet)
|
||||
return st
|
||||
|
||||
|
||||
class ips2(models.Model):
|
||||
inet2 = CidrAddressField()
|
||||
inet2_name = models.CharField(max_length=200)
|
||||
inet2_descrip = models.CharField(max_length=200)
|
||||
|
||||
|
|
29
kjg/IPv6/ula/ularegistry/randomcreate.py
Normal file
29
kjg/IPv6/ula/ularegistry/randomcreate.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from sys import argv
|
||||
import argparse
|
||||
import ipaddress
|
||||
import random
|
||||
|
||||
def createIP(ip):
|
||||
ipnet = ip
|
||||
size = 80
|
||||
|
||||
ip = ipnet.split('/')
|
||||
ip2 = ipaddress.ip_network(ip[0]).supernet(new_prefix=int(ip[1]))
|
||||
|
||||
# minimum network
|
||||
minNet = ipaddress.ip_network(ip[0]).supernet(new_prefix=128-size)
|
||||
|
||||
# seperate network
|
||||
ip2first = ipaddress.IPv6Network(ip2)[0]
|
||||
ip2last = ipaddress.IPv6Network(ip2)[-1]
|
||||
minNetlast = ipaddress.IPv6Network(minNet)[-1]
|
||||
|
||||
# calculation network
|
||||
maxRan = int(ip2last) - int(minNetlast) >> size
|
||||
result = ipaddress.ip_address((random.randrange(0,maxRan) << size) + int(ip2first) )
|
||||
resultNetwork = ipaddress.ip_network(result).supernet(new_prefix=128-size)
|
||||
|
||||
print(resultNetwork)
|
||||
return resultNetwork
|
||||
|
||||
|
16
kjg/IPv6/ula/ularegistry/templates/randomIP.html
Normal file
16
kjg/IPv6/ula/ularegistry/templates/randomIP.html
Normal file
|
@ -0,0 +1,16 @@
|
|||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
{{ form }}
|
||||
</table>
|
||||
<input type="submit" value="random create">
|
||||
</form>
|
||||
{% if ips_listr %}
|
||||
<ul>
|
||||
{% for ips2 in ips_listr %}
|
||||
<li>{{ ips2.inet2 }} {{ ips2.inet2_name}} {{ ips2.inet2_descrip }} </li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p>No registered IP.</p>
|
||||
{% endif %}
|
|
@ -4,5 +4,5 @@ from . import views
|
|||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
path('<int:ips_id>/results/', views.results, name='results'),
|
||||
path('randomIP', views.randomIP, name='randomIP'),
|
||||
]
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from .forms import ip
|
||||
from ularegistry.models import ips
|
||||
from .forms import ip, ip_random
|
||||
from ularegistry.models import ips, ips2
|
||||
from django.shortcuts import redirect
|
||||
from ularegistry.checkip import *
|
||||
from ularegistry.randomcreate import *
|
||||
|
||||
# Create your views here.
|
||||
def index(request):
|
||||
|
@ -42,7 +43,44 @@ def index(request):
|
|||
return render(request, 'index.html', ctx)
|
||||
#return HttpResponse("Hello, world.")
|
||||
|
||||
def randomIP(request):
|
||||
ips_listr = ips2.objects.values()
|
||||
if request.method == "POST":
|
||||
form = ip_random(request.POST)
|
||||
print(form)
|
||||
if form.is_valid():
|
||||
while True:
|
||||
newIP = createIP(form.cleaned_data['inputNetwork'])
|
||||
ips_listr2 = list(ips_listr)
|
||||
if not ips_listr2:
|
||||
print("test")
|
||||
q = ips2(inet2=newIP, inet2_name=form.cleaned_data['NetworkName'], inet2_descrip=form.cleaned_data['NetworkDescription'])
|
||||
q.save()
|
||||
break
|
||||
|
||||
overlapchk = 0
|
||||
|
||||
for value in ips_listr2:
|
||||
if overlapip(newIP,value['inet2']):
|
||||
overlapchk = 1
|
||||
print(overlapchk)
|
||||
q = ips2(inet2=newIP, inet2_name=form.cleaned_data['NetworkName'], inet2_descrip=form.cleaned_data['NetworkDescription'])
|
||||
q.save()
|
||||
break
|
||||
else:
|
||||
overlapchk = 0
|
||||
|
||||
if overlapchk:
|
||||
break
|
||||
|
||||
return redirect('randomIP')
|
||||
|
||||
else:
|
||||
form = ip_random()
|
||||
|
||||
ctx = { 'form' : form,
|
||||
'ips_listr' : ips_listr,
|
||||
}
|
||||
return render(request, 'randomIP.html', ctx)
|
||||
|
||||
def results(request, ips_id):
|
||||
return HttpResponse(ips_id)
|
||||
|
||||
|
|
Loading…
Reference in a new issue