root / flowspec / forms.py @ 9cad4715
History | View | Annotate | Download (3.5 kB)
1 |
from django import forms |
---|---|
2 |
from django.utils.safestring import mark_safe |
3 |
from django.utils.translation import ugettext as _ |
4 |
from django.utils.translation import ugettext_lazy |
5 |
from django.template.defaultfilters import filesizeformat |
6 |
|
7 |
from flowspy.flowspec.models import * |
8 |
from ipaddr import * |
9 |
|
10 |
class RouteForm(forms.ModelForm): |
11 |
# name = forms.CharField(help_text=ugettext_lazy("A unique route name,"
|
12 |
# " e.g. uoa_block_p80"), label=ugettext_lazy("Route Name"), required=False)
|
13 |
# source = forms.CharField(help_text=ugettext_lazy("A qualified IP Network address. CIDR notation,"
|
14 |
# " e.g.10.10.0.1/32"), label=ugettext_lazy("Source Address"), required=False)
|
15 |
# source_ports = forms.ModelMultipleChoiceField(queryset=MatchPort.objects.all(), help_text=ugettext_lazy("A set of source ports to block"), label=ugettext_lazy("Source Ports"), required=False)
|
16 |
# destination = forms.CharField(help_text=ugettext_lazy("A qualified IP Network address. CIDR notation,"
|
17 |
# " e.g.10.10.0.1/32"), label=ugettext_lazy("Destination Address"), required=False)
|
18 |
# destination_ports = forms.ModelMultipleChoiceField(queryset=MatchPort.objects.all(), help_text=ugettext_lazy("A set of destination ports to block"), label=ugettext_lazy("Destination Ports"), required=False)
|
19 |
# ports = forms.ModelMultipleChoiceField(queryset=MatchPort.objects.all(), help_text=ugettext_lazy("A set of ports to block"), label=ugettext_lazy("Ports"), required=False)
|
20 |
class Meta: |
21 |
model = Route |
22 |
|
23 |
def clean_source(self): |
24 |
data = self.cleaned_data['source'] |
25 |
if data:
|
26 |
try:
|
27 |
address = IPNetwork(data) |
28 |
return self.cleaned_data["source"] |
29 |
except Exception: |
30 |
raise forms.ValidationError('Invalid network address format') |
31 |
|
32 |
def clean_destination(self): |
33 |
data = self.cleaned_data['destination'] |
34 |
if data:
|
35 |
try:
|
36 |
address = IPNetwork(data) |
37 |
return self.cleaned_data["destination"] |
38 |
except Exception: |
39 |
raise forms.ValidationError('Invalid network address format') |
40 |
|
41 |
def clean(self): |
42 |
source = self.cleaned_data.get('source', None) |
43 |
sourceports = self.cleaned_data.get('sourceport', None) |
44 |
ports = self.cleaned_data.get('port', None) |
45 |
destination = self.cleaned_data.get('destination', None) |
46 |
destinationports = self.cleaned_data.get('destinationport', None) |
47 |
if (sourceports and ports): |
48 |
raise forms.ValidationError('Cannot create rule for source ports and ports at the same time. Select either ports or source ports') |
49 |
if (destinationports and ports): |
50 |
raise forms.ValidationError('Cannot create rule for destination ports and ports at the same time. Select either ports or destination ports') |
51 |
if sourceports and not source: |
52 |
raise forms.ValidationError('Once source port is matched, source has to be filled as well. Either deselect source port or fill source address') |
53 |
if destinationports and not destination: |
54 |
raise forms.ValidationError('Once destination port is matched, destination has to be filled as well. Either deselect destination port or fill destination address') |
55 |
if not (source or sourceports or ports or destination or destinationports): |
56 |
raise forms.ValidationError('Fill at least a Route Match Condition') |
57 |
return self.cleaned_data |