mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-09 01:49:35 -06:00
feat(filtersets): Add object_type_id filter for Jobs (#20674)
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
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
This commit is contained in:
parent
3cdc6251be
commit
87ff83ef1f
@ -1,8 +1,13 @@
|
|||||||
|
from drf_spectacular.utils import extend_schema_field
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
from core.choices import *
|
from core.choices import *
|
||||||
from core.models import Job
|
from core.models import Job
|
||||||
|
from netbox.api.exceptions import SerializerNotFound
|
||||||
from netbox.api.fields import ChoiceField, ContentTypeField
|
from netbox.api.fields import ChoiceField, ContentTypeField
|
||||||
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__ = (
|
||||||
'JobSerializer',
|
'JobSerializer',
|
||||||
@ -18,11 +23,28 @@ class JobSerializer(BaseModelSerializer):
|
|||||||
object_type = ContentTypeField(
|
object_type = ContentTypeField(
|
||||||
read_only=True
|
read_only=True
|
||||||
)
|
)
|
||||||
|
object = serializers.SerializerMethodField(
|
||||||
|
read_only=True
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Job
|
model = Job
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'url', 'display_url', 'display', 'object_type', 'object_id', 'name', 'status', 'created', 'scheduled',
|
'id', 'url', 'display_url', 'display', 'object_type', 'object_id', 'object', 'name', 'status', 'created',
|
||||||
'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', 'log_entries',
|
'scheduled', 'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', 'log_entries',
|
||||||
]
|
]
|
||||||
brief_fields = ('url', 'created', 'completed', 'user', 'status')
|
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
|
||||||
|
|||||||
@ -80,6 +80,10 @@ class JobFilterSet(BaseFilterSet):
|
|||||||
method='search',
|
method='search',
|
||||||
label=_('Search'),
|
label=_('Search'),
|
||||||
)
|
)
|
||||||
|
object_type_id = django_filters.ModelMultipleChoiceFilter(
|
||||||
|
queryset=ObjectType.objects.with_feature('jobs'),
|
||||||
|
field_name='object_type_id',
|
||||||
|
)
|
||||||
object_type = ContentTypeFilter()
|
object_type = ContentTypeFilter()
|
||||||
created = django_filters.DateTimeFilter()
|
created = django_filters.DateTimeFilter()
|
||||||
created__before = django_filters.DateTimeFilter(
|
created__before = django_filters.DateTimeFilter(
|
||||||
@ -124,7 +128,7 @@ class JobFilterSet(BaseFilterSet):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Job
|
model = Job
|
||||||
fields = ('id', 'object_type', 'object_id', 'name', 'interval', 'status', 'user', 'job_id')
|
fields = ('id', 'object_type', 'object_type_id', 'object_id', 'name', 'interval', 'status', 'user', 'job_id')
|
||||||
|
|
||||||
def search(self, queryset, name, value):
|
def search(self, queryset, name, value):
|
||||||
if not value.strip():
|
if not value.strip():
|
||||||
|
|||||||
@ -70,13 +70,13 @@ class JobFilterForm(SavedFiltersMixin, FilterForm):
|
|||||||
model = Job
|
model = Job
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
FieldSet('q', 'filter_id'),
|
FieldSet('q', 'filter_id'),
|
||||||
FieldSet('object_type', 'status', name=_('Attributes')),
|
FieldSet('object_type_id', 'status', name=_('Attributes')),
|
||||||
FieldSet(
|
FieldSet(
|
||||||
'created__before', 'created__after', 'scheduled__before', 'scheduled__after', 'started__before',
|
'created__before', 'created__after', 'scheduled__before', 'scheduled__after', 'started__before',
|
||||||
'started__after', 'completed__before', 'completed__after', 'user', name=_('Creation')
|
'started__after', 'completed__before', 'completed__after', 'user', name=_('Creation')
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
object_type = ContentTypeChoiceField(
|
object_type_id = ContentTypeChoiceField(
|
||||||
label=_('Object Type'),
|
label=_('Object Type'),
|
||||||
queryset=ObjectType.objects.with_feature('jobs'),
|
queryset=ObjectType.objects.with_feature('jobs'),
|
||||||
required=False,
|
required=False,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user