@@ -174,7 +174,6 @@ def clean(self):
174174 """
175175 # TODO: Refactor this method to fix error message logic.
176176 cleaned_data = super ().clean ()
177- error_message = {}
178177 if self .errors .get ("name" ):
179178 return cleaned_data
180179
@@ -190,12 +189,9 @@ def clean(self):
190189 host_type , host = host_types [0 ]
191190
192191 # 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 )
194193 # 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 )
199195
200196 return cleaned_data
201197
@@ -227,7 +223,7 @@ def _clean_check_host_types(self, host_types):
227223 "Access Lists must be assigned to a device, virtual chassis or virtual machine." ,
228224 )
229225
230- def _clean_check_duplicate_entry (self , name , host_type , host , error_message ):
226+ def _clean_check_duplicate_entry (self , name , host_type , host ):
231227 """
232228 Used by parent class's clean method. Check if duplicate entry. (Because of GFK.)
233229 """
@@ -241,29 +237,40 @@ def _clean_check_duplicate_entry(self, name, host_type, host, error_message):
241237 error_same_acl_name = (
242238 "An ACL with this name is already associated to this host."
243239 )
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+ )
248246
249- def _clean_check_acl_type_change (self , acl_type , error_message ):
247+ def _clean_check_acl_type_change (self , acl_type ):
250248 """
251249 Used by parent class's clean method. Check if Access List has no existing rules before change the Access List's type.
252250 """
253251 if self .instance .pk :
254- error_message [ "type" ] = [
252+ ERROR_MESSAGE_EXISTING_RULES = (
255253 "This ACL has ACL rules associated, CANNOT change ACL type." ,
256- ]
254+ )
257255 if (
258256 acl_type == ACLTypeChoices .TYPE_EXTENDED
259257 and self .instance .aclstandardrules .exists ()
260258 ):
261- raise forms .ValidationError (error_message )
259+ raise forms .ValidationError (
260+ {
261+ "type" : ERROR_MESSAGE_EXISTING_RULES ,
262+ }
263+ )
264+
262265 if (
263266 acl_type == ACLTypeChoices .TYPE_STANDARD
264267 and self .instance .aclextendedrules .exists ()
265268 ):
266- raise forms .ValidationError (error_message )
269+ raise forms .ValidationError (
270+ {
271+ "type" : ERROR_MESSAGE_EXISTING_RULES ,
272+ }
273+ )
267274
268275 def save (self , * args , ** kwargs ):
269276 """
@@ -566,23 +573,7 @@ class Meta:
566573 "source_ports" : HELP_TEXT_ACL_RULE_LOGIC ,
567574 }
568575
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 ):
586577 """
587578 Used by parent class's clean method. Checks form inputs before submitting:
588579 - 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):
611602 f'Action is set to remark, { attribute .replace ("_" , " " ).title ()} CANNOT be set.'
612603 ]
613604
605+ if error_message :
606+ forms .ValidationError (error_message )
607+
614608
615609class ACLStandardRuleForm (BaseACLRuleForm ):
616610 """
@@ -626,6 +620,25 @@ class Meta(BaseACLRuleForm.Meta):
626620 # Need to add source_prefix to the tuple here instead of base or it will cause a ValueError
627621 fields = BaseACLRuleForm .Meta .fields + ("source_prefix" ,)
628622
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+
629642
630643class ACLExtendedRuleForm (BaseACLRuleForm ):
631644 """
@@ -668,3 +681,22 @@ class Meta(BaseACLRuleForm.Meta):
668681 "destination_ports" ,
669682 "protocol" ,
670683 )
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