19
19
20
20
from ..choices import ACLTypeChoices
21
21
from .constants import (
22
- ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET ,
23
22
ERROR_MESSAGE_NO_REMARK ,
24
23
ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ,
25
24
HELP_TEXT_ACL_ACTION ,
@@ -185,14 +184,14 @@ def clean(self):
185
184
host_types = self ._get_host_types ()
186
185
187
186
# Check if no hosts selected.
188
- self ._validate_host_types (host_types )
187
+ self ._clean_check_host_types (host_types )
189
188
190
189
host_type , host = host_types [0 ]
191
190
192
191
# Check if duplicate entry.
193
- self ._validate_duplicate_entry (name , host_type , host , error_message )
192
+ self ._clean_check_duplicate_entry (name , host_type , host , error_message )
194
193
# Check if Access List has no existing rules before change the Access List's type.
195
- self ._validate_acl_type_change (acl_type , error_message )
194
+ self ._clean_check_acl_type_change (acl_type , error_message )
196
195
197
196
if error_message :
198
197
raise forms .ValidationError (error_message )
@@ -213,9 +212,9 @@ def _get_host_types(self):
213
212
]
214
213
return [x for x in host_types if x [1 ]]
215
214
216
- def _validate_host_types (self , host_types ):
215
+ def _clean_check_host_types (self , host_types ):
217
216
"""
218
- Check number of host types selected.
217
+ Used by parent class's clean method. Check number of host types selected.
219
218
"""
220
219
if len (host_types ) > 1 :
221
220
raise forms .ValidationError (
@@ -227,9 +226,9 @@ def _validate_host_types(self, host_types):
227
226
"Access Lists must be assigned to a device, virtual chassis or virtual machine." ,
228
227
)
229
228
230
- def _validate_duplicate_entry (self , name , host_type , host , error_message ):
229
+ def _clean_check_duplicate_entry (self , name , host_type , host , error_message ):
231
230
"""
232
- Check if duplicate entry. (Because of GFK.)
231
+ Used by parent class's clean method. Check if duplicate entry. (Because of GFK.)
233
232
"""
234
233
existing_acls = AccessList .objects .filter (
235
234
name = name , ** {host_type : host }
@@ -246,9 +245,9 @@ def _validate_duplicate_entry(self, name, host_type, host, error_message):
246
245
"name" : [error_same_acl_name ],
247
246
}
248
247
249
- def _validate_acl_type_change (self , acl_type , error_message ):
248
+ def _clean_check_acl_type_change (self , acl_type , error_message ):
250
249
"""
251
- Check if Access List has no existing rules before change the Access List's type.
250
+ Used by parent class's clean method. Check if Access List has no existing rules before change the Access List's type.
252
251
"""
253
252
if self .instance .pk :
254
253
error_message ["type" ] = [
@@ -386,10 +385,10 @@ def clean(self):
386
385
cleaned_data = super ().clean ()
387
386
388
387
# Get the interface types assigned to the Access List
389
- interface_types = self ._get_interface_types ()
388
+ interface_types = self ._clean_get_interface_types ()
390
389
391
390
# Initialize an error message variable
392
- self ._validate_interface_types (interface_types )
391
+ self ._clean_check_interface_types (interface_types )
393
392
394
393
# Get the assigned interface & interface type
395
394
assigned_object_type , assigned_object = interface_types [0 ]
@@ -404,12 +403,12 @@ def clean(self):
404
403
assigned_object_type_id = ContentType .objects .get_for_model (assigned_object ).pk
405
404
406
405
# Check if the parent host is assigned to the Access List
407
- self ._validate_if_interface_parent_is_assigned_to_access_list (
406
+ self ._clean_check_if_interface_parent_is_assigned_to_access_list (
408
407
cleaned_data .get ("access_list" ), assigned_object_type , assigned_object
409
408
)
410
409
411
410
# Check for duplicate entries in the Access List
412
- self ._validate_if_interface_already_has_acl_in_direction (
411
+ self ._clean_check_if_interface_already_has_acl_in_direction (
413
412
cleaned_data .get ("access_list" ),
414
413
assigned_object_id ,
415
414
assigned_object_type ,
@@ -419,9 +418,9 @@ def clean(self):
419
418
420
419
return cleaned_data
421
420
422
- def _get_interface_types (self ):
421
+ def _clean_get_interface_types (self ):
423
422
"""
424
- Get interface type/model assigned to the Access List.
423
+ Used by parent class's clean method. Get interface type/model assigned to the Access List.
425
424
"""
426
425
interface = self .cleaned_data .get ("interface" )
427
426
vminterface = self .cleaned_data .get ("vminterface" )
@@ -431,9 +430,9 @@ def _get_interface_types(self):
431
430
]
432
431
return [x for x in interface_types if x [1 ]]
433
432
434
- def _validate_interface_types (self , interface_types ):
433
+ def _clean_check_interface_types (self , interface_types ):
435
434
"""
436
- Check if number of interface type selected is 1.
435
+ Used by parent class's clean method. Check if number of interface type selected is 1.
437
436
"""
438
437
# Check if more than 1 hosts selected.
439
438
if len (interface_types ) > 1 :
@@ -444,11 +443,11 @@ def _validate_interface_types(self, interface_types):
444
443
elif not interface_types :
445
444
raise forms .ValidationError ("No interface or vminterface selected." )
446
445
447
- def _validate_if_interface_parent_is_assigned_to_access_list (
446
+ def _clean_check_if_interface_parent_is_assigned_to_access_list (
448
447
self , access_list , assigned_object_type , assigned_object
449
448
):
450
449
"""
451
- Check that an interface's parent device/virtual_machine is assigned to the Access List.
450
+ Used by parent class's clean method. Check that an interface's parent device/virtual_machine is assigned to the Access List.
452
451
"""
453
452
access_list_host = AccessList .objects .get (pk = access_list .pk ).assigned_object
454
453
host_type = (
@@ -469,7 +468,7 @@ def _validate_if_interface_parent_is_assigned_to_access_list(
469
468
}
470
469
)
471
470
472
- def _validate_if_interface_already_has_acl_in_direction (
471
+ def _clean_check_if_interface_already_has_acl_in_direction (
473
472
self ,
474
473
access_list ,
475
474
assigned_object_id ,
@@ -489,8 +488,9 @@ def _validate_if_interface_already_has_acl_in_direction(
489
488
direction = direction ,
490
489
).exists ():
491
490
raise forms .ValidationError ({"access_list" : ["Duplicate entry." ]})
491
+
492
492
# Check that the interface does not have an existing ACL applied in the direction already.
493
- elif ACLInterfaceAssignment .objects .filter (
493
+ if ACLInterfaceAssignment .objects .filter (
494
494
assigned_object_id = assigned_object_id ,
495
495
assigned_object_type = assigned_object_type_id ,
496
496
direction = direction ,
@@ -571,7 +571,7 @@ def clean(self):
571
571
# No need to check for unique_together since there is no usage of GFK
572
572
573
573
if cleaned_data .get ("action" ) == "remark" :
574
- self ._validate_acl_rules (cleaned_data , error_message , "extended" )
574
+ self ._clean_check_acl_rules (cleaned_data , error_message , "extended" )
575
575
# Check remark set, but action not set to remark.
576
576
elif cleaned_data .get ("remark" ):
577
577
error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
@@ -580,45 +580,33 @@ def clean(self):
580
580
raise forms .ValidationError (error_message )
581
581
return cleaned_data
582
582
583
- def _validate_acl_rules (self , cleaned_data , error_message , rule_type ):
583
+ def _clean_check_acl_rules (self , cleaned_data , error_message , rule_type ):
584
584
"""
585
- Validates form inputs before submitting:
586
- - Check if action set to remark, but no remark set.
587
- - Check if action set to remark, but source_prefix set.
588
- - Check if action set to remark, but source_ports set.
589
- - Check if action set to remark, but destination_prefix set.
590
- - Check if action set to remark, but destination_ports set.
591
- - Check if action set to remark, but destination_ports set.
592
- - Check if action set to remark, but protocol set.
585
+ Used by parent class's clean method. Checks form inputs before submitting:
586
+ - Check if action set to remark, but no remark set.
587
+ - Check if action set to remark, but other rule attributes set.
593
588
"""
594
589
# Check if action set to remark, but no remark set.
595
590
if not cleaned_data .get ("remark" ):
596
591
error_message ["remark" ] = [ERROR_MESSAGE_NO_REMARK ]
597
- # Check if action set to remark, but source_prefix set.
598
- if cleaned_data .get ("source_prefix" ):
599
- error_message ["source_prefix" ] = [
600
- ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET
601
- ]
592
+
593
+ # list all the fields of a rule besides the remark
602
594
if rule_type == "extended" :
603
- # Check if action set to remark, but source_ports set.
604
- if cleaned_data .get ("source_ports" ):
605
- error_message ["source_ports" ] = [
606
- "Action is set to remark, Source Ports CANNOT be set."
607
- ]
608
- # Check if action set to remark, but destination_prefix set.
609
- if cleaned_data .get ("destination_prefix" ):
610
- error_message ["destination_prefix" ] = [
611
- "Action is set to remark, Destination Prefix CANNOT be set." ,
612
- ]
613
- # Check if action set to remark, but destination_ports set.
614
- if cleaned_data .get ("destination_ports" ):
615
- error_message ["destination_ports" ] = [
616
- "Action is set to remark, Destination Ports CANNOT be set."
617
- ]
618
- # Check if action set to remark, but protocol set.
619
- if cleaned_data .get ("protocol" ):
620
- error_message ["protocol" ] = [
621
- "Action is set to remark, Protocol CANNOT be set."
595
+ rule_attributes = [
596
+ "source_prefix" ,
597
+ "source_ports" ,
598
+ "destination_prefix" ,
599
+ "destination_ports" ,
600
+ "protocol" ,
601
+ ]
602
+ else :
603
+ rule_attributes = ["source_prefix" ]
604
+
605
+ # Check if action set to remark, but other fields set.
606
+ for attribute in rule_attributes :
607
+ if cleaned_data .get (attribute ):
608
+ error_message [attribute ] = [
609
+ f'Action is set to remark, { attribute .replace ("_" , " " ).title ()} CANNOT be set.'
622
610
]
623
611
624
612
0 commit comments