@@ -453,11 +453,9 @@ def save(self, *args, **kwargs):
453453 return super ().save (* args , ** kwargs )
454454
455455
456- class ACLStandardRuleForm (NetBoxModelForm ):
456+ class BaseACLRuleForm (NetBoxModelForm ):
457457 """
458- GUI form to add or edit Standard Access List.
459- Requires an access_list, an index, and ACL rule type.
460- See the clean function for logic on other field requirements.
458+ GUI form to add or edit Access List Rules to be inherited by other classes
461459 """
462460
463461 access_list = DynamicModelChoiceField (
@@ -506,26 +504,13 @@ class Meta:
506504 }
507505
508506 def clean (self ):
509- """
510- Validates form inputs before submitting:
511- - Check if action set to remark, but no remark set.
512- - Check if action set to remark, but source_prefix set.
513- - Check remark set, but action not set to remark.
514- """
515507 cleaned_data = super ().clean ()
516508 error_message = {}
517509
518510 # No need to check for unique_together since there is no usage of GFK
519511
520512 if cleaned_data .get ("action" ) == "remark" :
521- # Check if action set to remark, but no remark set.
522- if not cleaned_data .get ("remark" ):
523- error_message ["remark" ] = [ERROR_MESSAGE_NO_REMARK ]
524- # Check if action set to remark, but source_prefix set.
525- if cleaned_data .get ("source_prefix" ):
526- error_message ["source_prefix" ] = [
527- ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET ,
528- ]
513+ self ._extracted_from_clean_20 (cleaned_data , error_message , "extended" )
529514 # Check remark set, but action not set to remark.
530515 elif cleaned_data .get ("remark" ):
531516 error_message ["remark" ] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK ]
@@ -535,7 +520,57 @@ def clean(self):
535520 return cleaned_data
536521
537522
538- class ACLExtendedRuleForm (NetBoxModelForm ):
523+ def _extracted_from_clean_20 (self , cleaned_data , error_message , rule_type ):
524+ """
525+ Validates form inputs before submitting:
526+ - Check if action set to remark, but no remark set.
527+ - Check if action set to remark, but source_prefix set.
528+ - Check if action set to remark, but source_ports set.
529+ - Check if action set to remark, but destination_prefix set.
530+ - Check if action set to remark, but destination_ports set.
531+ - Check if action set to remark, but destination_ports set.
532+ - Check if action set to remark, but protocol set.
533+ """
534+ # Check if action set to remark, but no remark set.
535+ if not cleaned_data .get ("remark" ):
536+ error_message ["remark" ] = [ERROR_MESSAGE_NO_REMARK ]
537+ # Check if action set to remark, but source_prefix set.
538+ if cleaned_data .get ("source_prefix" ):
539+ error_message ["source_prefix" ] = [
540+ ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET ,
541+ ]
542+ if rule_type == "extended" :
543+ # Check if action set to remark, but source_ports set.
544+ if cleaned_data .get ("source_ports" ):
545+ error_message ["source_ports" ] = [
546+ "Action is set to remark, Source Ports CANNOT be set." ,
547+ ]
548+ # Check if action set to remark, but destination_prefix set.
549+ if cleaned_data .get ("destination_prefix" ):
550+ error_message ["destination_prefix" ] = [
551+ "Action is set to remark, Destination Prefix CANNOT be set." ,
552+ ]
553+ # Check if action set to remark, but destination_ports set.
554+ if cleaned_data .get ("destination_ports" ):
555+ error_message ["destination_ports" ] = [
556+ "Action is set to remark, Destination Ports CANNOT be set." ,
557+ ]
558+ # Check if action set to remark, but protocol set.
559+ if cleaned_data .get ("protocol" ):
560+ error_message ["protocol" ] = [
561+ "Action is set to remark, Protocol CANNOT be set." ,
562+ ]
563+
564+
565+ class ACLStandardRuleForm (BaseACLRuleForm ):
566+ """
567+ GUI form to add or edit Standard Access List.
568+ Requires an access_list, an index, and ACL rule type.
569+ See the clean function for logic on other field requirements.
570+ """
571+
572+
573+ class ACLExtendedRuleForm (BaseACLRuleForm ):
539574 """
540575 GUI form to add or edit Extended Access List.
541576 Requires an access_list, an index, and ACL rule type.
@@ -553,35 +588,20 @@ class ACLExtendedRuleForm(NetBoxModelForm):
553588 label = "Access List" ,
554589 )
555590
556- source_prefix = DynamicModelChoiceField (
557- queryset = Prefix .objects .all (),
558- required = False ,
559- help_text = HELP_TEXT_ACL_RULE_LOGIC ,
560- label = "Source Prefix" ,
561- )
562591 destination_prefix = DynamicModelChoiceField (
563592 queryset = Prefix .objects .all (),
564593 required = False ,
565594 help_text = HELP_TEXT_ACL_RULE_LOGIC ,
566595 label = "Destination Prefix" ,
567596 )
568- fieldsets = (
569- ("Access List Details" , ("access_list" , "description" , "tags" )),
597+ fieldsets = BaseACLRuleForm .fieldsets [:- 1 ] + (
570598 (
571599 "Rule Definition" ,
572- (
573- "index" ,
574- "action" ,
575- "remark" ,
576- "source_prefix" ,
577- "source_ports" ,
578- "destination_prefix" ,
579- "destination_ports" ,
580- "protocol" ,
581- ),
600+ BaseACLRuleForm .fieldsets [- 1 ][1 ] + ("source_ports" , "destination_prefix" , "destination_ports" , "protocol" ),
582601 ),
583602 )
584603
604+
585605 class Meta :
586606 """
587607 Defines the Model and fields to be used by the form.
@@ -611,60 +631,3 @@ class Meta:
611631 ),
612632 "source_ports" : HELP_TEXT_ACL_RULE_LOGIC ,
613633 }
614-
615- def clean (self ):
616- """
617- Validates form inputs before submitting:
618- - Check if action set to remark, but no remark set.
619- - Check if action set to remark, but source_prefix set.
620- - Check if action set to remark, but source_ports set.
621- - Check if action set to remark, but destination_prefix set.
622- - Check if action set to remark, but destination_ports set.
623- - Check if action set to remark, but destination_ports set.
624- - Check if action set to remark, but protocol set.
625- - Check remark set, but action not set to remark.
626- """
627- cleaned_data = super ().clean ()
628- error_message = {}
629-
630- # No need to check for unique_together since there is no usage of GFK
631-
632- if cleaned_data .get ("action" ) == "remark" :
633- self ._extracted_from_clean_20 (cleaned_data , error_message )
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- return cleaned_data
640-
641- # TODO: Consolidate this function with the one in ACLStandardRuleForm
642- def _extracted_from_clean_20 (self , cleaned_data , error_message ):
643- # Check if action set to remark, but no remark set.
644- if not cleaned_data .get ("remark" ):
645- error_message ["remark" ] = [ERROR_MESSAGE_NO_REMARK ]
646- # Check if action set to remark, but source_prefix set.
647- if cleaned_data .get ("source_prefix" ):
648- error_message ["source_prefix" ] = [
649- ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET ,
650- ]
651- # Check if action set to remark, but source_ports set.
652- if cleaned_data .get ("source_ports" ):
653- error_message ["source_ports" ] = [
654- "Action is set to remark, Source Ports CANNOT be set." ,
655- ]
656- # Check if action set to remark, but destination_prefix set.
657- if cleaned_data .get ("destination_prefix" ):
658- error_message ["destination_prefix" ] = [
659- "Action is set to remark, Destination Prefix CANNOT be set." ,
660- ]
661- # Check if action set to remark, but destination_ports set.
662- if cleaned_data .get ("destination_ports" ):
663- error_message ["destination_ports" ] = [
664- "Action is set to remark, Destination Ports CANNOT be set." ,
665- ]
666- # Check if action set to remark, but protocol set.
667- if cleaned_data .get ("protocol" ):
668- error_message ["protocol" ] = [
669- "Action is set to remark, Protocol CANNOT be set." ,
670- ]
0 commit comments