mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-13 15:22:16 -06:00
Some checks failed
CodeQL / Analyze (${{ matrix.language }}) (none, actions) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, python) (push) Waiting to run
CI / build (20.x, 3.10) (push) Has been cancelled
CI / build (20.x, 3.11) (push) Has been cancelled
CI / build (20.x, 3.12) (push) Has been cancelled
Introduce a new `object_type_id` filter to enhance filtering by object type for Jobs. Update related forms and fieldsets to incorporate the new filter for better usability and consistency. Fixes #20653
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from drf_spectacular.utils import extend_schema_field
|
|
from rest_framework import serializers
|
|
|
|
from core.choices import *
|
|
from core.models import Job
|
|
from netbox.api.exceptions import SerializerNotFound
|
|
from netbox.api.fields import ChoiceField, ContentTypeField
|
|
from netbox.api.serializers import BaseModelSerializer
|
|
from users.api.serializers_.users import UserSerializer
|
|
from utilities.api import get_serializer_for_model
|
|
|
|
__all__ = (
|
|
'JobSerializer',
|
|
)
|
|
|
|
|
|
class JobSerializer(BaseModelSerializer):
|
|
user = UserSerializer(
|
|
nested=True,
|
|
read_only=True
|
|
)
|
|
status = ChoiceField(choices=JobStatusChoices, read_only=True)
|
|
object_type = ContentTypeField(
|
|
read_only=True
|
|
)
|
|
object = serializers.SerializerMethodField(
|
|
read_only=True
|
|
)
|
|
|
|
class Meta:
|
|
model = Job
|
|
fields = [
|
|
'id', 'url', 'display_url', 'display', 'object_type', 'object_id', 'object', 'name', 'status', 'created',
|
|
'scheduled', 'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', 'log_entries',
|
|
]
|
|
brief_fields = ('url', 'created', 'completed', 'user', 'status')
|
|
|
|
@extend_schema_field(serializers.JSONField(allow_null=True))
|
|
def get_object(self, obj):
|
|
"""
|
|
Serialize a nested representation of the object.
|
|
"""
|
|
if obj.object is None:
|
|
return None
|
|
try:
|
|
serializer = get_serializer_for_model(obj.object)
|
|
except SerializerNotFound:
|
|
return obj.object_repr
|
|
context = {'request': self.context['request']}
|
|
return serializer(obj.object, nested=True, context=context).data
|