Coverage for  / home / runner / work / netbox-InterfaceNameRules-plugin / netbox-InterfaceNameRules-plugin / netbox-InterfaceNameRules-plugin / netbox_interface_name_rules / filters.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-02 15:19 +0000

1# SPDX-License-Identifier: Apache-2.0 

2# Copyright (C) 2025 Marcin Zieba <marcinpsk@gmail.com> 

3import django_filters 

4from dcim.models import DeviceType, ModuleType, Platform 

5from django.db.models import Q 

6from netbox.filtersets import NetBoxModelFilterSet 

7 

8from .models import InterfaceNameRule 

9 

10 

11class InterfaceNameRuleFilterSet(NetBoxModelFilterSet): 

12 """FilterSet for InterfaceNameRule list/API filtering.""" 

13 

14 q = django_filters.CharFilter(method="search", label="Search") 

15 

16 module_type_id = django_filters.ModelChoiceFilter( 

17 queryset=ModuleType.objects.all(), 

18 field_name="module_type", 

19 label="Module Type", 

20 ) 

21 module_type_is_regex = django_filters.BooleanFilter(label="Regex Mode") 

22 enabled = django_filters.BooleanFilter(label="Enabled") 

23 applies_to_device_interfaces = django_filters.BooleanFilter(label="Device Interface Rules") 

24 module_type_pattern = django_filters.CharFilter(lookup_expr="icontains", label="Pattern") 

25 parent_module_type_id = django_filters.ModelChoiceFilter( 

26 queryset=ModuleType.objects.all(), 

27 field_name="parent_module_type", 

28 label="Parent Module Type", 

29 ) 

30 device_type_id = django_filters.ModelChoiceFilter( 

31 queryset=DeviceType.objects.all(), 

32 field_name="device_type", 

33 label="Device Type", 

34 ) 

35 platform_id = django_filters.ModelChoiceFilter( 

36 queryset=Platform.objects.all(), 

37 field_name="platform", 

38 label="Platform", 

39 ) 

40 

41 class Meta: 

42 model = InterfaceNameRule 

43 fields = [ 

44 "module_type_id", 

45 "module_type_is_regex", 

46 "applies_to_device_interfaces", 

47 "module_type_pattern", 

48 "parent_module_type_id", 

49 "device_type_id", 

50 "platform_id", 

51 "enabled", 

52 ] 

53 

54 def search(self, queryset, name, value): 

55 """Filter by pattern, template, description, or module type model name.""" 

56 return queryset.select_related("module_type").filter( 

57 Q(module_type_pattern__icontains=value) 

58 | Q(name_template__icontains=value) 

59 | Q(description__icontains=value) 

60 | Q(module_type__model__icontains=value) 

61 )