Commit df03fc5a authored by brdunn's avatar brdunn
Browse files

Merge branch 'modelviewset_test' into 'master'

Modelviewset test

See merge request !8
parents 95068951 3d5c040d
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
}
]
}
\ No newline at end of file
python: can't open file 'manage.py': [Errno 2] No such file or directory
from django.contrib import admin
from .models import Record, Host, Adapter, Mac_Addr, User, Role, Preamble, Subnet, IP
from . import models
# Register your models here.
admin.site.register(Record)
admin.site.register(Host)
admin.site.register(Adapter)
admin.site.register(Mac_Addr)
admin.site.register(Role)
admin.site.register(Preamble)
admin.site.register(Subnet)
admin.site.register(IP)
\ No newline at end of file
admin.site.register(models.IP)
admin.site.register(models.Subnet)
admin.site.register(models.Preamble)
admin.site.register(models.Role)
admin.site.register(models.Mac)
admin.site.register(models.Adapter)
admin.site.register(models.Record)
admin.site.register(models.Host)
\ No newline at end of file
<<<<<<< HEAD
# Generated by Django 2.2.4 on 2019-09-04 16:24
=======
# Generated by Django 2.2 on 2019-05-12 19:59
>>>>>>> 95068951492bb06b2a04cd6fb5a4f2d14b164638
from django.conf import settings
from django.db import migrations, models
......@@ -64,12 +68,12 @@ class Migration(migrations.Migration):
name='Record',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('record_type', models.CharField(choices=[('A', 'A'), ('AAAA', 'AAAA'), ('ALIAS', 'ALIAS'), ('cname', 'cname'), ('MX', 'MX'), ('NS', 'NS'), ('PTR', 'PTR'), ('SOA', 'SOA')], max_length=12)),
('host', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='records', to='ipam.Host')),
('record_type', models.CharField(choices=[('A', 'A'), ('AAAA', 'AAAA'), ('ALIAS', 'ALIAS'), ('cname', 'cname'), ('MX', 'MX'), ('NS', 'NS'), ('PTR', 'PTR'), ('SOA', 'SOA')], max_length=8)),
('host', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='host', to='ipam.Host')),
],
),
migrations.CreateModel(
name='Mac_Addr',
name='Mac',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('mac', models.CharField(max_length=18)),
......@@ -82,7 +86,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ipv4', models.CharField(blank=True, max_length=12, null=True)),
('ipv6', models.CharField(blank=True, max_length=40, null=True)),
('mac_addr', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='IP', to='ipam.Mac_Addr')),
('mac_addr', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='IP', to='ipam.Mac')),
('subnet', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='IP', to='ipam.Subnet')),
],
),
......
......@@ -3,38 +3,35 @@ from django.contrib.auth.models import User
# Create your models here.
class Host(models.Model):
host_name = models.CharField(max_length=256, default='Host')
host_name = models.CharField(default='Host', max_length=256)
def __str__(self):
return self.host_name + ', id: ' + str(self.id)
return self.host_name + '\n id: ' + str(self.id)
class Record(models.Model):
RECORD_TYPES = (('A', 'A'), ('AAAA', 'AAAA'), ('ALIAS', 'ALIAS'), ('cname', 'cname'),
('MX', 'MX'), ('NS', 'NS'), ('PTR', "PTR"), ('SOA', 'SOA'))
record_type = models.CharField(
max_length=12,
choices=RECORD_TYPES,
)
host = models.ForeignKey(Host, related_name='records', on_delete=models.CASCADE)
record_type = models.CharField(choices=RECORD_TYPES, max_length=8)
host = models.ForeignKey(Host, related_name='host', on_delete=models.CASCADE)
def __str__(self):
return self.record_type + ', id: ' + str(self.id)
return self.record_type + '\n id: ' + str(self.id)
class Adapter(models.Model):
host = models.ForeignKey(Host, related_name='adapter', on_delete=models.CASCADE)
adapter_name = models.CharField(max_length=50, default='Adapter')
def __str__(self):
return self.adapter_name + ', id: ' + str(self.id)
return self.adapter_name + '\n id: ' + str(self.id)
class Mac_Addr(models.Model):
class Mac(models.Model):
adapter = models.OneToOneField(Adapter, related_name='mac',on_delete=models.CASCADE)
mac = models.CharField(max_length=18)
def __str__(self):
return self.mac + ', id: ' + str(self.id)
return self.mac + '\n id: ' + str(self.id)
class Role(models.Model):
user = models.ManyToManyField(User)
role_name = models.CharField(max_length=50, default='Role')
def __str__(self):
return self.role_name + ', id: ' + str(self.id)
return self.role_name + '\n id: ' + str(self.id)
class Preamble(models.Model):
pa_subnet_name = models.CharField(max_length=50, default='pre')
......@@ -47,22 +44,22 @@ class Preamble(models.Model):
refresh = models.CharField(max_length=50)
nxdomain_ttl = models.CharField(max_length=50)
def __str__(self):
return self.pa_subnet_name + ', id: ' + str(self.id)
return self.pa_subnet_name + '\n id: ' + str(self.id)
class Subnet(models.Model):
preamble = models.OneToOneField(Preamble, related_name='subnet',on_delete=models.CASCADE)
role = models.OneToOneField(Role, related_name='subnet', on_delete=models.SET_NULL, blank=True, null=True)
subnet_name = models.CharField(max_length=128, default='Sub')
def __str__(self):
return self.subnet_name + ', id: ' + str(self.id)
return self.subnet_name + '\n id: ' + str(self.id)
class IP(models.Model):
mac_addr = models.OneToOneField(Mac_Addr, related_name='IP', on_delete=models.SET_NULL, blank=True, null=True)
mac_addr = models.OneToOneField(Mac, related_name='IP', on_delete=models.SET_NULL, blank=True, null=True)
subnet = models.ForeignKey(Subnet, related_name='IP', on_delete=models.DO_NOTHING)
ipv4 = models.CharField(max_length=12, blank=True, null=True)
ipv6 = models.CharField(max_length=40, blank=True, null=True)
def __str__(self):
if self.ipv4 is not None:
return self.ipv4 + ', id: ' + str(self.id)
return self.ipv4 + '\n id: ' + str(self.id)
if self.ipv6 is not None:
return self.ipv6 + ', id: ' + str(self.id)
return self.ipv6 + '\n id: ' + str(self.id)
from rest_framework import serializers
from .models import Record, Host, Adapter, Mac_Addr, Role, Preamble, Subnet, IP
from . import models
class RecordSerializer(serializers.ModelSerializer):
class HostSerializer(serializers.ModelSerializer):
class Meta:
model = Record
model = models.Host
fields = '__all__'
class HostSerializer(serializers.ModelSerializer):
class RecordSerializer(serializers.ModelSerializer):
class Meta:
model = Host
model = models.Record
fields = '__all__'
class AdapterSerializer(serializers.ModelSerializer):
class Meta:
model = Adapter
model = models.Adapter
fields = '__all__'
class MacAddrSerializer(serializers.ModelSerializer):
class MacSerializer(serializers.ModelSerializer):
class Meta:
model = Mac_Addr
model = models.Mac
fields = '__all__'
class RoleSerializer(serializers.ModelSerializer):
class SubnetSerializer(serializers.ModelSerializer):
class Meta:
model = Role
model = models.Subnet
fields = '__all__'
class PreambleSerializer(serializers.ModelSerializer):
class Meta:
model = Preamble
fields = '__all__'
class SubnetSerializer(serializers.ModelSerializer):
class Meta:
model = Subnet
model = models.Preamble
fields = '__all__'
class IPSerializer(serializers.ModelSerializer):
mac_addr = serializers.PrimaryKeyRelatedField(queryset=models.Mac.objects.all(), read_only=False)
class Meta:
model = IP
model = models.IP
fields = '__all__'
depth = 5
class IPUpdateSerializer(serializers.ModelSerializer):
mac_addr = serializers.PrimaryKeyRelatedField(queryset=Mac_Addr.objects.all(), read_only=False)
class Meta:
model = IP
fields = ('mac_addr', 'subnet', 'ipv4', 'ipv6')
depth = 1
def update(self, instance, validation_data):
print(validation_data)
print(instance.mac_addr)
instance.mac_addr = validation_data.get('mac_addr', instance.mac_addr)
print(instance.subnet)
instance.subnet = validation_data.get('subnet', instance.subnet)
instance.ipv4 = validation_data.get('ipv4', instance.ipv4)
instance.ipv6 = validation_data.get('ipv6', instance.ipv6)
instance.save()
return instance
class SubentSerializer(serializers.ModelSerializer):
class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = Subnet
model = models.Role
fields = '__all__'
depth = 3
from rest_framework import generics, permissions
from . import models
from . import serializers
from django.http import HttpRequest
from rest_framework.response import Response
from django.shortcuts import render
from rest_framework import viewsets, response, decorators
from . import serializers, models
from django.http import HttpResponse
request = HttpRequest()
request = HttpResponse()
class ListAvailaleIPs(generics.ListAPIView):
queryset = models.IP.objects.all().filter(mac_addr = None)
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.IPSerializer
class CreateAndListIps(generics.ListCreateAPIView):
queryset = models.IP.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.IPSerializer
class ListSubnet(generics.ListAPIView):
queryset = models.Subnet.objects.all()
#permission_class = (permissions.IsAuthenticated)
serializer_class = serializers.SubnetSerializer
class ListIPRange(generics.ListAPIView):
queryset = models.IP.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.IPSerializer
class CreateRecord(generics.CreateAPIView):
queryset = models.Record.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.RecordSerializer
class CreateHost(generics.ListCreateAPIView):
queryset = models.Host.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.HostSerializer
class CreateAdapter(generics.CreateAPIView):
queryset = models.Adapter.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.AdapterSerializer
class CreateMac(generics.CreateAPIView):
queryset = models.Mac_Addr.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.MacAddrSerializer
class UpdateIp(generics.UpdateAPIView):
queryset = models.IP.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.IPUpdateSerializer
class DeleteHost(generics.DestroyAPIView):
# Create your views here.
class HostViewset(viewsets.ModelViewSet):
queryset = models.Host.objects.all()
# permission_classes - (permissions.IsAuthenticated)
serializer_class = serializers.HostSerializer
class UpdateRecord(generics.UpdateAPIView):
class RecordViewset(viewsets.ModelViewSet):
queryset = models.Record.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.RecordSerializer
class UpdateHost(generics.UpdateAPIView):
queryset = models.Host.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.HostSerializer
class UpdateAdapter(generics.UpdateAPIView):
class AdapterViewset(viewsets.ModelViewSet):
queryset = models.Adapter.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.AdapterSerializer
class UpdateMac(generics.UpdateAPIView):
queryset = models.Mac_Addr.objects.all()
# permission_classes = (permissions.IsAuthenticated)
serializer_class = serializers.MacAddrSerializer
class MacViewset(viewsets.ModelViewSet):
queryset = models.Mac.objects.all()
serializer_class = serializers.MacSerializer
class ListRecord(generics.ListAPIView):
queryset = models.Record.objects.all()
#permission_class = (permissions.IsAuthenticated)
serializer_class = serializers.RecordSerializer
\ No newline at end of file
class RoleViewset(viewsets.ModelViewSet):
queryset = models.Role.objects.all()
serializer_class = serializers.RoleSerializer
class PreambleViewset(viewsets.ModelViewSet):
queryset = models.Preamble.objects.all()
serializer_class = serializers.PreambleSerializer
class SubnetViewset(viewsets.ModelViewSet):
queryset = models.Subnet.objects.all()
serializer_class = serializers.SubnetSerializer
class IPViewset(viewsets.ModelViewSet):
queryset = models.IP.objects.all()
serializer_class = serializers.IPSerializer
......@@ -20,13 +20,15 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1h8$gvb-qgi6=fp)%kgj!rv79d00@pz1cs6(=gs602hmrj0yv4'
SECRET_KEY = 'ryje5w$tb+4i-c^%86r!**sqeyn&-^&s)3%y8w7683x^7260q-'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['viper.cs.ksu.edu', 'localhost', '127.0.0.1', '10.131.188.80', '10.131.83.253']
CORS_ORIGIN_ALLOW_ALL = True
ALLOWED_HOSTS = ['localhost']
CORS_ORIGIN_WHITELIST = (
'https://localhost:3000',
)
# Application definition
......@@ -44,6 +46,7 @@ INSTALLED_APPS = [
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
......@@ -51,8 +54,6 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
ROOT_URLCONF = 'ipam_proj.urls'
......@@ -115,7 +116,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Chicago'
TIME_ZONE = 'UTC'
USE_I18N = True
......
from django.contrib import admin
from django.conf.urls import include, url
import ipam.views as views
from django.conf.urls import url
from django.urls import path, include
from rest_framework.routers import DefaultRouter
import ipam.views as views
router = DefaultRouter()
router.register('listallips', views.IPViewset)
router.register('createhost', views.HostViewset)
router.register('createrecord', views.RecordViewset)
router.register('createadapter', views.AdapterViewset)
router.register('createmac', views.MacViewset)
router.register('updateip', views.IPViewset)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^ipam/listavailaleips/', view=views.ListAvailaleIPs.as_view()),
url(r'^ipam/listallips/', view=views.CreateAndListIps.as_view()),
url(r'^ipam/listsubnets/', view=views.ListSubnet.as_view()),
url(r'^ipam/listrecords/', view=views.ListRecord.as_view()),
url(r'^ipam/createrecord/', view=views.CreateRecord.as_view()),
url(r'^ipam/createhost/', view=views.CreateHost.as_view()),
url(r'^ipam/createadapter/', view=views.CreateAdapter.as_view()),
url(r'^ipam/createmac/', view=views.CreateMac.as_view()),
url(r'^ipam/updateip/(?P<pk>\d+)/$', view=views.UpdateIp.as_view()),
url(r'^ipam/deletehost/(?P<pk>\d+)/$', view=views.DeleteHost.as_view()),
url(r'^ipam/updaterecord/(?P<pk>\d+)/$', view=views.UpdateRecord.as_view()),
url(r'^ipam/updatehost/(?P<pk>\d+)/$', view=views.UpdateHost.as_view()),
url(r'^ipam/updateadapter/(?P<pk>\d+)/$', view=views.UpdateAdapter.as_view()),
url(r'^ipam/updatemac/(?P<pk>\d+)/$', view=views.UpdateMac.as_view()),
path('admin/', admin.site.urls),
path('', include(router.urls))
]
#(?P<id>\d+)/$
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment