Merge branch 'master' of code.ungleich.ch:ungleich-public/ungleich-learning-circle

This commit is contained in:
Nico Schottelius 2021-01-22 10:37:59 +01:00
commit d627f35b25
12 changed files with 282 additions and 7 deletions

View file

@ -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

View file

@ -4,3 +4,13 @@ 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()
class new_ip_random(forms.Form):
NetworkName = forms.CharField()
NetworkDescription = forms.CharField()

View 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)),
],
),
]

View file

@ -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)

View 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

View file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- change something -->
{% if user.is_authenticated %}
<h3> {{ user.username }} welcome </h3>
<a href="{% url 'logout' %}">logout</a>
{% else %}
<a href="{% url 'signup' %}">signup</a>
<a href="{% url 'login' %}">login</a>
{% endif %}
{% block container %}
{% endblock %}
</body>
</html>

View file

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% block container %}
<h3>login</h3>
<form method="POST">
{% csrf_token %}
{{ login_form }}
<input type="submit" value="Submit"/>
</form>
{% endblock %}

View file

@ -0,0 +1,19 @@
{% extends 'base.html' %}
{% block container %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form2 }}
</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 %}
{% endblock %}

View 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 %}

View file

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% block container %}
<h3>sign-up</h3>
<form method="POST">
{% csrf_token %}
{{ signup_form }}
<input type="submit" name="Submit"/>
</form>
{% endblock %}

View file

@ -4,5 +4,9 @@ from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:ips_id>/results/', views.results, name='results'),
path('randomIP', views.randomIP, name='randomIP'),
path('newrandomIP', views.newrandomIP, name='newrandomIP'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
path('signup/', views.signup, name='signup'),
]

View file

@ -1,9 +1,14 @@
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ip
from ularegistry.models import ips
from .forms import ip_random, new_ip_random, ip
from ularegistry.models import ips, ips2
from django.shortcuts import redirect
from ularegistry.checkip import *
from ularegistry.randomcreate import *
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout
# Create your views here.
def index(request):
@ -42,7 +47,126 @@ 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
def results(request, ips_id):
return HttpResponse(ips_id)
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 newrandomIP(request):
ips_listr = ips2.objects.values()
if request.method == "POST":
form2 = new_ip_random(request.POST)
print(form2)
if form2.is_valid():
while True:
newIP = createIP("fd00::/8")
print("test3")
ips_listr2 = list(ips_listr)
if not ips_listr2:
print("test2")
q = ips2(inet2=newIP, inet2_name=form2.cleaned_data['NetworkName'], inet2_descrip=form2.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=form2.cleaned_data['NetworkName'], inet2_descrip=form2.cleaned_data['NetworkDescription'])
q.save()
break
else:
overlapchk = 0
if overlapchk:
break
return redirect('newrandomIP')
else:
form2 = new_ip_random()
ctx2 = { 'form2' : form2,
'ips_listr' : ips_listr,
}
return render(request, 'newrandomIP.html', ctx2)
def signup(request):
if request.user.is_authenticated:
return redirect('newrandomIP')
if request.method == 'POST':
signup_form = UserCreationForm(request.POST)
if signup_form.is_valid():
user = signup_form.save()
auth_login(request, user)
return redirect('login')
else:
print("fail singup")
else:
signup_form = UserCreationForm()
return render(request, 'signup.html', {'signup_form':signup_form})
def login(request):
if request.user.is_authenticated:
return redirect('newrandomIP')
if request.method == 'POST':
login_form = AuthenticationForm(request, request.POST)
if login_form.is_valid():
auth_login(request, login_form.get_user())
return redirect('newrandomIP')
else:
print("fail login")
else:
login_form = AuthenticationForm()
return render(request, 'login.html', {'login_form' : login_form})
def logout(request):
auth_logout(request)
return redirect('login')