Add add_empty_filtering_choice util for empty-value filtering

This commit is contained in:
Brian Tiemann 2024-09-18 16:14:04 -04:00
parent 4f9e7db23f
commit 1c6ee01a55
3 changed files with 12 additions and 4 deletions

View File

@ -1483,7 +1483,6 @@ class CableTypeChoices(ChoiceSet):
TYPE_AOC = 'aoc' TYPE_AOC = 'aoc'
TYPE_POWER = 'power' TYPE_POWER = 'power'
TYPE_USB = 'usb' TYPE_USB = 'usb'
TYPE_EMPTY = settings.FILTERS_NULL_CHOICE_VALUE
CHOICES = ( CHOICES = (
( (
@ -1520,7 +1519,6 @@ class CableTypeChoices(ChoiceSet):
_('Other'), ( _('Other'), (
(TYPE_USB, _('USB')), (TYPE_USB, _('USB')),
(TYPE_POWER, _('Power')), (TYPE_POWER, _('Power')),
(settings.FILTERS_NULL_CHOICE_VALUE, _('(unset)')),
) )
) )
) )

View File

@ -10,7 +10,7 @@ from ipam.models import ASN, VRF
from netbox.forms import NetBoxModelFilterSetForm from netbox.forms import NetBoxModelFilterSetForm
from tenancy.forms import ContactModelFilterForm, TenancyFilterForm from tenancy.forms import ContactModelFilterForm, TenancyFilterForm
from users.models import User from users.models import User
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, FilterForm, add_blank_choice from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, FilterForm, add_blank_choice, add_empty_filtering_choice
from utilities.forms.fields import ColorField, DynamicModelMultipleChoiceField, TagFilterField from utilities.forms.fields import ColorField, DynamicModelMultipleChoiceField, TagFilterField
from utilities.forms.rendering import FieldSet from utilities.forms.rendering import FieldSet
from utilities.forms.widgets import NumberWithOptions from utilities.forms.widgets import NumberWithOptions
@ -1052,7 +1052,7 @@ class CableFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm):
) )
type = forms.MultipleChoiceField( type = forms.MultipleChoiceField(
label=_('Type'), label=_('Type'),
choices=add_blank_choice(CableTypeChoices), choices=add_empty_filtering_choice(add_blank_choice(CableTypeChoices)),
required=False required=False
) )
status = forms.MultipleChoiceField( status = forms.MultipleChoiceField(

View File

@ -1,6 +1,7 @@
import re import re
from django import forms from django import forms
from django.conf import settings
from django.forms.models import fields_for_model from django.forms.models import fields_for_model
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -10,6 +11,7 @@ from .constants import *
__all__ = ( __all__ = (
'add_blank_choice', 'add_blank_choice',
'add_empty_filtering_choice',
'expand_alphanumeric_pattern', 'expand_alphanumeric_pattern',
'expand_ipaddress_pattern', 'expand_ipaddress_pattern',
'form_from_model', 'form_from_model',
@ -189,6 +191,14 @@ def add_blank_choice(choices):
return ((None, '---------'),) + tuple(choices) return ((None, '---------'),) + tuple(choices)
def add_empty_filtering_choice(choices):
"""
Add an empty (null) choice to the end of a choices list, to be used in filtering classes
such as NullableMultipleChoiceFilter to match on an empty value.
"""
return tuple(choices) + ((settings.FILTERS_NULL_CHOICE_VALUE, '(unset)'),)
def form_from_model(model, fields): def form_from_model(model, fields):
""" """
Return a Form class with the specified fields derived from a model. This is useful when we need a form to be used Return a Form class with the specified fields derived from a model. This is useful when we need a form to be used