mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-27 11:17:27 +02:00
@@ -1,6 +1,7 @@
|
||||
import django_filters
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ValidationError
|
||||
from django_filters.constants import EMPTY_VALUES
|
||||
from drf_spectacular.types import OpenApiTypes
|
||||
@@ -10,6 +11,7 @@ __all__ = (
|
||||
'ContentTypeFilter',
|
||||
'MultiValueArrayFilter',
|
||||
'MultiValueCharFilter',
|
||||
'MultiValueContentTypeFilter',
|
||||
'MultiValueDateFilter',
|
||||
'MultiValueDateTimeFilter',
|
||||
'MultiValueDecimalFilter',
|
||||
@@ -171,3 +173,27 @@ class ContentTypeFilter(django_filters.CharFilter):
|
||||
f'{self.field_name}__model': model
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class MultiValueContentTypeFilter(MultiValueCharFilter):
|
||||
"""
|
||||
A multi-value version of ContentTypeFilter.
|
||||
"""
|
||||
def filter(self, qs, value):
|
||||
if value in EMPTY_VALUES:
|
||||
return qs
|
||||
|
||||
content_types = []
|
||||
for key in value:
|
||||
try:
|
||||
app_label, model = key.lower().split('.')
|
||||
ct = ContentType.objects.get_by_natural_key(app_label, model)
|
||||
content_types.append(ct)
|
||||
except (ValueError, ContentType.DoesNotExist):
|
||||
continue
|
||||
|
||||
return qs.filter(
|
||||
**{
|
||||
f'{self.field_name}__in': content_types,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ from mptt.models import MPTTModel
|
||||
from taggit.managers import TaggableManager
|
||||
|
||||
from extras.filters import TagFilter
|
||||
from utilities.filters import ContentTypeFilter, TreeNodeMultipleChoiceFilter
|
||||
from utilities.filters import MultiValueContentTypeFilter, TreeNodeMultipleChoiceFilter
|
||||
|
||||
__all__ = (
|
||||
'BaseFilterSetTests',
|
||||
@@ -75,7 +75,7 @@ class BaseFilterSetTests:
|
||||
# Standardize on object_type for filter name even though it's technically a ContentType
|
||||
filter_name = 'object_type'
|
||||
return [
|
||||
(filter_name, ContentTypeFilter),
|
||||
(filter_name, MultiValueContentTypeFilter),
|
||||
(f'{filter_name}_id', django_filters.ModelMultipleChoiceFilter),
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user