mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 00:36:11 -06:00
adds custom field on webhook model #11936
This commit is contained in:
parent
c1ca8d5d8d
commit
836e6e008d
@ -18,7 +18,7 @@ from extras.utils import FeatureQuery
|
|||||||
from netbox.api.exceptions import SerializerNotFound
|
from netbox.api.exceptions import SerializerNotFound
|
||||||
from netbox.api.fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
|
from netbox.api.fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
|
||||||
from netbox.api.serializers import BaseModelSerializer, NetBoxModelSerializer, ValidatedModelSerializer
|
from netbox.api.serializers import BaseModelSerializer, NetBoxModelSerializer, ValidatedModelSerializer
|
||||||
from netbox.api.serializers.features import TaggableModelSerializer
|
from netbox.api.serializers.features import CustomFieldModelSerializer, TaggableModelSerializer
|
||||||
from netbox.constants import NESTED_SERIALIZER_PREFIX
|
from netbox.constants import NESTED_SERIALIZER_PREFIX
|
||||||
from tenancy.api.nested_serializers import NestedTenantSerializer, NestedTenantGroupSerializer
|
from tenancy.api.nested_serializers import NestedTenantSerializer, NestedTenantGroupSerializer
|
||||||
from tenancy.models import Tenant, TenantGroup
|
from tenancy.models import Tenant, TenantGroup
|
||||||
@ -61,7 +61,7 @@ __all__ = (
|
|||||||
# Webhooks
|
# Webhooks
|
||||||
#
|
#
|
||||||
|
|
||||||
class WebhookSerializer(ValidatedModelSerializer):
|
class WebhookSerializer(CustomFieldModelSerializer, ValidatedModelSerializer):
|
||||||
url = serializers.HyperlinkedIdentityField(view_name='extras-api:webhook-detail')
|
url = serializers.HyperlinkedIdentityField(view_name='extras-api:webhook-detail')
|
||||||
content_types = ContentTypeField(
|
content_types = ContentTypeField(
|
||||||
queryset=ContentType.objects.filter(FeatureQuery('webhooks').get_query()),
|
queryset=ContentType.objects.filter(FeatureQuery('webhooks').get_query()),
|
||||||
@ -74,7 +74,7 @@ class WebhookSerializer(ValidatedModelSerializer):
|
|||||||
'id', 'url', 'display', 'content_types', 'name', 'type_create', 'type_update', 'type_delete',
|
'id', 'url', 'display', 'content_types', 'name', 'type_create', 'type_update', 'type_delete',
|
||||||
'type_job_start', 'type_job_end', 'payload_url', 'enabled', 'http_method', 'http_content_type',
|
'type_job_start', 'type_job_end', 'payload_url', 'enabled', 'http_method', 'http_content_type',
|
||||||
'additional_headers', 'body_template', 'secret', 'conditions', 'ssl_verification', 'ca_file_path',
|
'additional_headers', 'body_template', 'secret', 'conditions', 'ssl_verification', 'ca_file_path',
|
||||||
'created', 'last_updated',
|
'custom_fields', 'created', 'last_updated',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
|
|
||||||
from extras.choices import *
|
from extras.choices import *
|
||||||
from extras.models import *
|
from extras.models import *
|
||||||
|
from netbox.forms import NetBoxModelBulkEditForm
|
||||||
from utilities.forms import BulkEditForm, add_blank_choice
|
from utilities.forms import BulkEditForm, add_blank_choice
|
||||||
from utilities.forms.fields import ColorField, DynamicModelChoiceField
|
from utilities.forms.fields import ColorField, DynamicModelChoiceField
|
||||||
from utilities.forms.widgets import BulkEditNullBooleanSelect
|
from utilities.forms.widgets import BulkEditNullBooleanSelect
|
||||||
@ -165,7 +166,9 @@ class SavedFilterBulkEditForm(BulkEditForm):
|
|||||||
nullable_fields = ('description',)
|
nullable_fields = ('description',)
|
||||||
|
|
||||||
|
|
||||||
class WebhookBulkEditForm(BulkEditForm):
|
class WebhookBulkEditForm(NetBoxModelBulkEditForm):
|
||||||
|
model = Webhook
|
||||||
|
|
||||||
pk = forms.ModelMultipleChoiceField(
|
pk = forms.ModelMultipleChoiceField(
|
||||||
queryset=Webhook.objects.all(),
|
queryset=Webhook.objects.all(),
|
||||||
widget=forms.MultipleHiddenInput
|
widget=forms.MultipleHiddenInput
|
||||||
@ -223,6 +226,10 @@ class WebhookBulkEditForm(BulkEditForm):
|
|||||||
label=_('CA file path')
|
label=_('CA file path')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_tags = None
|
||||||
|
|
||||||
|
remove_tags = None
|
||||||
|
|
||||||
nullable_fields = ('secret', 'conditions', 'ca_file_path')
|
nullable_fields = ('secret', 'conditions', 'ca_file_path')
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ class SavedFilterImportForm(CSVModelForm):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class WebhookImportForm(CSVModelForm):
|
class WebhookImportForm(NetBoxModelImportForm):
|
||||||
content_types = CSVMultipleContentTypeField(
|
content_types = CSVMultipleContentTypeField(
|
||||||
label=_('Content types'),
|
label=_('Content types'),
|
||||||
queryset=ContentType.objects.all(),
|
queryset=ContentType.objects.all(),
|
||||||
@ -148,6 +148,8 @@ class WebhookImportForm(CSVModelForm):
|
|||||||
help_text=_("One or more assigned object types")
|
help_text=_("One or more assigned object types")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
tags = None
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Webhook
|
model = Webhook
|
||||||
fields = (
|
fields = (
|
||||||
|
@ -16,7 +16,7 @@ from utilities.forms.fields import (
|
|||||||
)
|
)
|
||||||
from utilities.forms.widgets import APISelectMultiple, DateTimePicker
|
from utilities.forms.widgets import APISelectMultiple, DateTimePicker
|
||||||
from virtualization.models import Cluster, ClusterGroup, ClusterType
|
from virtualization.models import Cluster, ClusterGroup, ClusterType
|
||||||
from .mixins import SavedFiltersMixin
|
from .mixins import *
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'ConfigContextFilterForm',
|
'ConfigContextFilterForm',
|
||||||
@ -219,7 +219,9 @@ class SavedFilterFilterForm(SavedFiltersMixin, FilterForm):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class WebhookFilterForm(SavedFiltersMixin, FilterForm):
|
class WebhookFilterForm(CustomFieldsMixin, SavedFiltersMixin, FilterForm):
|
||||||
|
model = Webhook
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(None, ('q', 'filter_id')),
|
(None, ('q', 'filter_id')),
|
||||||
(_('Attributes'), ('content_type_id', 'http_method', 'enabled')),
|
(_('Attributes'), ('content_type_id', 'http_method', 'enabled')),
|
||||||
|
@ -214,13 +214,15 @@ class BookmarkForm(BootstrapMixin, forms.ModelForm):
|
|||||||
fields = ('object_type', 'object_id')
|
fields = ('object_type', 'object_id')
|
||||||
|
|
||||||
|
|
||||||
class WebhookForm(BootstrapMixin, forms.ModelForm):
|
class WebhookForm(NetBoxModelForm):
|
||||||
content_types = ContentTypeMultipleChoiceField(
|
content_types = ContentTypeMultipleChoiceField(
|
||||||
label=_('Content types'),
|
label=_('Content types'),
|
||||||
queryset=ContentType.objects.all(),
|
queryset=ContentType.objects.all(),
|
||||||
limit_choices_to=FeatureQuery('webhooks')
|
limit_choices_to=FeatureQuery('webhooks')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
tags = None
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(_('Webhook'), ('name', 'content_types', 'enabled')),
|
(_('Webhook'), ('name', 'content_types', 'enabled')),
|
||||||
(_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')),
|
(_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')),
|
||||||
|
@ -106,7 +106,7 @@ class TagType(ObjectType):
|
|||||||
filterset_class = filtersets.TagFilterSet
|
filterset_class = filtersets.TagFilterSet
|
||||||
|
|
||||||
|
|
||||||
class WebhookType(ObjectType):
|
class WebhookType(CustomFieldsMixin, ObjectType):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Webhook
|
model = models.Webhook
|
||||||
|
19
netbox/extras/migrations/0098_webhook_custom_field_data.py
Normal file
19
netbox/extras/migrations/0098_webhook_custom_field_data.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Generated by Django 4.1.10 on 2023-08-01 15:29
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import utilities.json
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('extras', '0097_customfield_remove_choices'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='webhook',
|
||||||
|
name='custom_field_data',
|
||||||
|
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
|
||||||
|
),
|
||||||
|
]
|
@ -39,7 +39,7 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Webhook(ExportTemplatesMixin, ChangeLoggedModel):
|
class Webhook(CustomFieldsMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
||||||
"""
|
"""
|
||||||
A Webhook defines a request that will be sent to a remote application when an object is created, updated, and/or
|
A Webhook defines a request that will be sent to a remote application when an object is created, updated, and/or
|
||||||
delete in NetBox. The request will contain a representation of the object, which the remote application can act on.
|
delete in NetBox. The request will contain a representation of the object, which the remote application can act on.
|
||||||
|
@ -36,7 +36,7 @@ class NetBoxModelForm(BootstrapMixin, CheckLastUpdatedMixin, CustomFieldsMixin,
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
# Limit tags to those applicable to the object type
|
# Limit tags to those applicable to the object type
|
||||||
if (ct := self._get_content_type()) and hasattr(self.fields['tags'].widget, 'add_query_param'):
|
if self.fields.get('tags') and (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)
|
self.fields['tags'].widget.add_query_param('for_object_type_id', ct.pk)
|
||||||
|
|
||||||
def _get_content_type(self):
|
def _get_content_type(self):
|
||||||
|
@ -147,6 +147,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% include 'inc/panels/custom_fields.html' %}
|
||||||
{% plugin_right_page object %}
|
{% plugin_right_page object %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user