mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-23 03:58:45 -06:00
Use GFKSerializerField for ObjectChangeSerializer.changed_object and EventRuleSerializer.action_object
This commit is contained in:
@@ -219028,9 +219028,8 @@
|
|||||||
"nullable": true
|
"nullable": true
|
||||||
},
|
},
|
||||||
"action_object": {
|
"action_object": {
|
||||||
"type": "object",
|
"readOnly": true,
|
||||||
"additionalProperties": {},
|
"nullable": true
|
||||||
"readOnly": true
|
|
||||||
},
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
@@ -229795,7 +229794,11 @@
|
|||||||
"format": "int64"
|
"format": "int64"
|
||||||
},
|
},
|
||||||
"changed_object": {
|
"changed_object": {
|
||||||
"nullable": true,
|
"readOnly": true,
|
||||||
|
"nullable": true
|
||||||
|
},
|
||||||
|
"object_repr": {
|
||||||
|
"type": "string",
|
||||||
"readOnly": true
|
"readOnly": true
|
||||||
},
|
},
|
||||||
"message": {
|
"message": {
|
||||||
@@ -229820,6 +229823,7 @@
|
|||||||
"display_url",
|
"display_url",
|
||||||
"id",
|
"id",
|
||||||
"message",
|
"message",
|
||||||
|
"object_repr",
|
||||||
"postchange_data",
|
"postchange_data",
|
||||||
"prechange_data",
|
"prechange_data",
|
||||||
"request_id",
|
"request_id",
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
from drf_spectacular.utils import extend_schema_field
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from core.choices import *
|
from core.choices import *
|
||||||
from core.models import ObjectChange
|
from core.models import ObjectChange
|
||||||
from netbox.api.exceptions import SerializerNotFound
|
|
||||||
from netbox.api.fields import ChoiceField, ContentTypeField
|
from netbox.api.fields import ChoiceField, ContentTypeField
|
||||||
|
from netbox.api.gfk_fields import GFKSerializerField
|
||||||
from netbox.api.serializers import BaseModelSerializer
|
from netbox.api.serializers import BaseModelSerializer
|
||||||
from users.api.serializers_.users import UserSerializer
|
from users.api.serializers_.users import UserSerializer
|
||||||
from utilities.api import get_serializer_for_model
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'ObjectChangeSerializer',
|
'ObjectChangeSerializer',
|
||||||
@@ -26,7 +24,10 @@ class ObjectChangeSerializer(BaseModelSerializer):
|
|||||||
changed_object_type = ContentTypeField(
|
changed_object_type = ContentTypeField(
|
||||||
read_only=True
|
read_only=True
|
||||||
)
|
)
|
||||||
changed_object = serializers.SerializerMethodField(
|
changed_object = GFKSerializerField(
|
||||||
|
read_only=True
|
||||||
|
)
|
||||||
|
object_repr = serializers.CharField(
|
||||||
read_only=True
|
read_only=True
|
||||||
)
|
)
|
||||||
prechange_data = serializers.JSONField(
|
prechange_data = serializers.JSONField(
|
||||||
@@ -44,22 +45,6 @@ class ObjectChangeSerializer(BaseModelSerializer):
|
|||||||
model = ObjectChange
|
model = ObjectChange
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'url', 'display_url', 'display', 'time', 'user', 'user_name', 'request_id', 'action',
|
'id', 'url', 'display_url', 'display', 'time', 'user', 'user_name', 'request_id', 'action',
|
||||||
'changed_object_type', 'changed_object_id', 'changed_object', 'message', 'prechange_data',
|
'changed_object_type', 'changed_object_id', 'changed_object', 'object_repr', 'message',
|
||||||
'postchange_data',
|
'prechange_data', 'postchange_data',
|
||||||
]
|
]
|
||||||
|
|
||||||
@extend_schema_field(serializers.JSONField(allow_null=True))
|
|
||||||
def get_changed_object(self, obj):
|
|
||||||
"""
|
|
||||||
Serialize a nested representation of the changed object.
|
|
||||||
"""
|
|
||||||
if obj.changed_object is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
serializer = get_serializer_for_model(obj.changed_object)
|
|
||||||
except SerializerNotFound:
|
|
||||||
return obj.object_repr
|
|
||||||
data = serializer(obj.changed_object, nested=True, context={'request': self.context['request']}).data
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
from drf_spectacular.types import OpenApiTypes
|
|
||||||
from drf_spectacular.utils import extend_schema_field
|
|
||||||
from rest_framework import serializers
|
|
||||||
|
|
||||||
from core.models import ObjectType
|
from core.models import ObjectType
|
||||||
from extras.choices import *
|
from extras.choices import *
|
||||||
from extras.models import EventRule, Webhook
|
from extras.models import EventRule, Webhook
|
||||||
from netbox.api.fields import ChoiceField, ContentTypeField
|
from netbox.api.fields import ChoiceField, ContentTypeField
|
||||||
|
from netbox.api.gfk_fields import GFKSerializerField
|
||||||
from netbox.api.serializers import NetBoxModelSerializer
|
from netbox.api.serializers import NetBoxModelSerializer
|
||||||
from users.api.serializers_.mixins import OwnerMixin
|
from users.api.serializers_.mixins import OwnerMixin
|
||||||
from utilities.api import get_serializer_for_model
|
|
||||||
from .scripts import ScriptSerializer
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'EventRuleSerializer',
|
'EventRuleSerializer',
|
||||||
@@ -30,7 +25,7 @@ class EventRuleSerializer(OwnerMixin, NetBoxModelSerializer):
|
|||||||
action_object_type = ContentTypeField(
|
action_object_type = ContentTypeField(
|
||||||
queryset=ObjectType.objects.with_feature('event_rules'),
|
queryset=ObjectType.objects.with_feature('event_rules'),
|
||||||
)
|
)
|
||||||
action_object = serializers.SerializerMethodField(read_only=True)
|
action_object = GFKSerializerField(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = EventRule
|
model = EventRule
|
||||||
@@ -41,17 +36,6 @@ class EventRuleSerializer(OwnerMixin, NetBoxModelSerializer):
|
|||||||
]
|
]
|
||||||
brief_fields = ('id', 'url', 'display', 'name', 'description')
|
brief_fields = ('id', 'url', 'display', 'name', 'description')
|
||||||
|
|
||||||
@extend_schema_field(OpenApiTypes.OBJECT)
|
|
||||||
def get_action_object(self, instance):
|
|
||||||
context = {'request': self.context['request']}
|
|
||||||
# We need to manually instantiate the serializer for scripts
|
|
||||||
if instance.action_type == EventRuleActionChoices.SCRIPT:
|
|
||||||
script = instance.action_object
|
|
||||||
return ScriptSerializer(script, nested=True, context=context).data
|
|
||||||
else:
|
|
||||||
serializer = get_serializer_for_model(instance.action_object_type.model_class())
|
|
||||||
return serializer(instance.action_object, nested=True, context=context).data
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Webhooks
|
# Webhooks
|
||||||
|
|||||||
Reference in New Issue
Block a user