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 *
8 from django.contrib.auth.models import User
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)
23 def clean_source(self):
24 data = self.cleaned_data['source']
27 address = IPNetwork(data)
28 return self.cleaned_data["source"]
30 raise forms.ValidationError('Invalid network address format')
32 def clean_destination(self):
33 data = self.cleaned_data['destination']
36 address = IPNetwork(data)
37 return self.cleaned_data["destination"]
39 raise forms.ValidationError('Invalid network address format')
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()
51 for network in networks:
52 net = IPNetwork(network.network)
53 if IPNetwork(destination) in net:
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
69 class ThenPlainForm(forms.ModelForm):
70 # action = forms.CharField(initial='rate-limit')
74 def clean_action_value(self):
75 action_value = self.cleaned_data['action_value']
78 assert(int(action_value))
79 return "%s" %self.cleaned_data["action_value"]
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')
85 raise forms.ValidationError('Cannot be empty')
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')
92 return self.cleaned_data["action"]
94 class PortPlainForm(forms.ModelForm):
95 # action = forms.CharField(initial='rate-limit')
100 port = self.cleaned_data['port']
104 return "%s" %self.cleaned_data["port"]
106 raise forms.ValidationError('Port should be an integer')
108 raise forms.ValidationError('Cannot be empty')