content: import django_filters
  from django.contrib.contenttypes.models import ContentType
  from django.utils.translation import gettext as _
  from drf_spectacular.types import OpenApiTypes
  from drf_spectacular.utils import extend_schema_field
  
  from circuits.models import CircuitTermination, VirtualCircuit, VirtualCircuitTermination
  from extras.filtersets import LocalConfigContextFilterSet
  from extras.models import ConfigTemplate
  from ipam.filtersets import PrimaryIPFilterSet
  from ipam.models import ASN, IPAddress, VLANTranslationPolicy, VRF
  from netbox.choices import ColorChoices
  from netbox.filtersets import (
      AttributeFiltersMixin, BaseFilterSet, ChangeLoggedModelFilterSet, NestedGroupModelFilterSet, NetBoxModelFilterSet,
      OrganizationalModelFilterSet,
  )
  from tenancy.filtersets import ContactModelFilterSet, TenancyFilterSet
  from tenancy.models import *
  from users.models import User
  from utilities.filters import (
      ContentTypeFilter, MultiValueCharFilter, MultiValueMACAddressFilter, MultiValueNumberFilter, MultiValueWWNFilter,
      NumericArrayFilter, TreeNodeMultipleChoiceFilter,
  )
  from virtualization.models import Cluster, ClusterGroup, VirtualMachine, VMInterface
  from vpn.models import L2VPN
  from wireless.choices import WirelessChannelChoices, WirelessRoleChoices
  from wireless.models import WirelessLAN, WirelessLink
  from .choices import *
  from .constants import *
  from .models import *
  
  __all__ = (
      'CableFilterSet',
      'CabledObjectFilterSet',
      'CableTerminationFilterSet',
      'CommonInterfaceFilterSet',
      'ConsoleConnectionFilterSet',
      'ConsolePortFilterSet',
      'ConsolePortTemplateFilterSet',
      'ConsoleServerPortFilterSet',
      'ConsoleServerPortTemplateFilterSet',
      'DeviceBayFilterSet',
      'DeviceBayTemplateFilterSet',
      'DeviceFilterSet',
      'DeviceRoleFilterSet',
      'DeviceTypeFilterSet',
      'FrontPortFilterSet',
      'FrontPortTemplateFilterSet',
      'InterfaceConnectionFilterSet',
      'InterfaceFilterSet',
      'InterfaceTemplateFilterSet',
      'InventoryItemFilterSet',
      'InventoryItemRoleFilterSet',
      'InventoryItemTemplateFilterSet',
      'LocationFilterSet',
      'MACAddressFilterSet',
      'ManufacturerFilterSet',
      'ModuleBayFilterSet',
      'ModuleBayTemplateFilterSet',
      'ModuleFilterSet',
      'ModuleTypeFilterSet',
      'ModuleTypeProfileFilterSet',
      'PathEndpointFilterSet',
      'PlatformFilterSet',
      'PowerConnectionFilterSet',
      'PowerFeedFilterSet',
      'PowerOutletFilterSet',
      'PowerOutletTemplateFilterSet',
      'PowerPanelFilterSet',
      'PowerPortFilterSet',
      'PowerPortTemplateFilterSet',
      'RackFilterSet',
      'RackReservationFilterSet',
      'RackRoleFilterSet',
      'RackTypeFilterSet',
      'RearPortFilterSet',
      'RearPortTemplateFilterSet',
      'RegionFilterSet',
      'SiteFilterSet',
      'SiteGroupFilterSet',
      'VirtualChassisFilterSet',
      'VirtualDeviceContextFilterSet',
  )
  
  
  class RegionFilterSet(NestedGroupModelFilterSet, ContactModelFilterSet):
      parent_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Region.objects.all(),
          label=_('Parent region (ID)'),
      )
      parent = django_filters.ModelMultipleChoiceFilter(
          field_name='parent__slug',
          queryset=Region.objects.all(),
          to_field_name='slug',
          label=_('Parent region (slug)'),
      )
      ancestor_id = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='parent',
          lookup_expr='in',
          label=_('Region (ID)'),
      )
      ancestor = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='parent',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Region (slug)'),
      )
  
      class Meta:
          model = Region
          fields = ('id', 'name', 'slug', 'description')
  
  
  class SiteGroupFilterSet(NestedGroupModelFilterSet, ContactModelFilterSet):
      parent_id = django_filters.ModelMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          label=_('Parent site group (ID)'),
      )
      parent = django_filters.ModelMultipleChoiceFilter(
          field_name='parent__slug',
          queryset=SiteGroup.objects.all(),
          to_field_name='slug',
          label=_('Parent site group (slug)'),
      )
      ancestor_id = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='parent',
          lookup_expr='in',
          label=_('Site group (ID)'),
      )
      ancestor = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='parent',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Site group (slug)'),
      )
  
      class Meta:
          model = SiteGroup
          fields = ('id', 'name', 'slug', 'description')
  
  
  class SiteFilterSet(NetBoxModelFilterSet, TenancyFilterSet, ContactModelFilterSet):
      status = django_filters.MultipleChoiceFilter(
          choices=SiteStatusChoices,
          null_value=None
      )
      region_id = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='region',
          lookup_expr='in',
          label=_('Region (ID)'),
      )
      region = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          lookup_expr='in',
          to_field_name='slug',
          label=_('Region (slug)'),
      )
      group_id = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='group',
          lookup_expr='in',
          label=_('Group (ID)'),
      )
      group = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          lookup_expr='in',
          to_field_name='slug',
          label=_('Group (slug)'),
      )
      asn = django_filters.ModelMultipleChoiceFilter(
          field_name='asns__asn',
          queryset=ASN.objects.all(),
          to_field_name='asn',
          label=_('AS (ID)'),
      )
      asn_id = django_filters.ModelMultipleChoiceFilter(
          field_name='asns',
          queryset=ASN.objects.all(),
          label=_('AS (ID)'),
      )
      time_zone = MultiValueCharFilter()
  
      class Meta:
          model = Site
          fields = ('id', 'name', 'slug', 'facility', 'latitude', 'longitude', 'description')
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          qs_filter = (
              Q(name__icontains=value) |
              Q(facility__icontains=value) |
              Q(description__icontains=value) |
              Q(physical_address__icontains=value) |
              Q(shipping_address__icontains=value) |
              Q(comments__icontains=value)
          )
          try:
              qs_filter |= Q(asns__asn=int(value.strip()))
          except ValueError:
              pass
          return queryset.filter(qs_filter).distinct()
  
  
  class LocationFilterSet(TenancyFilterSet, ContactModelFilterSet, NestedGroupModelFilterSet):
      region_id = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='site__region',
          lookup_expr='in',
          label=_('Region (ID)'),
      )
      region = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='site__region',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Region (slug)'),
      )
      site_group_id = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='site__group',
          lookup_expr='in',
          label=_('Site group (ID)'),
      )
      site_group = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='site__group',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Site group (slug)'),
      )
      site_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Site.objects.all(),
          label=_('Site (ID)'),
      )
      site = django_filters.ModelMultipleChoiceFilter(
          field_name='site__slug',
          queryset=Site.objects.all(),
          to_field_name='slug',
          label=_('Site (slug)'),
      )
      parent_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Location.objects.all(),
          label=_('Parent location (ID)'),
      )
      parent = django_filters.ModelMultipleChoiceFilter(
          field_name='parent__slug',
          queryset=Location.objects.all(),
          to_field_name='slug',
          label=_('Parent location (slug)'),
      )
      ancestor_id = TreeNodeMultipleChoiceFilter(
          queryset=Location.objects.all(),
          field_name='parent',
          lookup_expr='in',
          label=_('Location (ID)'),
      )
      ancestor = TreeNodeMultipleChoiceFilter(
          queryset=Location.objects.all(),
          field_name='parent',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Location (slug)'),
      )
      status = django_filters.MultipleChoiceFilter(
          choices=LocationStatusChoices,
          null_value=None
      )
  
      class Meta:
          model = Location
          fields = ('id', 'name', 'slug', 'facility', 'description')
  
      def search(self, queryset, name, value):
          # extended in order to include querying on Location.facility
          queryset = super().search(queryset, name, value)
  
          if value.strip():
              queryset = queryset | queryset.model.objects.filter(facility__icontains=value)
  
          return queryset
  
  
  class RackRoleFilterSet(OrganizationalModelFilterSet):
  
      class Meta:
          model = RackRole
          fields = ('id', 'name', 'slug', 'color', 'description')
  
  
  class RackTypeFilterSet(NetBoxModelFilterSet):
      manufacturer_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Manufacturer.objects.all(),
          label=_('Manufacturer (ID)'),
      )
      manufacturer = django_filters.ModelMultipleChoiceFilter(
          field_name='manufacturer__slug',
          queryset=Manufacturer.objects.all(),
          to_field_name='slug',
          label=_('Manufacturer (slug)'),
      )
      form_factor = django_filters.MultipleChoiceFilter(
          choices=RackFormFactorChoices
      )
      width = django_filters.MultipleChoiceFilter(
          choices=RackWidthChoices
      )
  
      class Meta:
          model = RackType
          fields = (
              'id', 'model', 'slug', 'u_height', 'starting_unit', 'desc_units', 'outer_width', 'outer_height',
              'outer_depth', 'outer_unit', 'mounting_depth', 'weight', 'max_weight', 'weight_unit', 'description',
          )
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(model__icontains=value) |
              Q(description__icontains=value) |
              Q(comments__icontains=value)
          )
  
  
  class RackFilterSet(NetBoxModelFilterSet, TenancyFilterSet, ContactModelFilterSet):
      region_id = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='site__region',
          lookup_expr='in',
          label=_('Region (ID)'),
      )
      region = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='site__region',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Region (slug)'),
      )
      site_group_id = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='site__group',
          lookup_expr='in',
          label=_('Site group (ID)'),
      )
      site_group = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='site__group',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Site group (slug)'),
      )
      site_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Site.objects.all(),
          label=_('Site (ID)'),
      )
      site = django_filters.ModelMultipleChoiceFilter(
          field_name='site__slug',
          queryset=Site.objects.all(),
          to_field_name='slug',
          label=_('Site (slug)'),
      )
      location_id = TreeNodeMultipleChoiceFilter(
          queryset=Location.objects.all(),
          field_name='location',
          lookup_expr='in',
          label=_('Location (ID)'),
      )
      location = TreeNodeMultipleChoiceFilter(
          queryset=Location.objects.all(),
          field_name='location',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Location (slug)'),
      )
      manufacturer_id = django_filters.ModelMultipleChoiceFilter(
          field_name='rack_type__manufacturer',
          queryset=Manufacturer.objects.all(),
          label=_('Manufacturer (ID)'),
      )
      manufacturer = django_filters.ModelMultipleChoiceFilter(
          field_name='rack_type__manufacturer__slug',
          queryset=Manufacturer.objects.all(),
          to_field_name='slug',
          label=_('Manufacturer (slug)'),
      )
      rack_type = django_filters.ModelMultipleChoiceFilter(
          field_name='rack_type__slug',
          queryset=RackType.objects.all(),
          to_field_name='slug',
          label=_('Rack type (slug)'),
      )
      rack_type_id = django_filters.ModelMultipleChoiceFilter(
          queryset=RackType.objects.all(),
          label=_('Rack type (ID)'),
      )
      status = django_filters.MultipleChoiceFilter(
          choices=RackStatusChoices,
          null_value=None
      )
      form_factor = django_filters.MultipleChoiceFilter(
          choices=RackFormFactorChoices
      )
      width = django_filters.MultipleChoiceFilter(
          choices=RackWidthChoices
      )
      role_id = django_filters.ModelMultipleChoiceFilter(
          queryset=RackRole.objects.all(),
          label=_('Role (ID)'),
      )
      role = django_filters.ModelMultipleChoiceFilter(
          field_name='role__slug',
          queryset=RackRole.objects.all(),
          to_field_name='slug',
          label=_('Role (slug)'),
      )
      serial = MultiValueCharFilter(
          lookup_expr='iexact'
      )
  
      class Meta:
          model = Rack
          fields = (
              'id', 'name', 'facility_id', 'asset_tag', 'u_height', 'starting_unit', 'desc_units', 'outer_width',
              'outer_height', 'outer_depth', 'outer_unit', 'mounting_depth', 'airflow', 'weight', 'max_weight',
              'weight_unit', 'description',
          )
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(name__icontains=value) |
              Q(facility_id__icontains=value) |
              Q(serial__icontains=value.strip()) |
              Q(asset_tag__icontains=value.strip()) |
              Q(description__icontains=value) |
              Q(comments__icontains=value)
          )
  
  
  class RackReservationFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
      rack_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Rack.objects.all(),
          label=_('Rack (ID)'),
      )
      site_id = django_filters.ModelMultipleChoiceFilter(
          field_name='rack__site',
          queryset=Site.objects.all(),
          label=_('Site (ID)'),
      )
      site = django_filters.ModelMultipleChoiceFilter(
          field_name='rack__site__slug',
          queryset=Site.objects.all(),
          to_field_name='slug',
          label=_('Site (slug)'),
      )
      region_id = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='rack__site__region',
          lookup_expr='in',
          label=_('Region (ID)'),
      )
      region = TreeNodeMultipleChoiceFilter(
          queryset=Region.objects.all(),
          field_name='rack__site__region',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Region (slug)'),
      )
      site_group_id = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='rack__site__group',
          lookup_expr='in',
          label=_('Site group (ID)'),
      )
      site_group = TreeNodeMultipleChoiceFilter(
          queryset=SiteGroup.objects.all(),
          field_name='rack__site__group',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Site group (slug)'),
      )
      location_id = TreeNodeMultipleChoiceFilter(
          queryset=Location.objects.all(),
          field_name='rack__location',
          lookup_expr='in',
          label=_('Location (ID)'),
      )
      location = TreeNodeMultipleChoiceFilter(
          queryset=Location.objects.all(),
          field_name='rack__location',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Location (slug)'),
      )
      status = django_filters.MultipleChoiceFilter(
          choices=RackReservationStatusChoices,
          null_value=None
      )
      user_id = django_filters.ModelMultipleChoiceFilter(
          queryset=User.objects.all(),
          label=_('User (ID)'),
      )
      user = django_filters.ModelMultipleChoiceFilter(
          field_name='user__username',
          queryset=User.objects.all(),
          to_field_name='username',
          label=_('User (name)'),
      )
      unit = NumericArrayFilter(
          field_name='units',
          lookup_expr='contains'
      )
  
      class Meta:
          model = RackReservation
          fields = ('id', 'created', 'description')
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(rack__name__icontains=value) |
              Q(rack__facility_id__icontains=value) |
              Q(user__username__icontains=value) |
              Q(description__icontains=value)
          )
  
  
  class ManufacturerFilterSet(OrganizationalModelFilterSet, ContactModelFilterSet):
  
      class Meta:
          model = Manufacturer
          fields = ('id', 'name', 'slug', 'description')
  
  
  class DeviceTypeFilterSet(NetBoxModelFilterSet):
      manufacturer_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Manufacturer.objects.all(),
          label=_('Manufacturer (ID)'),
      )
      manufacturer = django_filters.ModelMultipleChoiceFilter(
          field_name='manufacturer__slug',
          queryset=Manufacturer.objects.all(),
          to_field_name='slug',
          label=_('Manufacturer (slug)'),
      )
      default_platform_id = TreeNodeMultipleChoiceFilter(
          queryset=Platform.objects.all(),
          field_name='default_platform',
          lookup_expr='in',
          label=_('Default platform (ID)'),
      )
      default_platform = TreeNodeMultipleChoiceFilter(
          queryset=Platform.objects.all(),
          field_name='default_platform',
          to_field_name='slug',
          lookup_expr='in',
          label=_('Default platform (slug)'),
      )
      has_front_image = django_filters.BooleanFilter(
          label=_('Has a front image'),
          method='_has_front_image'
      )
      has_rear_image = django_filters.BooleanFilter(
          label=_('Has a rear image'),
          method='_has_rear_image'
      )
      console_ports = django_filters.BooleanFilter(
          method='_console_ports',
          label=_('Has console ports'),
      )
      console_server_ports = django_filters.BooleanFilter(
          method='_console_server_ports',
          label=_('Has console server ports'),
      )
      power_ports = django_filters.BooleanFilter(
          method='_power_ports',
          label=_('Has power ports'),
      )
      power_outlets = django_filters.BooleanFilter(
          method='_power_outlets',
          label=_('Has power outlets'),
      )
      interfaces = django_filters.BooleanFilter(
          method='_interfaces',
          label=_('Has interfaces'),
      )
      pass_through_ports = django_filters.BooleanFilter(
          method='_pass_through_ports',
          label=_('Has pass-through ports'),
      )
      module_bays = django_filters.BooleanFilter(
          method='_module_bays',
          label=_('Has module bays'),
      )
      device_bays = django_filters.BooleanFilter(
          method='_device_bays',
          label=_('Has device bays'),
      )
      inventory_items = django_filters.BooleanFilter(
          method='_inventory_items',
          label=_('Has inventory items'),
      )
  
      class Meta:
          model = DeviceType
          fields = (
              'id', 'model', 'slug', 'part_number', 'u_height', 'exclude_from_utilization', 'is_full_depth',
              'subdevice_role', 'airflow', 'weight', 'weight_unit', 'description',
  
              # Counters
              'console_port_template_count',
              'console_server_port_template_count',
              'power_port_template_count',
              'power_outlet_template_count',
              'interface_template_count',
              'front_port_template_count',
              'rear_port_template_count',
              'device_bay_template_count',
              'module_bay_template_count',
              'inventory_item_template_count',
          )
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(manufacturer__name__icontains=value) |
              Q(model__icontains=value) |
              Q(part_number__icontains=value) |
              Q(description__icontains=value) |
              Q(comments__icontains=value)
          )
  
      def _has_front_image(self, queryset, name, value):
          if value:
              return queryset.exclude(front_image='')
          else:
              return queryset.filter(front_image='')
  
      def _has_rear_image(self, queryset, name, value):
          if value:
              return queryset.exclude(rear_image='')
          else:
              return queryset.filter(rear_image='')
  
      def _console_ports(self, queryset, name, value):
          return queryset.exclude(consoleporttemplates__isnull=value)
  
      def _console_server_ports(self, queryset, name, value):
          return queryset.exclude(consoleserverporttemplates__isnull=value)
  
      def _power_ports(self, queryset, name, value):
          return queryset.exclude(powerporttemplates__isnull=value)
  
      def _power_outlets(self, queryset, name, value):
          return queryset.exclude(poweroutlettemplates__isnull=value)
  
      def _interfaces(self, queryset, name, value):
          return queryset.exclude(interfacetemplates__isnull=value)
  
      def _pass_through_ports(self, queryset, name, value):
          return queryset.exclude(
              frontporttemplates__isnull=value,
              rearporttemplates__isnull=value
          )
  
      def _module_bays(self, queryset, name, value):
          return queryset.exclude(modulebaytemplates__isnull=value)
  
      def _device_bays(self, queryset, name, value):
          return queryset.exclude(devicebaytemplates__isnull=value)
  
      def _inventory_items(self, queryset, name, value):
          return queryset.exclude(inventoryitemtemplates__isnull=value)
  
  
  class ModuleTypeProfileFilterSet(NetBoxModelFilterSet):
  
      class Meta:
          model = ModuleTypeProfile
          fields = ('id', 'name', 'description')
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(name__icontains=value) |
              Q(description__icontains=value) |
              Q(comments__icontains=value)
          )
  
  
  class ModuleTypeFilterSet(AttributeFiltersMixin, NetBoxModelFilterSet):
      profile_id = django_filters.ModelMultipleChoiceFilter(
          queryset=ModuleTypeProfile.objects.all(),
          label=_('Profile (ID)'),
      )
      profile = django_filters.ModelMultipleChoiceFilter(
          field_name='profile__name',
          queryset=ModuleTypeProfile.objects.all(),
          to_field_name='name',
          label=_('Profile (name)'),
      )
      manufacturer_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Manufacturer.objects.all(),
          label=_('Manufacturer (ID)'),
      )
      manufacturer = django_filters.ModelMultipleChoiceFilter(
          field_name='manufacturer__slug',
          queryset=Manufacturer.objects.all(),
          to_field_name='slug',
          label=_('Manufacturer (slug)'),
      )
      console_ports = django_filters.BooleanFilter(
          method='_console_ports',
          label=_('Has console ports'),
      )
      console_server_ports = django_filters.BooleanFilter(
          method='_console_server_ports',
          label=_('Has console server ports'),
      )
      power_ports = django_filters.BooleanFilter(
          method='_power_ports',
          label=_('Has power ports'),
      )
      power_outlets = django_filters.BooleanFilter(
          method='_power_outlets',
          label=_('Has power outlets'),
      )
      interfaces = django_filters.BooleanFilter(
          method='_interfaces',
          label=_('Has interfaces'),
      )
      pass_through_ports = django_filters.BooleanFilter(
          method='_pass_through_ports',
          label=_('Has pass-through ports'),
      )
  
      class Meta:
          model = ModuleType
          fields = ('id', 'model', 'part_number', 'airflow', 'weight', 'weight_unit', 'description')
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(manufacturer__name__icontains=value) |
              Q(model__icontains=value) |
              Q(part_number__icontains=value) |
              Q(description__icontains=value) |
              Q(comments__icontains=value)
          )
  
      def _console_ports(self, queryset, name, value):
          return queryset.exclude(consoleporttemplates__isnull=value)
  
      def _console_server_ports(self, queryset, name, value):
          return queryset.exclude(consoleserverporttemplates__isnull=value)
  
      def _power_ports(self, queryset, name, value):
          return queryset.exclude(powerporttemplates__isnull=value)
  
      def _power_outlets(self, queryset, name, value):
          return queryset.exclude(poweroutlettemplates__isnull=value)
  
      def _interfaces(self, queryset, name, value):
          return queryset.exclude(interfacetemplates__isnull=value)
  
      def _pass_through_ports(self, queryset, name, value):
          return queryset.exclude(
              frontporttemplates__isnull=value,
              rearporttemplates__isnull=value
          )
  
  
  class DeviceTypeComponentFilterSet(django_filters.FilterSet):
      q = django_filters.CharFilter(
          method='search',
          label=_('Search'),
      )
      device_type_id = django_filters.ModelMultipleChoiceFilter(
          queryset=DeviceType.objects.all(),
          field_name='device_type_id',
          label=_('Device type (ID)'),
      )
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          return queryset.filter(
              Q(name__icontains=value) |
              Q(description__icontains=value)
          )
  
  
  class ModularDeviceTypeComponentFilterSet(DeviceTypeComponentFilterSet):
      module_type_id = django_filters.ModelMultipleChoiceFilter(
          queryset=ModuleType.objects.all(),
          field_name='module_type_id',
          label=_('Module type (ID)'),
      )
  
  
  class ConsolePortTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
  
      class Meta:
          model = ConsolePortTemplate
          fields = ('id', 'name', 'label', 'type', 'description')
  
  
  class ConsoleServerPortTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
  
      class Meta:
          model = ConsoleServerPortTemplate
          fields = ('id', 'name', 'label', 'type', 'description')
  
  
  class PowerPortTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
  
      class Meta:
          model = PowerPortTemplate
          fields = ('id', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description')
  
  
  class PowerOutletTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
      feed_leg = django_filters.MultipleChoiceFilter(
          choices=PowerOutletFeedLegChoices,
          null_value=None
      )
      power_port_id = django_filters.ModelMultipleChoiceFilter(
          queryset=PowerPortTemplate.objects.all(),
          label=_('Power port (ID)'),
      )
  
      class Meta:
          model = PowerOutletTemplate
          fields = ('id', 'name', 'label', 'type', 'feed_leg', 'description')
  
  
  class InterfaceTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
      type = django_filters.MultipleChoiceFilter(
          choices=InterfaceTypeChoices,
          null_value=None
      )
      bridge_id = django_filters.ModelMultipleChoiceFilter(
          field_name='bridge',
          queryset=InterfaceTemplate.objects.all()
      )
      poe_mode = django_filters.MultipleChoiceFilter(
          choices=InterfacePoEModeChoices
      )
      poe_type = django_filters.MultipleChoiceFilter(
          choices=InterfacePoETypeChoices
      )
      rf_role = django_filters.MultipleChoiceFilter(
          choices=WirelessRoleChoices
      )
  
      class Meta:
          model = InterfaceTemplate
          fields = ('id', 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description')
  
  
  class FrontPortTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
      type = django_filters.MultipleChoiceFilter(
          choices=PortTypeChoices,
          null_value=None
      )
      rear_port_id = django_filters.ModelMultipleChoiceFilter(
          queryset=RearPortTemplate.objects.all()
      )
  
      class Meta:
          model = FrontPortTemplate
          fields = ('id', 'name', 'label', 'type', 'color', 'rear_port_position', 'description')
  
  
  class RearPortTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
      type = django_filters.MultipleChoiceFilter(
          choices=PortTypeChoices,
          null_value=None
      )
  
      class Meta:
          model = RearPortTemplate
          fields = ('id', 'name', 'label', 'type', 'color', 'positions', 'description')
  
  
  class ModuleBayTemplateFilterSet(ChangeLoggedModelFilterSet, ModularDeviceTypeComponentFilterSet):
  
      class Meta:
          model = ModuleBayTemplate
          fields = ('id', 'name', 'label', 'position', 'description')
  
  
  class DeviceBayTemplateFilterSet(ChangeLoggedModelFilterSet, DeviceTypeComponentFilterSet):
  
      class Meta:
          model = DeviceBayTemplate
          fields = ('id', 'name', 'label', 'description')
  
  
  class InventoryItemTemplateFilterSet(ChangeLoggedModelFilterSet, DeviceTypeComponentFilterSet):
      parent_id = django_filters.ModelMultipleChoiceFilter(
          queryset=InventoryItemTemplate.objects.all(),
          label=_('Parent inventory item (ID)'),
      )
      manufacturer_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Manufacturer.objects.all(),
          label=_('Manufacturer (ID)'),
      )
      manufacturer = django_filters.ModelMultipleChoiceFilter(
          field_name='manufacturer__slug',
          queryset=Manufacturer.objects.all(),
          to_field_name='slug',
          label=_('Manufacturer (slug)'),
      )
      role_id = django_filters.ModelMultipleChoiceFilter(
          queryset=InventoryItemRole.objects.all(),
          label=_('Role (ID)'),
      )
      role = django_filters.ModelMultipleChoiceFilter(
          field_name='role__slug',
          queryset=InventoryItemRole.objects.all(),
          to_field_name='slug',
          label=_('Role (slug)'),
      )
      component_type = ContentTypeFilter()
      component_id = MultiValueNumberFilter()
  
      class Meta:
          model = InventoryItemTemplate
          fields = ('id', 'name', 'label', 'part_id', 'description')
  
      def search(self, queryset, name, value):
          if not value.strip():
              return queryset
          qs_filter = (
              Q(name__icontains=value) |
              Q(part_id__icontains=value) |
              Q(description__icontains=value)
          )
          return queryset.filter(qs_filter)
  
  
  class DeviceRoleFilterSet(OrganizationalModelFilterSet):
      config_template_id = django_filters.ModelMultipleChoiceFilter(
          queryset=ConfigTemplate.objects.all(),
          label=_('Config template (ID)'),
      )
      parent_id = django_filters.ModelMultipleChoiceFilter(
          queryset=DeviceRole.objects.all(),
          label=_('Parent device role (ID)'),
      )
      parent = django_filters.ModelMultipleChoiceFilter(
          field_name='parent__slug',
          queryset=DeviceRole.objects.all(),
          to_field_name='slug',
          label=_('Parent device role (slug)'),
      )
      ancestor_id = TreeNodeMultipleChoiceFilter(
          queryset=DeviceRole.objects.all(),
          field_name='parent',
          lookup_expr='in',
          label=_('Parent device role (ID)'),
      )
      ancestor = TreeNodeMultipleChoiceFilter(
          queryset=DeviceRole.objects.all(),
          field_name='parent',
          lookup_expr='in',
          to_field_name='slug',
          label=_('Parent device role (slug)'),
      )
  
      class Meta:
          model = DeviceRole
          fields = ('id', 'name', 'slug', 'color', 'vm_role', 'description')
  
  
  class PlatformFilterSet(OrganizationalModelFilterSet):
      parent_id = django_filters.ModelMultipleChoiceFilter(
          queryset=Platform.objects.all(),
          label=_('Immediate parent platform (ID)'),
      )
      parent = django_filters.ModelMultipleChoiceFilter(
          field_name='parent__slug',
          queryset=Platform.objects.all(),
  