Browse Source

Merge "Classifier to support custom IP protocol numbers"

Jenkins 3 years ago
parent
commit
5d0c13b845
2 changed files with 36 additions and 20 deletions
  1. 19
    0
      gbpui/fields.py
  2. 17
    20
      gbpui/panels/application_policy/forms.py

+ 19
- 0
gbpui/fields.py View File

@@ -12,7 +12,9 @@
12 12
 
13 13
 from django.core import urlresolvers
14 14
 from django.forms import fields
15
+from django.forms import TextInput
15 16
 from django.forms import widgets
17
+from django.utils.safestring import mark_safe
16 18
 
17 19
 
18 20
 class DynamicMultiSelectWidget(widgets.SelectMultiple):
@@ -65,3 +67,20 @@ class DynamicMultiChoiceField(fields.MultipleChoiceField):
65 67
 class CustomMultiChoiceField(DynamicMultiChoiceField):
66 68
     def validate(self, *args, **kwargs):
67 69
         return True
70
+
71
+
72
+class DropdownEditWidget(TextInput):
73
+    def __init__(self, data_list, name, *args, **kwargs):
74
+        super(DropdownEditWidget, self).__init__(*args, **kwargs)
75
+        self._name = name
76
+        self._list = data_list
77
+        self.attrs.update({'list': 'list__%s' % self._name})
78
+
79
+    def render(self, name, value, attrs=None):
80
+        text_html = super(DropdownEditWidget, self).render(
81
+            name, value, attrs=attrs)
82
+        data_list = '<datalist id="list__%s">' % self._name
83
+        for item in self._list:
84
+            data_list += '<option value="%s">' % item
85
+        data_list += '</datalist>'
86
+        return mark_safe(text_html + data_list)

+ 17
- 20
gbpui/panels/application_policy/forms.py View File

@@ -23,17 +23,10 @@ from horizon import messages
23 23
 
24 24
 from gbpui import client
25 25
 from gbpui import column_filters as gfilters
26
+from gbpui import fields
26 27
 
27
-PROTOCOLS = [('tcp', _('TCP')),
28
-             ('udp', _('UDP')),
29
-             ('icmp', _('ICMP')),
30
-             ('http', _('HTTP')),
31
-             ('https', _('HTTPS')),
32
-             ('smtp', _('SMTP')),
33
-             ('dns', _('DNS')),
34
-             ('ftp', _('FTP')),
35
-             ('any', _('ANY'))
36
-             ]
28
+PROTOCOLS = ('TCP', 'UDP', 'ICMP', 'HTTP',
29
+    'HTTPS', 'SMTP', 'DNS', 'FTP', 'ANY')
37 30
 DIRECTIONS = [('in', _('IN')),
38 31
               ('out', _('OUT')),
39 32
               ('bi', _('BI'))]
@@ -195,9 +188,7 @@ class UpdatePolicyActionForm(BaseUpdateForm):
195 188
 
196 189
 class AddPolicyClassifierForm(forms.SelfHandlingForm):
197 190
     name = forms.CharField(max_length=80, label=_("Name"), required=False)
198
-    protocol = forms.ChoiceField(label=_("Protocol"), choices=PROTOCOLS,
199
-                widget=forms.Select(attrs={'class': 'switchable',
200
-                                           'data-slug': 'source'}))
191
+    protocol = forms.CharField(required=True)
201 192
     port_range = forms.CharField(max_length=80, label=_("Port/Range(min:max)"),
202 193
                     required=False,
203 194
                     widget=forms.TextInput(attrs={'class': 'switched',
@@ -219,13 +210,16 @@ class AddPolicyClassifierForm(forms.SelfHandlingForm):
219 210
 
220 211
     def __init__(self, request, *args, **kwargs):
221 212
         super(AddPolicyClassifierForm, self).__init__(request, *args, **kwargs)
213
+        self.fields['protocol'].widget = fields.DropdownEditWidget(
214
+            data_list=PROTOCOLS, name='list')
222 215
 
223 216
     def handle(self, request, context):
224 217
         url = reverse('horizon:project:application_policy:index')
225 218
         try:
226
-            if context.get('protocol') in PROTOCOL_MAP:
227
-                context['protocol'] = PROTOCOL_MAP[context['protocol']]
228
-            elif context.get('protocol') == "any":
219
+            protocol = context.get('protocol').lower()
220
+            if protocol in PROTOCOL_MAP:
221
+                context['protocol'] = PROTOCOL_MAP[protocol]
222
+            elif protocol == "any":
229 223
                 del context['protocol']
230 224
             if not context.get('port_range'):
231 225
                 context['port_range'] = None
@@ -244,7 +238,7 @@ class AddPolicyClassifierForm(forms.SelfHandlingForm):
244 238
 class UpdatePolicyClassifierForm(BaseUpdateForm):
245 239
     name = forms.CharField(max_length=80, label=_("Name"), required=False)
246 240
     description = forms.CharField(label=_("Description"), required=False)
247
-    protocol = forms.ChoiceField(label=_("Protocol"), choices=PROTOCOLS)
241
+    protocol = forms.CharField(required=True)
248 242
     port_range = forms.CharField(max_length=80, label=_("Port/Range(min:max)"),
249 243
             required=False)
250 244
     direction = forms.ChoiceField(label=_("Direction"), choices=DIRECTIONS)
@@ -253,6 +247,8 @@ class UpdatePolicyClassifierForm(BaseUpdateForm):
253 247
     def __init__(self, request, *args, **kwargs):
254 248
         super(UpdatePolicyClassifierForm, self).__init__(
255 249
             request, *args, **kwargs)
250
+        self.fields['protocol'].widget = fields.DropdownEditWidget(
251
+            data_list=PROTOCOLS, name='list')
256 252
         try:
257 253
             policyclassifier_id = self.initial['policyclassifier_id']
258 254
             classifier = client.policyclassifier_get(
@@ -274,9 +270,10 @@ class UpdatePolicyClassifierForm(BaseUpdateForm):
274 270
         url = reverse('horizon:project:application_policy:index')
275 271
         try:
276 272
             policyclassifier_id = self.initial['policyclassifier_id']
277
-            if context.get('protocol') in PROTOCOL_MAP:
278
-                context['protocol'] = PROTOCOL_MAP[context['protocol']]
279
-            elif context.get('protocol') == "any":
273
+            protocol = context.get('protocol').lower()
274
+            if protocol in PROTOCOL_MAP:
275
+                context['protocol'] = PROTOCOL_MAP[protocol]
276
+            elif protocol == "any":
280 277
                 context['protocol'] = None
281 278
             if 'port_range' in context and context['port_range'] == '':
282 279
                 context['port_range'] = None

Loading…
Cancel
Save