class RelationshipAssociationFilterForm(BootstrapMixin, forms.Form): model = RelationshipAssociation relationship = DynamicModelMultipleChoiceField( queryset=Relationship.objects.all(), to_field_name="slug", required=False, ) source_type = MultipleContentTypeField( queryset=ContentType.objects.filter( FeatureQuery("relationships").get_query()).order_by( "app_label", "model"), required=False, label="Source Type", widget=StaticSelect2Multiple(), ) destination_type = MultipleContentTypeField( queryset=ContentType.objects.filter( FeatureQuery("relationships").get_query()).order_by( "app_label", "model"), required=False, label="Destination Type", widget=StaticSelect2Multiple(), )
class SecretFilterForm(BootstrapMixin, CustomFieldFilterForm): model = Secret q = forms.CharField(required=False, label="Search") provider = forms.MultipleChoiceField(choices=provider_choices_with_blank, widget=StaticSelect2Multiple(), required=False) tag = TagFilterField(model)
class ServiceFilterForm(BootstrapMixin, CustomFieldFilterForm): model = Service q = forms.CharField(required=False, label="Search") protocol = forms.ChoiceField( choices=add_blank_choice(ServiceProtocolChoices), required=False, widget=StaticSelect2Multiple(), ) port = forms.IntegerField(required=False, ) tag = TagFilterField(model)
class GoldenConfigSettingFeatureForm( utilities_forms.BootstrapMixin, extras_forms.CustomFieldModelForm, extras_forms.RelationshipModelForm ): """Filter Form for GoldenConfigSettingFeatureForm instances.""" backup_repository = forms.ModelMultipleChoiceField( queryset=GitRepository.objects.filter(provided_contents__contains="nautobot_golden_config.backupconfigs"), widget=StaticSelect2Multiple(), ) intended_repository = forms.ModelMultipleChoiceField( queryset=GitRepository.objects.filter(provided_contents__contains="nautobot_golden_config.intendedconfigs"), widget=StaticSelect2Multiple(), ) class Meta: """Filter Form Meta Data for GoldenConfigSettingFeatureForm instances.""" model = models.GoldenConfigSetting fields = ( "backup_repository", "backup_match_rule", "backup_path_template", "intended_repository", "intended_match_rule", "intended_path_template", "jinja_repository", "jinja_path_template", "backup_test_connectivity", "scope", "sot_agg_query", ) def clean(self): """Clean.""" super().clean() # This custom clean function validates logic of when or when not to # have a template matching path in GlobalConfigSettings for repos. for repo_type in ["intended", "backup"]: clean_config_settings( repo_type=repo_type, repo_count=self.cleaned_data.get(f"{repo_type}_repository").count(), match_rule=self.cleaned_data.get(f"{repo_type}_match_rule"), )
class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, StatusFilterFormMixin, CustomFieldFilterForm): model = IPAddress field_order = [ "q", "parent", "family", "mask_length", "vrf_id", "present_in_vrf_id", "status", "role", "assigned_to_interface", "tenant_group", "tenant", ] q = forms.CharField(required=False, label="Search") parent = forms.CharField( required=False, widget=forms.TextInput(attrs={ "placeholder": "Prefix", }), label="Parent Prefix", ) family = forms.ChoiceField( required=False, choices=add_blank_choice(IPAddressFamilyChoices), label="Address family", widget=StaticSelect2(), ) mask_length = forms.ChoiceField( required=False, choices=IPADDRESS_MASK_LENGTH_CHOICES, label="Mask length", widget=StaticSelect2(), ) vrf_id = DynamicModelMultipleChoiceField( queryset=VRF.objects.all(), required=False, label="Assigned VRF", null_option="Global", ) present_in_vrf_id = DynamicModelChoiceField(queryset=VRF.objects.all(), required=False, label="Present in VRF") role = forms.MultipleChoiceField(choices=IPAddressRoleChoices, required=False, widget=StaticSelect2Multiple()) assigned_to_interface = forms.NullBooleanField( required=False, label="Assigned to an interface", widget=StaticSelect2(choices=BOOLEAN_WITH_BLANK_CHOICES), ) tag = TagFilterField(model)
class StatusFilterForm(BootstrapMixin, CustomFieldFilterForm): """Filtering/search form for `Status` objects.""" model = Status q = forms.CharField(required=False, label="Search") content_types = MultipleContentTypeField( queryset=ContentType.objects.filter( FeatureQuery("statuses").get_query()).order_by( "app_label", "model"), required=False, label="Content type(s)", widget=StaticSelect2Multiple(), ) color = forms.CharField(max_length=6, required=False, widget=ColorSelect())
class RelationshipFilterForm(BootstrapMixin, forms.Form): model = Relationship type = forms.MultipleChoiceField(choices=RelationshipTypeChoices, required=False, widget=StaticSelect2Multiple()) source_type = MultipleContentTypeField( queryset=ContentType.objects.filter( FeatureQuery("relationships").get_query()).order_by( "app_label", "model"), required=False, label="Source Type", widget=StaticSelect2Multiple(), ) destination_type = MultipleContentTypeField( queryset=ContentType.objects.filter( FeatureQuery("relationships").get_query()).order_by( "app_label", "model"), required=False, label="Destination Type", widget=StaticSelect2Multiple(), )
class RelationshipFilterForm(BootstrapMixin, forms.Form): model = Relationship type = forms.MultipleChoiceField(choices=RelationshipTypeChoices, required=False, widget=StaticSelect2Multiple()) source_type = MultipleContentTypeField(feature="relationships", choices_as_strings=True, required=False, label="Source Type") destination_type = MultipleContentTypeField(feature="relationships", choices_as_strings=True, required=False, label="Destination Type")
class Meta: model = Service fields = [ "name", "protocol", "ports", "ipaddresses", "description", "tags", ] help_texts = { "ipaddresses": "IP address assignment is optional. If no IPs are selected, the service is assumed to be " "reachable via all IPs assigned to the device.", } widgets = { "protocol": StaticSelect2(), "ipaddresses": StaticSelect2Multiple(), }
class RelationshipFilterForm(BootstrapMixin, forms.Form): model = Relationship type = forms.MultipleChoiceField(choices=RelationshipTypeChoices, required=False, widget=StaticSelect2Multiple()) source_type = DynamicModelMultipleChoiceField( queryset=ContentType.objects.all(), required=False, display_field="display_name", label="Source Type", widget=APISelectMultiple(api_url="/api/extras/content-types/", ), ) destination_type = DynamicModelMultipleChoiceField( queryset=ContentType.objects.all(), required=False, display_field="display_name", label="Destination Type", widget=APISelectMultiple(api_url="/api/extras/content-types/", ), )
def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False): """ Return a form field suitable for setting a CustomField's value for an object. set_initial: Set initial date for the field. This should be False when generating a field for bulk editing. enforce_required: Honor the value of CustomField.required. Set to False for filtering/bulk editing. for_csv_import: Return a form field suitable for bulk import of objects in CSV format. """ initial = self.default if set_initial else None required = self.required if enforce_required else False # Integer if self.type == CustomFieldTypeChoices.TYPE_INTEGER: field = forms.IntegerField( required=required, initial=initial, min_value=self.validation_minimum, max_value=self.validation_maximum, ) # Boolean elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN: choices = ( (None, "---------"), (True, "True"), (False, "False"), ) field = forms.NullBooleanField( required=required, initial=initial, widget=StaticSelect2(choices=choices), ) # Date elif self.type == CustomFieldTypeChoices.TYPE_DATE: field = forms.DateField(required=required, initial=initial, widget=DatePicker()) # URL elif self.type == CustomFieldTypeChoices.TYPE_URL: field = LaxURLField(required=required, initial=initial) # Text elif self.type == CustomFieldTypeChoices.TYPE_TEXT: field = forms.CharField(max_length=255, required=required, initial=initial) if self.validation_regex: field.validators = [ RegexValidator( regex=self.validation_regex, message=mark_safe( f"Values must match this regex: <code>{self.validation_regex}</code>" ), ) ] # Select or Multi-select else: choices = [(cfc.value, cfc.value) for cfc in self.choices.all()] default_choice = self.choices.filter(value=self.default).first() if not required or default_choice is None: choices = add_blank_choice(choices) # Set the initial value to the first available choice (if any) if set_initial and default_choice: initial = default_choice.value if self.type == CustomFieldTypeChoices.TYPE_SELECT: field_class = CSVChoiceField if for_csv_import else forms.ChoiceField field = field_class( choices=choices, required=required, initial=initial, widget=StaticSelect2(), ) else: field_class = CSVMultipleChoiceField if for_csv_import else forms.MultipleChoiceField field = field_class(choices=choices, required=required, initial=initial, widget=StaticSelect2Multiple()) field.model = self field.label = str(self) if self.description: field.help_text = self.description return field