37
37
"<b>*Note:</b> CANNOT be set if action is set to remark." ,
38
38
)
39
39
# Sets a standard help_text value to be used by the various classes for acl action
40
- help_text_acl_action = "Action the rule will take (remark, deny, or allow)."
40
+ HELP_TEXT_ACL_ACTION = "Action the rule will take (remark, deny, or allow)."
41
41
# Sets a standard help_text value to be used by the various classes for acl index
42
- help_text_acl_rule_index = (
42
+ HELP_TEXT_ACL_RULE_INDEX = (
43
43
"Determines the order of the rule in the ACL processing. AKA Sequence Number."
44
44
)
45
45
46
46
# Sets a standard error message for ACL rules with an action of remark, but no remark set.
47
- error_message_no_remark = "Action is set to remark, you MUST add a remark."
47
+ ERROR_MESSAGE_NO_REMARK = "Action is set to remark, you MUST add a remark."
48
48
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
49
- error_message_action_remark_source_prefix_set = (
49
+ ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET = (
50
50
"Action is set to remark, Source Prefix CANNOT be set."
51
51
)
52
52
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
53
- error_message_remark_without_action_remark = (
53
+ ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK = (
54
54
"CANNOT set remark unless action is set to remark."
55
55
)
56
56
@@ -133,6 +133,10 @@ class AccessListForm(NetBoxModelForm):
133
133
comments = CommentField ()
134
134
135
135
class Meta :
136
+ """
137
+ Defines the Model and fields to be used by the form.
138
+ """
139
+
136
140
model = AccessList
137
141
fields = (
138
142
"region" ,
@@ -156,16 +160,19 @@ class Meta:
156
160
}
157
161
158
162
def __init__ (self , * args , ** kwargs ):
163
+ """
164
+ Initializes the form
165
+ """
159
166
160
167
# Initialize helper selectors
161
168
instance = kwargs .get ("instance" )
162
169
initial = kwargs .get ("initial" , {}).copy ()
163
170
if instance :
164
- if type (instance .assigned_object ) is Device :
171
+ if isinstance (instance .assigned_object , Device ) :
165
172
initial ["device" ] = instance .assigned_object
166
- elif type (instance .assigned_object ) is VirtualChassis :
173
+ elif isinstance (instance .assigned_object , VirtualChassis ) :
167
174
initial ["virtual_chassis" ] = instance .assigned_object
168
- elif type (instance .assigned_object ) is VirtualMachine :
175
+ elif isinstance (instance .assigned_object , VirtualMachine ) :
169
176
initial ["virtual_machine" ] = instance .assigned_object
170
177
kwargs ["initial" ] = initial
171
178
@@ -231,12 +238,16 @@ def clean(self):
231
238
host_type : [error_same_acl_name ],
232
239
"name" : [error_same_acl_name ],
233
240
}
241
+ # Check if Access List has no existing rules before change the Access List's type.
234
242
if self .instance .pk :
235
- # Check if Access List has no existing rules before change the Access List's type.
236
243
if (
237
244
acl_type == ACLTypeChoices .TYPE_EXTENDED
238
245
and self .instance .aclstandardrules .exists ()
239
- ) or (
246
+ ):
247
+ error_message ["type" ] = [
248
+ "This ACL has ACL rules associated, CANNOT change ACL type." ,
249
+ ]
250
+ elif (
240
251
acl_type == ACLTypeChoices .TYPE_STANDARD
241
252
and self .instance .aclextendedrules .exists ()
242
253
):
@@ -250,7 +261,9 @@ def clean(self):
250
261
return cleaned_data
251
262
252
263
def save (self , * args , ** kwargs ):
253
- # Set assigned object
264
+ """
265
+ Set assigned object
266
+ """
254
267
self .instance .assigned_object = (
255
268
self .cleaned_data .get ("device" )
256
269
or self .cleaned_data .get ("virtual_chassis" )
@@ -315,22 +328,28 @@ class ACLInterfaceAssignmentForm(NetBoxModelForm):
315
328
comments = CommentField ()
316
329
317
330
def __init__ (self , * args , ** kwargs ):
331
+ """
332
+ Initialize helper selectors
333
+ """
318
334
319
- # Initialize helper selectors
320
335
instance = kwargs .get ("instance" )
321
336
initial = kwargs .get ("initial" , {}).copy ()
322
337
if instance :
323
- if type (instance .assigned_object ) is Interface :
338
+ if isinstance (instance .assigned_object , Interface ) :
324
339
initial ["interface" ] = instance .assigned_object
325
340
initial ["device" ] = "device"
326
- elif type (instance .assigned_object ) is VMInterface :
341
+ elif isinstance (instance .assigned_object , VMInterface ) :
327
342
initial ["vminterface" ] = instance .assigned_object
328
343
initial ["virtual_machine" ] = "virtual_machine"
329
344
kwargs ["initial" ] = initial
330
345
331
346
super ().__init__ (* args , ** kwargs )
332
347
333
348
class Meta :
349
+ """
350
+ Defines the Model and fields to be used by the form.
351
+ """
352
+
334
353
model = ACLInterfaceAssignment
335
354
fields = (
336
355
"access_list" ,
@@ -441,7 +460,9 @@ def clean(self):
441
460
return cleaned_data
442
461
443
462
def save (self , * args , ** kwargs ):
444
- # Set assigned object
463
+ """
464
+ Set assigned object
465
+ """
445
466
self .instance .assigned_object = self .cleaned_data .get (
446
467
"interface" ,
447
468
) or self .cleaned_data .get ("vminterface" )
@@ -478,6 +499,10 @@ class ACLStandardRuleForm(NetBoxModelForm):
478
499
)
479
500
480
501
class Meta :
502
+ """
503
+ Defines the Model and fields to be used by the form.
504
+ """
505
+
481
506
model = ACLStandardRule
482
507
fields = (
483
508
"access_list" ,
@@ -489,8 +514,8 @@ class Meta:
489
514
"description" ,
490
515
)
491
516
help_texts = {
492
- "index" : help_text_acl_rule_index ,
493
- "action" : help_text_acl_action ,
517
+ "index" : HELP_TEXT_ACL_RULE_INDEX ,
518
+ "action" : HELP_TEXT_ACL_ACTION ,
494
519
"remark" : mark_safe (
495
520
"<b>*Note:</b> CANNOT be set if source prefix OR action is set." ,
496
521
),
@@ -511,15 +536,15 @@ def clean(self):
511
536
if cleaned_data .get ("action" ) == "remark" :
512
537
# Check if action set to remark, but no remark set.
513
538
if not cleaned_data .get ("remark" ):
514
- error_message ["remark" ] = [error_message_no_remark ]
539
+ error_message ["remark" ] = [ERROR_MESSAGE_NO_REMARK ]
515
540
# Check if action set to remark, but source_prefix set.
516
541
if cleaned_data .get ("source_prefix" ):
517
542
error_message ["source_prefix" ] = [
518
- error_message_action_remark_source_prefix_set ,
543
+ ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET ,
519
544
]
520
545
# Check remark set, but action not set to remark.
521
546
elif cleaned_data .get ("remark" ):
522
- error_message ["remark" ] = [error_message_remark_without_action_remark ]
547
+ error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
523
548
524
549
if error_message :
525
550
raise forms .ValidationError (error_message )
@@ -574,6 +599,10 @@ class ACLExtendedRuleForm(NetBoxModelForm):
574
599
)
575
600
576
601
class Meta :
602
+ """
603
+ Defines the Model and fields to be used by the form.
604
+ """
605
+
577
606
model = ACLExtendedRule
578
607
fields = (
579
608
"access_list" ,
@@ -589,9 +618,9 @@ class Meta:
589
618
"description" ,
590
619
)
591
620
help_texts = {
592
- "action" : help_text_acl_action ,
621
+ "action" : HELP_TEXT_ACL_ACTION ,
593
622
"destination_ports" : help_text_acl_rule_logic ,
594
- "index" : help_text_acl_rule_index ,
623
+ "index" : HELP_TEXT_ACL_RULE_INDEX ,
595
624
"protocol" : help_text_acl_rule_logic ,
596
625
"remark" : mark_safe (
597
626
"<b>*Note:</b> CANNOT be set if action is not set to remark." ,
@@ -619,11 +648,11 @@ def clean(self):
619
648
if cleaned_data .get ("action" ) == "remark" :
620
649
# Check if action set to remark, but no remark set.
621
650
if not cleaned_data .get ("remark" ):
622
- error_message ["remark" ] = [error_message_no_remark ]
651
+ error_message ["remark" ] = [ERROR_MESSAGE_NO_REMARK ]
623
652
# Check if action set to remark, but source_prefix set.
624
653
if cleaned_data .get ("source_prefix" ):
625
654
error_message ["source_prefix" ] = [
626
- error_message_action_remark_source_prefix_set ,
655
+ ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET ,
627
656
]
628
657
# Check if action set to remark, but source_ports set.
629
658
if cleaned_data .get ("source_ports" ):
@@ -647,7 +676,7 @@ def clean(self):
647
676
]
648
677
# Check if action not set to remark, but remark set.
649
678
elif cleaned_data .get ("remark" ):
650
- error_message ["remark" ] = [error_message_remark_without_action_remark ]
679
+ error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
651
680
652
681
if error_message :
653
682
raise forms .ValidationError (error_message )
0 commit comments