mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 12:06:53 -06:00
Fixes #13782: Fix tag exclusion support for contact assignments
This commit is contained in:
parent
b0541be107
commit
9aa7444bf9
@ -9,6 +9,7 @@ from utilities.forms.fields import DynamicModelMultipleChoiceField
|
|||||||
__all__ = (
|
__all__ = (
|
||||||
'CustomFieldsMixin',
|
'CustomFieldsMixin',
|
||||||
'SavedFiltersMixin',
|
'SavedFiltersMixin',
|
||||||
|
'TagsMixin',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -72,3 +73,19 @@ class SavedFiltersMixin(forms.Form):
|
|||||||
'usable': True,
|
'usable': True,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TagsMixin(forms.Form):
|
||||||
|
tags = DynamicModelMultipleChoiceField(
|
||||||
|
queryset=Tag.objects.all(),
|
||||||
|
required=False,
|
||||||
|
label=_('Tags'),
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Limit tags to those applicable to the object type
|
||||||
|
content_type = ContentType.objects.get_for_model(self._meta.model)
|
||||||
|
if content_type and hasattr(self.fields['tags'].widget, 'add_query_param'):
|
||||||
|
self.fields['tags'].widget.add_query_param('for_object_type_id', content_type.pk)
|
||||||
|
@ -4,10 +4,11 @@ from django.db.models import Q
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from extras.choices import CustomFieldFilterLogicChoices, CustomFieldTypeChoices, CustomFieldVisibilityChoices
|
from extras.choices import CustomFieldFilterLogicChoices, CustomFieldTypeChoices, CustomFieldVisibilityChoices
|
||||||
from extras.forms.mixins import CustomFieldsMixin, SavedFiltersMixin
|
from extras.forms.mixins import CustomFieldsMixin, SavedFiltersMixin, TagsMixin
|
||||||
from extras.models import CustomField, Tag
|
from extras.models import CustomField, Tag
|
||||||
from utilities.forms import BootstrapMixin, CSVModelForm, CheckLastUpdatedMixin
|
from utilities.forms import CSVModelForm
|
||||||
from utilities.forms.fields import CSVModelMultipleChoiceField, DynamicModelMultipleChoiceField
|
from utilities.forms.fields import CSVModelMultipleChoiceField, DynamicModelMultipleChoiceField
|
||||||
|
from utilities.forms.mixins import BootstrapMixin, CheckLastUpdatedMixin
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'NetBoxModelForm',
|
'NetBoxModelForm',
|
||||||
@ -17,7 +18,7 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class NetBoxModelForm(BootstrapMixin, CheckLastUpdatedMixin, CustomFieldsMixin, forms.ModelForm):
|
class NetBoxModelForm(BootstrapMixin, CheckLastUpdatedMixin, CustomFieldsMixin, TagsMixin, forms.ModelForm):
|
||||||
"""
|
"""
|
||||||
Base form for creating & editing NetBox models. Extends Django's ModelForm to add support for custom fields.
|
Base form for creating & editing NetBox models. Extends Django's ModelForm to add support for custom fields.
|
||||||
|
|
||||||
@ -26,18 +27,6 @@ class NetBoxModelForm(BootstrapMixin, CheckLastUpdatedMixin, CustomFieldsMixin,
|
|||||||
the rendered form (optional). If not defined, the all fields will be rendered as a single section.
|
the rendered form (optional). If not defined, the all fields will be rendered as a single section.
|
||||||
"""
|
"""
|
||||||
fieldsets = ()
|
fieldsets = ()
|
||||||
tags = DynamicModelMultipleChoiceField(
|
|
||||||
queryset=Tag.objects.all(),
|
|
||||||
required=False,
|
|
||||||
label=_('Tags'),
|
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
# Limit tags to those applicable to the object type
|
|
||||||
if (ct := self._get_content_type()) and hasattr(self.fields['tags'].widget, 'add_query_param'):
|
|
||||||
self.fields['tags'].widget.add_query_param('for_object_type_id', ct.pk)
|
|
||||||
|
|
||||||
def _get_content_type(self):
|
def _get_content_type(self):
|
||||||
return ContentType.objects.get_for_model(self._meta.model)
|
return ContentType.objects.get_for_model(self._meta.model)
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from extras.forms.mixins import TagsMixin
|
||||||
from extras.models import Tag
|
from extras.models import Tag
|
||||||
from netbox.forms import NetBoxModelForm
|
from netbox.forms import NetBoxModelForm
|
||||||
from tenancy.models import *
|
from tenancy.models import *
|
||||||
from utilities.forms import BootstrapMixin
|
from utilities.forms.mixins import BootstrapMixin
|
||||||
from utilities.forms.fields import CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SlugField
|
from utilities.forms.fields import CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SlugField
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -121,7 +122,7 @@ class ContactForm(NetBoxModelForm):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class ContactAssignmentForm(BootstrapMixin, forms.ModelForm):
|
class ContactAssignmentForm(BootstrapMixin, TagsMixin, forms.ModelForm):
|
||||||
group = DynamicModelChoiceField(
|
group = DynamicModelChoiceField(
|
||||||
label=_('Group'),
|
label=_('Group'),
|
||||||
queryset=ContactGroup.objects.all(),
|
queryset=ContactGroup.objects.all(),
|
||||||
@ -141,11 +142,6 @@ class ContactAssignmentForm(BootstrapMixin, forms.ModelForm):
|
|||||||
label=_('Role'),
|
label=_('Role'),
|
||||||
queryset=ContactRole.objects.all()
|
queryset=ContactRole.objects.all()
|
||||||
)
|
)
|
||||||
tags = DynamicModelMultipleChoiceField(
|
|
||||||
queryset=Tag.objects.all(),
|
|
||||||
required=False,
|
|
||||||
label=_('Tags')
|
|
||||||
)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ContactAssignment
|
model = ContactAssignment
|
||||||
|
Loading…
Reference in New Issue
Block a user