8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-28 01:43:46 +00:00

Renomage des tables, ecriture des admin et fioritures

This commit is contained in:
Gabriel Detraz 2017-10-02 04:19:07 +02:00 committed by root
parent 5f511eaf3f
commit c459d3f45c
8 changed files with 60 additions and 98 deletions

View file

@ -26,7 +26,9 @@ from __future__ import unicode_literals
from django.contrib import admin
from reversion.admin import VersionAdmin
from .models import IpType, Machine, MachineType, Domain, IpList, Interface, Extension, Mx, Ns, Vlan, Text, Nas, Service
from .models import IpType, Machine, MachineType, Domain, IpList, Interface
from .models import Extension, Mx, Ns, Vlan, Text, Nas, Service, OuverturePort
from .models import OuverturePortList
class MachineAdmin(VersionAdmin):
pass
@ -58,6 +60,12 @@ class NasAdmin(VersionAdmin):
class IpListAdmin(VersionAdmin):
pass
class OuverturePortAdmin(VersionAdmin):
pass
class OuverturePortListAdmin(VersionAdmin):
pass
class InterfaceAdmin(VersionAdmin):
list_display = ('machine','type','mac_address','ipv4','details')
@ -80,3 +88,7 @@ admin.site.register(Domain, DomainAdmin)
admin.site.register(Service, ServiceAdmin)
admin.site.register(Vlan, VlanAdmin)
admin.site.register(Nas, NasAdmin)
admin.site.register(OuverturePort, OuverturePortAdmin)
admin.site.register(OuverturePortList, OuverturePortListAdmin)

View file

@ -26,7 +26,7 @@ import re
from django.forms import ModelForm, Form, ValidationError
from django import forms
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType, PortList, Port
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType, OuverturePortList, OuverturePort
from django.db.models import Q
from django.core.validators import validate_email
@ -232,13 +232,13 @@ class VlanForm(ModelForm):
class DelVlanForm(Form):
vlan = forms.ModelMultipleChoiceField(queryset=Vlan.objects.all(), label="Vlan actuels", widget=forms.CheckboxSelectMultiple)
class EditPortConfigForm(ModelForm):
class EditOuverturePortConfigForm(ModelForm):
class Meta:
model = Interface
fields = ['port_lists']
class EditPortListForm(ModelForm):
class EditOuverturePortListForm(ModelForm):
class Meta:
model = PortList
model = OuverturePortList
fields = '__all__'

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-09-28 15:11
# Generated by Django 1.10.7 on 2017-10-02 01:50
from __future__ import unicode_literals
from django.db import migrations, models
@ -14,24 +14,30 @@ class Migration(migrations.Migration):
operations = [
migrations.CreateModel(
name='Port',
name='OuverturePort',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('begin', models.IntegerField()),
('end', models.IntegerField()),
('protocole', models.CharField(choices=[('T', 'TCP'), ('U', 'UDP')], default='T', max_length=1)),
('io', models.CharField(choices=[('I', 'IN'), ('O', 'OUT')], default='O', max_length=1)),
],
),
migrations.CreateModel(
name='PortList',
name='OuverturePortList',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Nom de la configuration des ports.', max_length=255)),
('interfaces', models.ManyToManyField(to='machines.Interface')),
],
),
migrations.AddField(
model_name='port',
model_name='ouvertureport',
name='port_list',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='machines.PortList'),
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='machines.OuverturePortList'),
),
migrations.AddField(
model_name='interface',
name='port_lists',
field=models.ManyToManyField(blank=True, to='machines.OuverturePortList'),
),
]

View file

@ -1,20 +0,0 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-09-28 16:03
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('machines', '0058_auto_20170928_1711'),
]
operations = [
migrations.AddField(
model_name='port',
name='protocole',
field=models.CharField(choices=[('T', 'TCP'), ('U', 'UDP')], default='T', max_length=1),
),
]

View file

@ -1,20 +0,0 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-10-01 09:30
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('machines', '0059_port_protocole'),
]
operations = [
migrations.AddField(
model_name='port',
name='io',
field=models.CharField(choices=[('I', 'IN'), ('O', 'OUT')], default='O', max_length=1),
),
]

View file

@ -1,24 +0,0 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-10-01 15:27
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('machines', '0060_port_io'),
]
operations = [
migrations.RemoveField(
model_name='portlist',
name='interfaces',
),
migrations.AddField(
model_name='interface',
name='port_lists',
field=models.ManyToManyField(to='machines.PortList'),
),
]

View file

