Skip to content

Commit

Permalink
add role_filter to selector
Browse files Browse the repository at this point in the history
  • Loading branch information
amyasnikov committed Jan 22, 2025
1 parent 1633387 commit 16eeff6
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 35 deletions.
5 changes: 5 additions & 0 deletions validity/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from core.api.serializers import DataFileSerializer, DataSourceSerializer, JobSerializer
from core.models import DataSource
from dcim.api.serializers import (
DeviceRoleSerializer,
DeviceSerializer,
DeviceTypeSerializer,
LocationSerializer,
Expand Down Expand Up @@ -54,6 +55,9 @@ class ComplianceSelectorSerializer(NetBoxModelSerializer):
type_filter = SerializedPKRelatedField(
serializer=DeviceTypeSerializer, many=True, nested=True, required=False, queryset=DeviceType.objects.all()
)
role_filter = SerializedPKRelatedField(
serializer=DeviceRoleSerializer, many=True, nested=True, required=False, queryset=DeviceType.objects.all()
)
platform_filter = SerializedPKRelatedField(
serializer=PlatformSerializer, many=True, nested=True, required=False, queryset=Platform.objects.all()
)
Expand All @@ -79,6 +83,7 @@ class Meta:
"tag_filter",
"manufacturer_filter",
"type_filter",
"role_filter",
"platform_filter",
"status_filter",
"location_filter",
Expand Down
11 changes: 1 addition & 10 deletions validity/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ def run(self, request):


class ComplianceSelectorViewSet(NetBoxModelViewSet):
queryset = models.ComplianceSelector.objects.prefetch_related(
"tag_filter",
"manufacturer_filter",
"type_filter",
"platform_filter",
"location_filter",
"site_filter",
"tenant_filter",
"tags",
)
queryset = models.ComplianceSelector.objects.prefetch_filters().prefetch_related("tags")
serializer_class = serializers.ComplianceSelectorSerializer
filterset_class = filtersets.ComplianceSelectorFilterSet

Expand Down
7 changes: 6 additions & 1 deletion validity/forms/general.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from core.forms.mixins import SyncedDataMixin
from core.models import DataSource
from dcim.models import Device, DeviceType, Location, Manufacturer, Platform, Site
from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Site
from django.forms import BooleanField, CharField, ChoiceField, IntegerField, Select, Textarea, ValidationError
from django.utils.translation import gettext_lazy as _
from extras.forms import ScriptForm
Expand Down Expand Up @@ -40,6 +40,9 @@ class ComplianceSelectorForm(NetBoxModelForm):
type_filter = DynamicModelMultipleChoiceField(
queryset=DeviceType.objects.all(), required=False, label=_("Device Type Filter")
)
role_filter = DynamicModelMultipleChoiceField(
queryset=DeviceRole.objects.all(), required=False, label=_("Device Role Filter")
)
platform_filter = DynamicModelMultipleChoiceField(queryset=Platform.objects.all(), required=False)
location_filter = DynamicModelMultipleChoiceField(queryset=Location.objects.all(), required=False)
site_filter = DynamicModelMultipleChoiceField(queryset=Site.objects.all(), required=False)
Expand All @@ -52,6 +55,7 @@ class ComplianceSelectorForm(NetBoxModelForm):
"filter_operation",
"name_filter",
"type_filter",
"role_filter",
"location_filter",
"manufacturer_filter",
"platform_filter",
Expand All @@ -74,6 +78,7 @@ class Meta:
"tag_filter",
"manufacturer_filter",
"type_filter",
"role_filter",
"platform_filter",
"status_filter",
"location_filter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ class Migration(migrations.Migration):
},
bases=(validity.models.base.SubformMixin, validity.models.base.URLMixin, models.Model),
),
migrations.AddField(
model_name='complianceselector',
name='role_filter',
field=models.ManyToManyField(blank=True, related_name='+', to='dcim.devicerole'),
),
]
5 changes: 4 additions & 1 deletion validity/models/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Generator

