Skip to content

Commit 87ff83e

Browse files
authored
feat(filtersets): Add object_type_id filter for Jobs (#20674)
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
1 parent 3cdc625 commit 87ff83e

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

netbox/core/api/serializers_/jobs.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
from drf_spectacular.utils import extend_schema_field
2+
from rest_framework import serializers
3+
14
from core.choices import *
25
from core.models import Job
6+
from netbox.api.exceptions import SerializerNotFound
37
from netbox.api.fields import ChoiceField, ContentTypeField
48
from netbox.api.serializers import BaseModelSerializer
59
from users.api.serializers_.users import UserSerializer
10+
from utilities.api import get_serializer_for_model
611

712
__all__ = (
813
'JobSerializer',
@@ -18,11 +23,28 @@ class JobSerializer(BaseModelSerializer):
1823
object_type = ContentTypeField(
1924
read_only=True
2025
)
26+
object = serializers.SerializerMethodField(
27+
read_only=True
28+
)
2129

2230
class Meta:
2331
model = Job
2432
fields = [
25-
'id', 'url', 'display_url', 'display', 'object_type', 'object_id', 'name', 'status', 'created', 'scheduled',
26-
'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', 'log_entries',
33+
'id', 'url', 'display_url', 'display', 'object_type', 'object_id', 'object', 'name', 'status', 'created',
34+
'scheduled', 'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', 'log_entries',
2735
]
2836
brief_fields = ('url', 'created', 'completed', 'user', 'status')
37+
38+
@extend_schema_field(serializers.JSONField(allow_null=True))
39+
def get_object(self, obj):
40+
"""
41+
Serialize a nested representation of the object.
42+
"""
43+
if obj.object is None:
44+
return None
45+
try:
46+
serializer = get_serializer_for_model(obj.object)
47+
except SerializerNotFound:
48+
return obj.object_repr
49+
context = {'request': self.context['request']}
50+
return serializer(obj.object, nested=True, context=context).data

netbox/core/filtersets.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class JobFilterSet(BaseFilterSet):
8080
method='search',
8181
label=_('Search'),
8282
)
83+
object_type_id = django_filters.ModelMultipleChoiceFilter(
84+
queryset=ObjectType.objects.with_feature('jobs'),
85+
field_name='object_type_id',
86+
)
8387
object_type = ContentTypeFilter()
8488
created = django_filters.DateTimeFilter()
8589
created__before = django_filters.DateTimeFilter(
@@ -124,7 +128,7 @@ class JobFilterSet(BaseFilterSet):
124128

125129
class Meta:
126130
model = Job
127-
fields = ('id', 'object_type', 'object_id', 'name', 'interval', 'status', 'user', 'job_id')
131+
fields = ('id', 'object_type', 'object_type_id', 'object_id', 'name', 'interval', 'status', 'user', 'job_id')
128132

129133
def search(self, queryset, name, value):
130134
if not value.strip():

netbox/core/forms/filtersets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ class JobFilterForm(SavedFiltersMixin, FilterForm):
7070
model = Job
7171
fieldsets = (
7272
FieldSet('q', 'filter_id'),
73-
FieldSet('object_type', 'status', name=_('Attributes')),
73+
FieldSet('object_type_id', 'status', name=_('Attributes')),
7474
FieldSet(
7575
'created__before', 'created__after', 'scheduled__before', 'scheduled__after', 'started__before',
7676
'started__after', 'completed__before', 'completed__after', 'user', name=_('Creation')
7777
),
7878
)
79-
object_type = ContentTypeChoiceField(
79+
object_type_id = ContentTypeChoiceField(
8080
label=_('Object Type'),
8181
queryset=ObjectType.objects.with_feature('jobs'),
8282
required=False,

0 commit comments

Comments
 (0)