From 60e42d699338002c4b93bef503364865bdad7d07 Mon Sep 17 00:00:00 2001 From: LEVY-FALK Hugo Date: Sun, 1 Oct 2017 16:42:55 +0200 Subject: [PATCH] =?UTF-8?q?=C3=89dition=20des=20listes=20de=20ports.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machines/forms.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++- machines/views.py | 5 +++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/machines/forms.py b/machines/forms.py index a611fa98..dec2cad2 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -22,9 +22,11 @@ from __future__ import unicode_literals +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 +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.core.validators import validate_email @@ -238,4 +240,57 @@ class EditPortListForm(ModelForm): model = PortList 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 diff --git a/machines/views.py b/machines/views.py index 32b04c2e..a35ad79d 100644 --- a/machines/views.py +++ b/machines/views.py @@ -925,6 +925,11 @@ def edit_portlist(request, pk): port_list_instance = get_object_or_404(PortList, pk=pk) port_list = EditPortListForm(request.POST or None, instance=port_list_instance) 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 form({'machineform' : port_list}, 'machines/machine.html', request)