Revision 97e42c7d flowspec/forms.py
b/flowspec/forms.py | ||
---|---|---|
3 | 3 |
from django.utils.translation import ugettext as _ |
4 | 4 |
from django.utils.translation import ugettext_lazy |
5 | 5 |
from django.template.defaultfilters import filesizeformat |
6 |
|
|
7 | 6 |
from flowspy.flowspec.models import * |
8 | 7 |
from ipaddr import * |
8 |
from django.contrib.auth.models import User |
|
9 | 9 |
|
10 | 10 |
class RouteForm(forms.ModelForm): |
11 | 11 |
# name = forms.CharField(help_text=ugettext_lazy("A unique route name," |
... | ... | |
44 | 44 |
ports = self.cleaned_data.get('port', None) |
45 | 45 |
destination = self.cleaned_data.get('destination', None) |
46 | 46 |
destinationports = self.cleaned_data.get('destinationport', None) |
47 |
user = self.cleaned_data.get('applier', None) |
|
48 |
networks = user.get_profile().peer.networks.all() |
|
49 |
mynetwork = False |
|
50 |
if destination: |
|
51 |
for network in networks: |
|
52 |
net = IPNetwork(network.network) |
|
53 |
if IPNetwork(destination) in net: |
|
54 |
mynetwork = True |
|
55 |
if not mynetwork: |
|
56 |
raise forms.ValidationError('Destination address/network should belong to your administrative address space. Check My Profile to review your networks') |
|
47 | 57 |
if (sourceports and ports): |
48 | 58 |
raise forms.ValidationError('Cannot create rule for source ports and ports at the same time. Select either ports or source ports') |
49 | 59 |
if (destinationports and ports): |
... | ... | |
54 | 64 |
raise forms.ValidationError('Once destination port is matched, destination has to be filled as well. Either deselect destination port or fill destination address') |
55 | 65 |
if not (source or sourceports or ports or destination or destinationports): |
56 | 66 |
raise forms.ValidationError('Fill at least a Route Match Condition') |
57 |
return self.cleaned_data |
|
67 |
return self.cleaned_data |
|
68 |
|
|
69 |
class ThenPlainForm(forms.ModelForm): |
|
70 |
# action = forms.CharField(initial='rate-limit') |
|
71 |
class Meta: |
|
72 |
model = ThenAction |
|
73 |
|
|
74 |
def clean_action_value(self): |
|
75 |
action_value = self.cleaned_data['action_value'] |
|
76 |
if action_value: |
|
77 |
try: |
|
78 |
assert(int(action_value)) |
|
79 |
return "%s" %self.cleaned_data["action_value"] |
|
80 |
except: |
|
81 |
raise forms.ValidationError('Rate-limiting should be an integer') |
|
82 |
if int(action_value) < 50: |
|
83 |
raise forms.ValidationError('Rate-limiting cannot be < 50kbps') |
|
84 |
else: |
|
85 |
raise forms.ValidationError('Cannot be empty') |
|
86 |
|
|
87 |
def clean_action(self): |
|
88 |
action = self.cleaned_data['action'] |
|
89 |
if action != 'rate-limit': |
|
90 |
raise forms.ValidationError('Cannot select something other than rate-limit') |
|
91 |
else: |
|
92 |
return self.cleaned_data["action"] |
|
93 |
|
|
94 |
class PortPlainForm(forms.ModelForm): |
|
95 |
# action = forms.CharField(initial='rate-limit') |
|
96 |
class Meta: |
|
97 |
model = MatchPort |
|
98 |
|
|
99 |
def clean_port(self): |
|
100 |
port = self.cleaned_data['port'] |
|
101 |
if port: |
|
102 |
try: |
|
103 |
assert(int(port)) |
|
104 |
return "%s" %self.cleaned_data["port"] |
|
105 |
except: |
|
106 |
raise forms.ValidationError('Port should be an integer') |
|
107 |
else: |
|
108 |
raise forms.ValidationError('Cannot be empty') |
Also available in: Unified diff