@ -223,7 +223,7 @@ class Interface(models.Model):
machine = models.ForeignKey('Machine', on_delete=models.CASCADE)
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
details = models.CharField(max_length=255, blank=True)
port_lists = models.ManyToManyField('PortList', blank=True)
port_lists = models.ManyToManyField('OuverturePortList', blank=True)
@cached_property
def is_active(self):
@ -280,8 +280,13 @@ class Interface(models.Model):
return str(domain)
def has_private_ip(self):
return IPAddress(str(self.ipv4)).is_private()
if hasattr(self, 'ipv4'):
return IPAddress(str(self.ipv4)).is_private()
else:
return False
def may_have_port_open(self):
return hasattr(self, 'ipv4') and self.has_private_ip()
class Domain(models.Model):
PRETTY_NAME = "Domaine dns"
@ -412,7 +417,7 @@ class Service_link(models.Model):
return str(self.server) + " " + str(self.service)
class PortList(models.Model):
class OuverturePortList(models.Model):
"""Liste des ports ouverts sur une interface."""
name = models.CharField(help_text="Nom de la configuration des ports.", max_length=255)
@ -420,19 +425,19 @@ class PortList(models.Model):
return self.name
def tcp_ports_in(self):
return self.port_set.filter(protocole=Port.TCP, io=Port.IN)
return self.ouvertureport_set.filter(protocole=OuverturePort.TCP, io=OuverturePort.IN)
def udp_ports_in(self):
return self.port_set.filter(protocole=Port.UDP, io=Port.IN)
return self.ouvertureport_set.filter(protocole=OuverturePort.UDP, io=OuverturePort.IN)
def tcp_ports_out(self):
return self.port_set.filter(protocole=Port.TCP, io=Port.OUT)
return self.ouvertureport_set.filter(protocole=OuverturePort.TCP, io=OuverturePort.OUT)
def udp_ports_out(self):
return self.port_set.filter(protocole=Port.UDP, io=Port.OUT)
return self.ouvertureport_set.filter(protocole=OuverturePort.UDP, io=OuverturePort.OUT)
class Port(models.Model):
class OuverturePort(models.Model):
"""
Représente un simple port ou une plage de ports.
@ -445,7 +450,7 @@ class Port(models.Model):
OUT = 'O'
begin = models.IntegerField()
end = models.IntegerField()
port_list = models.ForeignKey('PortList', on_delete=models.CASCADE)
port_list = models.ForeignKey('OuverturePortList', on_delete=models.CASCADE)
protocole = models.CharField(
max_length=1,
choices=(
@ -492,6 +497,9 @@ def interface_post_save(sender, **kwargs):
interface = kwargs['instance']
user = interface.machine.user
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
if interface.may_have_port_open() and interface.port_lists.all():
interface.port_lists.clear()
# Regen services
regen('dhcp')
regen('mac_ip_list')

View file

@ -48,8 +48,8 @@ from reversion.models import Version
import re
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
from .forms import EditIpTypeForm, IpTypeForm, DelIpTypeForm, DomainForm, AliasForm, DelAliasForm, NsForm, DelNsForm, TextForm, DelTextForm, MxForm, DelMxForm, VlanForm, DelVlanForm, ServiceForm, DelServiceForm, NasForm, DelNasForm
from .forms import EditPortListForm, EditPortConfigForm
from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain, Service, Service_link, Vlan, Nas, Text, PortList, Port
from .forms import EditOuverturePortListForm, EditOuverturePortConfigForm
from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain, Service, Service_link, Vlan, Nas, Text, OuverturePortList, OuverturePort
from users.models import User
from users.models import all_has_access
from preferences.models import GeneralOption, OptionalMachine
@ -916,20 +916,20 @@ def history(request, object, id):
@login_required
@permission_required('cableur')
def index_portlist(request):
port_list = PortList.objects.all().order_by('name')
port_list = OuverturePortList.objects.all().order_by('name')
return render(request, "machines/index_portlist.html", {'port_list':port_list})
@login_required
@permission_required('bureau')
def edit_portlist(request, pk):
try:
port_list_instance = PortList.objects.get(pk=pk)
except PortList.DoesNotExist:
port_list_instance = OuverturePortList.objects.get(pk=pk)
except OuverturePortList.DoesNotExist:
messages.error(request, "Liste de ports inexistante")
return redirect("/machines/index_portlist/")
port_list = EditPortListForm(request.POST or None, instance=port_list_instance)
port_list = EditOuverturePortListForm(request.POST or None, instance=port_list_instance)
port_formset = modelformset_factory(
Port,
OuverturePort,
fields=('begin','end','protocole','io'),
extra=0,
can_delete=True,
@ -952,8 +952,8 @@ def edit_portlist(request, pk):
@permission_required('bureau')
def del_portlist(request, pk):
try:
port_list_instance = PortList.objects.get(pk=pk)
except PortList.DoesNotExist:
port_list_instance = OuverturePortList.objects.get(pk=pk)
except OuverturePortList.DoesNotExist:
messages.error(request, "Liste de ports inexistante")
return redirect("/machines/index_portlist/")
if port_list_instance.interface_set.all():
@ -966,15 +966,15 @@ def del_portlist(request, pk):
@login_required
@permission_required('bureau')
def add_portlist(request):
port_list = EditPortListForm(request.POST or None)
port_list = EditOuverturePortListForm(request.POST or None)
port_formset = modelformset_factory(
Port,
OuverturePort,
fields=('begin','end','protocole','io'),
extra=0,
can_delete=True,
min_num=1,
validate_min=True,
)(request.POST or None, queryset=Port.objects.none())
)(request.POST or None, queryset=OuverturePort.objects.none())
if port_list.is_valid() and port_formset.is_valid():
pl = port_list.save()
instances = port_formset.save(commit=False)
@ -986,7 +986,7 @@ def add_portlist(request):
messages.success(request, "Liste de ports créée")
return redirect("/machines/index_portlist/")
return form({'port_list' : port_list, 'ports' : port_formset}, 'machines/edit_portlist.html', request)
port_list = EditPortListForm(request.POST or None)
port_list = EditOuverturePortListForm(request.POST or None)
if port_list.is_valid():
port_list.save()
messages.success(request, "Liste de ports créée")
@ -1001,7 +1001,7 @@ def configure_ports(request, pk):
except Interface.DoesNotExist:
messages.error(request, u"Interface inexistante" )
return redirect("/machines")
interface = EditPortConfigForm(request.POST or None, instance=interface_instance)
interface = EditOuverturePortConfigForm(request.POST or None, instance=interface_instance)
if interface.is_valid():
interface.save()
messages.success(request, "Configuration des ports mise à jour.")