@@ -391,54 +391,35 @@ def clean(self):
391
391
interface_types = self ._get_interface_types ()
392
392
393
393
# Initialize an error message variable
394
- error_message = self ._validate_interface_types (interface_types )
394
+ self ._validate_interface_types (interface_types )
395
395
396
- if not error_message :
397
- assigned_object_type , assigned_object = interface_types [0 ]
398
- host_type = (
399
- "device" if assigned_object_type == "interface" else "virtual_machine"
400
- )
396
+ # Get the assigned interface & interface type
397
+ assigned_object_type , assigned_object = interface_types [0 ]
401
398
402
- # Get the parent host (device or virtual machine) of the assigned interface
403
- if assigned_object_type == "interface" :
404
- host = Interface .objects .get (pk = assigned_object .pk ).device
405
- assigned_object_id = Interface .objects .get (pk = assigned_object .pk ).pk
406
- else :
407
- host = VMInterface .objects .get (pk = assigned_object .pk ).virtual_machine
408
- assigned_object_id = VMInterface .objects .get (pk = assigned_object .pk ).pk
409
-
410
- # Get the ContentType id for the assigned object
411
- assigned_object_type_id = ContentType .objects .get_for_model (
412
- assigned_object
413
- ).pk
414
-
415
- if not error_message :
416
- # Check if the parent host is assigned to the Access List
417
- error_message |= self ._check_if_interface_parent_is_assigned_to_access_list (
418
- cleaned_data .get ("access_list" ), assigned_object_type , host_type , host
419
- )
399
+ # Get the parent host (device or virtual machine) of the assigned interface
400
+ if assigned_object_type == "interface" :
401
+ assigned_object_id = Interface .objects .get (pk = assigned_object .pk ).pk
402
+ else :
403
+ assigned_object_id = VMInterface .objects .get (pk = assigned_object .pk ).pk
420
404
421
- if not error_message :
422
- # Check for duplicate entries in the Access List
423
- error_message |= self ._check_for_duplicate_entry (
424
- cleaned_data .get ("access_list" ),
425
- assigned_object_id ,
426
- assigned_object_type_id ,
427
- cleaned_data .get ("direction" ),
428
- )
405
+ # Get the ContentType id for the assigned object
406
+ assigned_object_type_id = ContentType .objects .get_for_model (assigned_object ).pk
429
407
430
- if not error_message :
431
- # Check if the interface already has an ACL applied in the specified direction
432
- error_message |= self ._check_if_interface_already_has_acl_in_direction (
433
- assigned_object_id ,
434
- assigned_object_type_id ,
435
- cleaned_data .get ("direction" ),
436
- )
408
+ # Check if the parent host is assigned to the Access List
409
+ self ._check_if_interface_parent_is_assigned_to_access_list (
410
+ cleaned_data .get ("access_list" ), assigned_object_type , assigned_object
411
+ )
437
412
438
- if error_message :
439
- raise forms .ValidationError (error_message )
440
- else :
441
- return cleaned_data
413
+ # Check for duplicate entries in the Access List
414
+ self ._check_if_interface_already_has_acl_in_direction (
415
+ cleaned_data .get ("access_list" ),
416
+ assigned_object_id ,
417
+ assigned_object_type ,
418
+ assigned_object_type_id ,
419
+ cleaned_data .get ("direction" ),
420
+ )
421
+
422
+ return cleaned_data
442
423
443
424
def _get_interface_types (self ):
444
425
"""
@@ -458,69 +439,73 @@ def _validate_interface_types(self, interface_types):
458
439
"""
459
440
# Check if more than 1 hosts selected.
460
441
if len (interface_types ) > 1 :
461
- return "Assignment can only be to one interface at a time (either a interface or vm_interface)."
442
+ raise forms .ValidationError (
443
+ "Assignment can only be to one interface at a time (either a interface or vminterface)."
444
+ )
462
445
# Check if no hosts selected.
463
446
elif not interface_types :
464
- return "No interface or vm_interface selected."
465
- else :
466
- return {}
447
+ raise forms .ValidationError ("No interface or vminterface selected." )
467
448
468
449
def _check_if_interface_parent_is_assigned_to_access_list (
469
- self , access_list , assigned_object_type , host_type , host
450
+ self , access_list , assigned_object_type , assigned_object
470
451
):
471
452
"""
472
453
Check that an interface's parent device/virtual_machine is assigned to the Access List.
473
454
"""
474
-
475
455
access_list_host = AccessList .objects .get (pk = access_list .pk ).assigned_object
456
+ host_type = (
457
+ "device" if assigned_object_type == "interface" else "virtual_machine"
458
+ )
459
+ if assigned_object_type == "interface" :
460
+ host = Interface .objects .get (pk = assigned_object .pk ).device
461
+ else :
462
+ host = VMInterface .objects .get (pk = assigned_object .pk ).virtual_machine
476
463
477
464
if access_list_host != host :
478
465
ERROR_ACL_NOT_ASSIGNED_TO_HOST = "Access List not present on selected host."
479
- return {
480
- "access_list" : [ ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
481
- assigned_object_type : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
482
- host_type : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
483
- }
484
- else :
485
- return {}
466
+ raise forms . ValidationError (
467
+ {
468
+ "access_list" : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
469
+ assigned_object_type : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
470
+ host_type : [ ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
471
+ }
472
+ )
486
473
487
- def _check_for_duplicate_entry (
488
- self , access_list , assigned_object_id , assigned_object_type_id , direction
474
+ def _check_if_interface_already_has_acl_in_direction (
475
+ self ,
476
+ access_list ,
477
+ assigned_object_id ,
478
+ assigned_object_type ,
479
+ assigned_object_type_id ,
480
+ direction ,
489
481
):
490
482
"""
491
- Check for duplicate entry. (Because of GFK)
483
+ Check that the interface does not have an existing ACL applied in the direction already.
492
484
"""
493
485
486
+ # Check for duplicate entry. (Because of GFK)
494
487
if ACLInterfaceAssignment .objects .filter (
495
488
access_list = access_list ,
496
489
assigned_object_id = assigned_object_id ,
497
490
assigned_object_type = assigned_object_type_id ,
498
491
direction = direction ,
499
492
).exists ():
500
- return {"access_list" : ["Duplicate entry." ]}
501
- else :
502
- return {}
503
-
504
- def _check_if_interface_already_has_acl_in_direction (
505
- self , assigned_object_id , assigned_object_type_id , direction
506
- ):
507
- """
508
- Check that the interface does not have an existing ACL applied in the direction already.
509
- """
510
- if not ACLInterfaceAssignment .objects .filter (
493
+ raise forms .ValidationError ({"access_list" : ["Duplicate entry." ]})
494
+ # Check that the interface does not have an existing ACL applied in the direction already.
495
+ elif ACLInterfaceAssignment .objects .filter (
511
496
assigned_object_id = assigned_object_id ,
512
497
assigned_object_type = assigned_object_type_id ,
513
498
direction = direction ,
514
499
).exists ():
515
- return {}
516
-
517
- error_interface_already_assigned = (
518
- "Interfaces can only have 1 Access List assigned in each direction."
519
- )
520
- return {
521
- "direction" : [error_interface_already_assigned ],
522
- assigned_object_type : [ error_interface_already_assigned ],
523
- }
500
+ error_interface_already_assigned = (
501
+ "Interfaces can only have 1 Access List assigned in each direction."
502
+ )
503
+ raise forms . ValidationError (
504
+ {
505
+ "direction" : [ error_interface_already_assigned ],
506
+ assigned_object_type : [error_interface_already_assigned ],
507
+ }
508
+ )
524
509
525
510
def save (self , * args , ** kwargs ):
526
511
"""
0 commit comments