from dcim.choices import DeviceStatusChoices
from dcim.models import Device, DeviceType, Location, Manufacturer, Platform, Site
from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Site
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -33,6 +33,7 @@ class ComplianceSelector(BaseModel):
Manufacturer, verbose_name=_("Manufacturer Filter"), blank=True, related_name="+"
)
type_filter = models.ManyToManyField(DeviceType, verbose_name=_("Device Type Filter"), blank=True, related_name="+")
role_filter = models.ManyToManyField(DeviceRole, verbose_name=_("Device Role Filter"), blank=True, related_name="+")
platform_filter = models.ManyToManyField(Platform, verbose_name=_("Platform Filter"), blank=True, related_name="+")
status_filter = models.CharField(max_length=50, choices=DeviceStatusChoices, blank=True)
location_filter = models.ManyToManyField(Location, verbose_name=_("Location Filter"), blank=True, related_name="+")
Expand All @@ -51,6 +52,7 @@ class ComplianceSelector(BaseModel):
"tag_filter",
"manufacturer_filter",
"type_filter",
"role_filter",
"platform_filter",
"status_filter",
"location_filter",
Expand All @@ -64,6 +66,7 @@ class ComplianceSelector(BaseModel):
"tag_filter": "tags",
"manufacturer_filter": "device_type__manufacturer",
"type_filter": "device_type",
"role_filter": "role",
"platform_filter": "platform",
"status_filter": "status",
"location_filter": "location",
Expand Down
2 changes: 2 additions & 0 deletions validity/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SelectorTable(NetBoxTable):
tag_filter = BooleanColumn(accessor="tag_filter__all")
manufacturer_filter = BooleanColumn(accessor="manufacturer_filter__all")
type_filter = BooleanColumn(accessor="type_filter__all")
role_filter = BooleanColumn(accessor="role_filter__all")
platform_filter = BooleanColumn(accessor="platform_filter__all")
status_filter = BooleanColumn(empty_values=())
location_filter = BooleanColumn(accessor="location_filter__all")
Expand All @@ -42,6 +43,7 @@ class Meta(NetBoxTable.Meta):
"tag_filter",
"manufacturer_filter",
"type_filter",
"role_filter",
"platform_filter",
"status_filter",
"location_filter",
Expand Down
26 changes: 15 additions & 11 deletions validity/templates/validity/complianceselector.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ <h5 class="card-header">Compliance Selector</h5>
</tr>
<tr>
<th scope="row">Dynamic Pairs</th>
<td><code>{{ object | colored_choice:"dynamic_pairs" }}</code></td>
<td>{{ object | colored_choice:"dynamic_pairs" }}</td>
</tr>
<tr>
<th scope="row">Dynamic Pair Tag Prefix</th>
<td><code>{{ object.dp_tag_prefix | placeholder }}</code></td>
<td>{{ object.dp_tag_prefix | placeholder }}</td>
</tr>
</table>
</div>
Expand All @@ -37,39 +37,43 @@ <h5 class="card-header">Filters</h5>
</tr>
<tr>
<th scope="row">Device Name Filter</th>
<td><code>{{ object.name_filter | placeholder }}</code></td>
<td>{{ object.name_filter | placeholder }}</td>
</tr>
<tr>
<th scope="row">Device Type Filter</th>
<td><code>{{ object.type_filter.all | linkify_list }}</code></td>
<td>{{ object.type_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Device Role Filter</th>
<td>{{ object.role_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Location Filter</th>
<td><code>{{ object.location_filter.all | linkify_list }}</code></td>
<td>{{ object.location_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Manufacturer Filter</th>
<td><code>{{ object.manufacturer_filter.all | linkify_list }}</code></td>
<td>{{ object.manufacturer_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Platform Filter</th>
<td><code>{{ object.platform_filter.all | linkify_list }}</code></td>
<td>{{ object.platform_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Site Filter</th>
<td><code>{{ object.site_filter.all | linkify_list }}</code></td>
<td>{{ object.site_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Status Filter</th>
<td><code>{{ object | colored_choice:"status_filter" | placeholder }}</code></td>
<td>{{ object | colored_choice:"status_filter" | placeholder }}</td>
</tr>
<tr>
<th scope="row">Tag Filter</th>
<td><code>{{ object.tag_filter.all | linkify_list }}</code></td>
<td>{{ object.tag_filter.all | linkify_list }}</td>
</tr>
<tr>
<th scope="row">Tenant Filter</th>
<td><code>{{ object.tenant_filter.all | linkify_list }}</code></td>
<td>{{ object.tenant_filter.all | linkify_list }}</td>
</tr>
</table>
</div>
Expand Down
2 changes: 2 additions & 0 deletions validity/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DataFileFactory,
DataSourceFactory,
DeviceFactory,
DeviceRoleFactory,
DeviceTypeFactory,
DSBackupJobFactory,
LocationFactory,
Expand Down Expand Up @@ -67,6 +68,7 @@ class TestSelector(ApiPostGetTest):
"tag_filter": [TagFactory, TagFactory],
"manufacturer_filter": [ManufacturerFactory, ManufacturerFactory],
"type_filter": [DeviceTypeFactory, DeviceTypeFactory],
"role_factory": [DeviceRoleFactory],
"platform_filter": [PlatformFactory, PlatformFactory],
"status_filter": "active",
"location_filter": [LocationFactory],
Expand Down
4 changes: 3 additions & 1 deletion validity/tests/test_models/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db.models import Q
from factories import (
DeviceFactory,
DeviceRoleFactory,
DeviceTypeFactory,
LocationFactory,
ManufacturerFactory,
Expand All @@ -28,13 +29,14 @@
"(AND: ('device_type__manufacturer', <Manufacturer: manufacturer-0>))",
),
("type_filter", [DeviceTypeFactory], "AND", "(AND: ('device_type', <DeviceType: model-0>))"),
("role_filter", [DeviceRoleFactory], "AND", "(AND: ('role', <DeviceRole: role-0>))"),
("platform_filter", [PlatformFactory], "OR", "(AND: ('platform', <Platform: platform-0>))"),
("status_filter", "ACTIVE", "OR", "(AND: ('status', 'ACTIVE'))"),
("location_filter", [LocationFactory], "AND", "(AND: ('location', <Location: location-0>))"),
("site_filter", [SiteFactory], "AND", "(AND: ('site', <Site: site-0>))"),
],
)
@pytest.mark.django_db
@pytest.mark.django_db(transaction=True, reset_sequences=True)
def test_filter(attr, attr_value, filter_operation, expected_filter):
model = SelectorFactory()
if isinstance(attr_value, list):
Expand Down
13 changes: 2 additions & 11 deletions validity/views/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@


class ComplianceSelectorListView(generic.ObjectListView):
queryset = models.ComplianceSelector.objects.all()
queryset = models.ComplianceSelector.objects.prefetch_filters()
table = tables.SelectorTable
filterset = filtersets.ComplianceSelectorFilterSet
filterset_form = forms.ComplianceSelectorFilterForm


@register_model_view(models.ComplianceSelector)
class ComplianceSelectorView(TableMixin, generic.ObjectView):
queryset = models.ComplianceSelector.objects.prefetch_related(
"tag_filter",
"manufacturer_filter",
"type_filter",
"platform_filter",
"location_filter",
"site_filter",
"tenant_filter",
"tags",
)
queryset = models.ComplianceSelector.objects.prefetch_filters().prefetch_related("tags")
filterset = DeviceFilterSet
object_table_field = "devices"
table = tables.DynamicPairsTable
Expand Down

0 comments on commit 16eeff6

Please sign in to comment.