mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-09-12 01:03:09 +00:00
Édition des listes de ports.
This commit is contained in:
parent
cf9db8f1d1
commit
60e42d6993
2 changed files with 61 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue