@@ -374,7 +374,6 @@ class Meta:
374
374
),
375
375
}
376
376
377
-
378
377
def clean (self ):
379
378
"""
380
379
Validates form inputs before submitting:
@@ -396,7 +395,9 @@ def clean(self):
396
395
397
396
if not error_message :
398
397
assigned_object_type , assigned_object = interface_types [0 ]
399
- host_type = ("device" if assigned_object_type == "interface" else "virtual_machine" )
398
+ host_type = (
399
+ "device" if assigned_object_type == "interface" else "virtual_machine"
400
+ )
400
401
401
402
# Get the parent host (device or virtual machine) of the assigned interface
402
403
if assigned_object_type == "interface" :
@@ -407,26 +408,38 @@ def clean(self):
407
408
assigned_object_id = VMInterface .objects .get (pk = assigned_object .pk ).pk
408
409
409
410
# Get the ContentType id for the assigned object
410
- assigned_object_type_id = ContentType .objects .get_for_model (assigned_object ).pk
411
+ assigned_object_type_id = ContentType .objects .get_for_model (
412
+ assigned_object
413
+ ).pk
411
414
412
415
if not error_message :
413
416
# Check if the parent host is assigned to the Access List
414
- error_message |= self ._check_if_interface_parent_is_assigned_to_access_list (cleaned_data .get ("access_list" ), assigned_object_type , host_type , host )
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
+ )
415
420
416
421
if not error_message :
417
422
# Check for duplicate entries in the Access List
418
- error_message |= self ._check_for_duplicate_entry (cleaned_data .get ("access_list" ), assigned_object_id , assigned_object_type_id , cleaned_data .get ("direction" ),)
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
+ )
419
429
420
430
if not error_message :
421
431
# Check if the interface already has an ACL applied in the specified direction
422
- error_message |= self ._check_if_interface_already_has_acl_in_direction (assigned_object_id , assigned_object_type_id , cleaned_data .get ("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
+ )
423
437
424
438
if error_message :
425
439
raise forms .ValidationError (error_message )
426
440
else :
427
441
return cleaned_data
428
442
429
-
430
443
def _get_interface_types (self ):
431
444
"""
432
445
Get interface type/model assigned to the Access List.
@@ -452,26 +465,28 @@ def _validate_interface_types(self, interface_types):
452
465
else :
453
466
return {}
454
467
455
- def _check_if_interface_parent_is_assigned_to_access_list (self , access_list , assigned_object_type , host_type , host ):
468
+ def _check_if_interface_parent_is_assigned_to_access_list (
469
+ self , access_list , assigned_object_type , host_type , host
470
+ ):
456
471
"""
457
472
Check that an interface's parent device/virtual_machine is assigned to the Access List.
458
473
"""
459
474
460
475
access_list_host = AccessList .objects .get (pk = access_list .pk ).assigned_object
461
476
462
477
if access_list_host != host :
463
- ERROR_ACL_NOT_ASSIGNED_TO_HOST = (
464
- "Access List not present on selected host."
465
- )
478
+ ERROR_ACL_NOT_ASSIGNED_TO_HOST = "Access List not present on selected host."
466
479
return {
467
480
"access_list" : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
468
481
assigned_object_type : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
469
482
host_type : [ERROR_ACL_NOT_ASSIGNED_TO_HOST ],
470
- }
483
+ }
471
484
else :
472
485
return {}
473
486
474
- def _check_for_duplicate_entry (self , access_list , assigned_object_id , assigned_object_type_id , direction ):
487
+ def _check_for_duplicate_entry (
488
+ self , access_list , assigned_object_id , assigned_object_type_id , direction
489
+ ):
475
490
"""
476
491
Check for duplicate entry. (Because of GFK)
477
492
"""
@@ -486,25 +501,26 @@ def _check_for_duplicate_entry(self, access_list, assigned_object_id, assigned_o
486
501
else :
487
502
return {}
488
503
489
- def _check_if_interface_already_has_acl_in_direction (self , assigned_object_id , assigned_object_type_id , direction ):
504
+ def _check_if_interface_already_has_acl_in_direction (
505
+ self , assigned_object_id , assigned_object_type_id , direction
506
+ ):
490
507
"""
491
508
Check that the interface does not have an existing ACL applied in the direction already.
492
509
"""
493
- if ACLInterfaceAssignment .objects .filter (
510
+ if not ACLInterfaceAssignment .objects .filter (
494
511
assigned_object_id = assigned_object_id ,
495
512
assigned_object_type = assigned_object_type_id ,
496
513
direction = direction ,
497
514
).exists ():
515
+ return {}
516
+ else :
498
517
error_interface_already_assigned = (
499
518
"Interfaces can only have 1 Access List assigned in each direction."
500
519
)
501
520
return {
502
521
"direction" : [error_interface_already_assigned ],
503
522
assigned_object_type : [error_interface_already_assigned ],
504
523
}
505
- else :
506
- return {}
507
-
508
524
509
525
def save (self , * args , ** kwargs ):
510
526
"""
0 commit comments