@@ -174,7 +174,6 @@ def clean(self):
174
174
"""
175
175
# TODO: Refactor this method to fix error message logic.
176
176
cleaned_data = super ().clean ()
177
- error_message = {}
178
177
if self .errors .get ("name" ):
179
178
return cleaned_data
180
179
@@ -190,12 +189,9 @@ def clean(self):
190
189
host_type , host = host_types [0 ]
191
190
192
191
# Check if duplicate entry.
193
- self ._clean_check_duplicate_entry (name , host_type , host , error_message )
192
+ self ._clean_check_duplicate_entry (name , host_type , host )
194
193
# Check if Access List has no existing rules before change the Access List's type.
195
- self ._clean_check_acl_type_change (acl_type , error_message )
196
-
197
- if error_message :
198
- raise forms .ValidationError (error_message )
194
+ self ._clean_check_acl_type_change (acl_type )
199
195
200
196
return cleaned_data
201
197
@@ -227,7 +223,7 @@ def _clean_check_host_types(self, host_types):
227
223
"Access Lists must be assigned to a device, virtual chassis or virtual machine." ,
228
224
)
229
225
230
- def _clean_check_duplicate_entry (self , name , host_type , host , error_message ):
226
+ def _clean_check_duplicate_entry (self , name , host_type , host ):
231
227
"""
232
228
Used by parent class's clean method. Check if duplicate entry. (Because of GFK.)
233
229
"""
@@ -241,29 +237,40 @@ def _clean_check_duplicate_entry(self, name, host_type, host, error_message):
241
237
error_same_acl_name = (
242
238
"An ACL with this name is already associated to this host."
243
239
)
244
- error_message |= {
245
- host_type : [error_same_acl_name ],
246
- "name" : [error_same_acl_name ],
247
- }
240
+ raise forms .ValidationError (
241
+ {
242
+ host_type : [error_same_acl_name ],
243
+ "name" : [error_same_acl_name ],
244
+ }
245
+ )
248
246
249
- def _clean_check_acl_type_change (self , acl_type , error_message ):
247
+ def _clean_check_acl_type_change (self , acl_type ):
250
248
"""
251
249
Used by parent class's clean method. Check if Access List has no existing rules before change the Access List's type.
252
250
"""
253
251
if self .instance .pk :
254
- error_message [ "type" ] = [
252
+ ERROR_MESSAGE_EXISTING_RULES = (
255
253
"This ACL has ACL rules associated, CANNOT change ACL type." ,
256
- ]
254
+ )
257
255
if (
258
256
acl_type == ACLTypeChoices .TYPE_EXTENDED
259
257
and self .instance .aclstandardrules .exists ()
260
258
):
261
- raise forms .ValidationError (error_message )
259
+ raise forms .ValidationError (
260
+ {
261
+ "type" : ERROR_MESSAGE_EXISTING_RULES ,
262
+ }
263
+ )
264
+
262
265
if (
263
266
acl_type == ACLTypeChoices .TYPE_STANDARD
264
267
and self .instance .aclextendedrules .exists ()
265
268
):
266
- raise forms .ValidationError (error_message )
269
+ raise forms .ValidationError (
270
+ {
271
+ "type" : ERROR_MESSAGE_EXISTING_RULES ,
272
+ }
273
+ )
267
274
268
275
def save (self , * args , ** kwargs ):
269
276
"""
@@ -566,23 +573,7 @@ class Meta:
566
573
"source_ports" : HELP_TEXT_ACL_RULE_LOGIC ,
567
574
}
568
575
569
- def clean (self ):
570
- cleaned_data = super ().clean ()
571
- error_message = {}
572
-
573
- # No need to check for unique_together since there is no usage of GFK
574
-
575
- if cleaned_data .get ("action" ) == "remark" :
576
- self ._clean_check_acl_rules (cleaned_data , error_message , "extended" )
577
- # Check remark set, but action not set to remark.
578
- elif cleaned_data .get ("remark" ):
579
- error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
580
-
581
- if error_message :
582
- raise forms .ValidationError (error_message )
583
- return cleaned_data
584
-
585
- def _clean_check_acl_rules (self , cleaned_data , error_message , rule_type ):
576
+ def clean_check_remark_rule (self , cleaned_data , error_message , rule_type ):
586
577
"""
587
578
Used by parent class's clean method. Checks form inputs before submitting:
588
579
- Check if action set to remark, but no remark set.
@@ -611,6 +602,9 @@ def _clean_check_acl_rules(self, cleaned_data, error_message, rule_type):
611
602
f'Action is set to remark, { attribute .replace ("_" , " " ).title ()} CANNOT be set.'
612
603
]
613
604
605
+ if error_message :
606
+ forms .ValidationError (error_message )
607
+
614
608
615
609
class ACLStandardRuleForm (BaseACLRuleForm ):
616
610
"""
@@ -626,6 +620,25 @@ class Meta(BaseACLRuleForm.Meta):
626
620
# Need to add source_prefix to the tuple here instead of base or it will cause a ValueError
627
621
fields = BaseACLRuleForm .Meta .fields + ("source_prefix" ,)
628
622
623
+ def clean (self ):
624
+ cleaned_data = super ().clean ()
625
+ error_message = {}
626
+
627
+ # Check if action set to remark, but no remark set OR other fields set.
628
+ if cleaned_data .get ("action" ) == "remark" :
629
+ BaseACLRuleForm .clean_check_remark_rule (
630
+ self , cleaned_data , error_message , rule_type = "standard"
631
+ )
632
+
633
+ # Check remark set, but action not set to remark.
634
+ elif cleaned_data .get ("remark" ):
635
+ error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
636
+
637
+ if error_message :
638
+ raise forms .ValidationError (error_message )
639
+
640
+ return cleaned_data
641
+
629
642
630
643
class ACLExtendedRuleForm (BaseACLRuleForm ):
631
644
"""
@@ -668,3 +681,22 @@ class Meta(BaseACLRuleForm.Meta):
668
681
"destination_ports" ,
669
682
"protocol" ,
670
683
)
684
+
685
+ def clean (self ):
686
+ cleaned_data = super ().clean ()
687
+ error_message = {}
688
+
689
+ # Check if action set to remark, but no remark set OR other fields set.
690
+ if cleaned_data .get ("action" ) == "remark" :
691
+ BaseACLRuleForm .clean_check_remark_rule (
692
+ self , cleaned_data , error_message , rule_type = "extended"
693
+ )
694
+
695
+ # Check remark set, but action not set to remark.
696
+ elif cleaned_data .get ("remark" ):
697
+ error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
698
+
699
+ if error_message :
700
+ raise forms .ValidationError (error_message )
701
+
702
+ return cleaned_data
0 commit comments