Commit 60e42d69 authored by LEVY-FALK Hugo's avatar LEVY-FALK Hugo Committed by root

Édition des listes de ports.

parent cf9db8f1
...@@ -22,9 +22,11 @@ ...@@ -22,9 +22,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import re
from django.forms import ModelForm, Form, ValidationError from django.forms import ModelForm, Form, ValidationError
from django import forms from django import forms
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType, PortList from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType, PortList, Port
from django.db.models import Q from django.db.models import Q
from django.core.validators import validate_email from django.core.validators import validate_email
...@@ -238,4 +240,57 @@ class EditPortListForm(ModelForm): ...@@ -238,4 +240,57 @@ class EditPortListForm(ModelForm):
model = PortList model = PortList
fields = ['name'] fields = ['name']
def __init__(self, *args, **kwargs):
super(EditPortListForm, self).__init__(*args, **kwargs)
self.fields['name'].label = "Nom de la liste"
if 'instance' in kwargs.keys():
p = kwargs['instance']
self.fields['tcp_ports_in'].initial = ', '.join(map(str, p.tcp_ports_in()))
self.fields['tcp_ports_out'].initial = ', '.join(map(str, p.tcp_ports_out()))
self.fields['udp_ports_in'].initial = ', '.join(map(str, p.udp_ports_in()))
self.fields['udp_ports_out'].initial = ', '.join(map(str, p.udp_ports_out()))
def save(self, commit=False):
"""
Sauvegarde l'instance. Le commit est obligatoire à cause des ForeignKey.
"""
instance = super(EditPortListForm, self).save(commit=False)
# Suppression des anciens ports.
for port in instance.port_set.all():
port.delete()
split = r',\s+'
ip_range = r'\d+-\d+'
def add_port(string, protocole, mode):
for p in re.split(split, string):
if not p:
continue
if re.match(ip_range, p):
a,b = p.split('-')
a,b = int(a), int(b)
begin,end = min(a,b),max(a,b)
else:
begin = end = int(p.strip())
port = Port()
port.begin = begin
port.end = end
port.port_list = instance
port.protocole = protocole
port.io = mode
port.save()
# Ajout des ports TCP en entrée
add_port(self.cleaned_data['tcp_ports_in'], Port.TCP, Port.IN)
# Ajout des ports TCP en sortie
add_port(self.cleaned_data['tcp_ports_out'], Port.TCP, Port.OUT)
# Ajout des ports UDP en entrée
add_port(self.cleaned_data['tcp_ports_in'], Port.UDP, Port.IN)
# Ajout des ports UDP en sortie
add_port(self.cleaned_data['tcp_ports_in'], Port.UDP, Port.OUT)
if commit:
instance.save()
return instance
...@@ -925,6 +925,11 @@ def edit_portlist(request, pk): ...@@ -925,6 +925,11 @@ def edit_portlist(request, pk):
port_list_instance = get_object_or_404(PortList, pk=pk) port_list_instance = get_object_or_404(PortList, pk=pk)
port_list = EditPortListForm(request.POST or None, instance=port_list_instance) port_list = EditPortListForm(request.POST or None, instance=port_list_instance)
if port_list.is_valid(): if port_list.is_valid():
with transaction.atomic(), reversion.create_revision():
port_list.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in port_list.changed_data))
messages.success(request, "Liste de ports modifiée")
return redirect("/machines/index_portlist/") return redirect("/machines/index_portlist/")
return form({'machineform' : port_list}, 'machines/machine.html', request) return form({'machineform' : port_list}, 'machines/machine.html', request)
......
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