Monkey patched User model. Poller js is templated. Plus minor changes
[flowspy] / flowspec / forms.py
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 from flowspy.flowspec.models import * 
7 from ipaddr import *
8 from django.contrib.auth.models import User
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         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')
57         if (sourceports and ports):
58             raise forms.ValidationError('Cannot create rule for source ports and ports at the same time. Select either ports or source ports')
59         if (destinationports and ports):
60             raise forms.ValidationError('Cannot create rule for destination ports and ports at the same time. Select either ports or destination ports')
61         if sourceports and not source:
62             raise forms.ValidationError('Once source port is matched, source has to be filled as well. Either deselect source port or fill source address')
63         if destinationports and not destination:
64             raise forms.ValidationError('Once destination port is matched, destination has to be filled as well. Either deselect destination port or fill destination address')
65         if not (source or sourceports or ports or destination or destinationports):
66             raise forms.ValidationError('Fill at least a Route Match Condition')
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')