Skip to content

Fix incorrect filters on Circuit Maintenance list views (#377) - #391

Open
jvanderaa wants to merge 4 commits into
developfrom
377-fix-list-view-filters
Open

Fix incorrect filters on Circuit Maintenance list views (#377)#391
jvanderaa wants to merge 4 commits into
developfrom
377-fix-list-view-filters

Conversation

@jvanderaa

Copy link
Copy Markdown
Collaborator

Closes: #377

What's Changed

Fixes incorrect filters on the Circuit Maintenance, Circuit Impact, and Note list views. Several filterset filters referenced model fields that don't exist (raising FieldError), used the wrong Circuit natural key, or were missing entirely, and two filter-form fields were free-text where the underlying filter expects choices (producing "Invalid filters were specified").

Fixes

Filter Problem Fix
CircuitMaintenance.provider field_name="provider" — no such field on the model → FieldError Traverse circuitimpact__circuit__provider with distinct=True
CircuitMaintenance.circuit field_name="circuit" (no such field) + default to_field_name="name" (Circuit uses cid) Traverse circuitimpact__circuit, to_field_name="cid", distinct=True
CircuitImpact.q Filter form had a q field but the filterset had no q search filter Added a SearchFilter (searches circuit__cid, maintenance__name)
CircuitImpact.circuit Default to_field_name="name" invalid for Circuit to_field_name="cid"
CircuitImpact.impact (form) Free-text CharField fed invalid values to a choice-based filter MultipleChoiceField(choices=CircuitImpactChoices)
Note.level (form) Same as above; title/comment filter fields were also required MultipleChoiceField(choices=NoteLevelChoices); all Note filter fields made optional

On the "Sort Columns" part of the issue

Investigated — no defect found. The only property-backed table columns (circuits, providers on CircuitMaintenanceTable) are already orderable=False (django-tables2 auto-detects this), and sorting the list view by them returns HTTP 200. No change was needed there.

Screenshots

Circuit Maintenance — filter by provider (before: server error → after: filtered results)

Before After
provider before provider after

Circuit Impact — Impact filter field (before: free-text → after: multi-select of valid choices)

Before After
impact before impact after

Note — Level filter field (before: free-text → after: multi-select of valid choices)

Before After
level before level after

Testing

  • Added nautobot_circuit_maintenance/tests/test_filters.py with 10 regression tests covering each filter and filter-form fix (all failed before, pass after).
  • No regressions: test_views (195), test_api (8), test_graphql (4) all pass.
  • ruff lint + format clean; no missing migrations.

CircuitMaintenance provider/circuit filters referenced fields that do not
exist on the model; filter them through the circuitimpact -> circuit
relationship (with distinct) instead. Correct the Circuit natural key
lookups to use cid rather than the default name, add the missing q search
filter to CircuitImpactFilterSet, and switch the impact/level filter form
fields to MultipleChoiceField so choice values validate (no more
'Invalid filters were specified'). Make the remaining Note filter form
fields optional.

Adds regression tests covering each filter and filter-form fix.

Closes #377
Set shared test fixtures directly on the test class instead of returning
and dereferencing a dict, and drop the redundant field_name="circuit" on
CircuitImpactFilterSet.circuit (it matches the filter name).
@github-actions

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  nautobot_circuit_maintenance
  filters.py
  forms.py
Project Total  

This report was generated by python-coverage-comment-action

Revert the shared fixtures from external cls-attribute assignment back to a
returned dict accessed via self.data[...]. pylint cannot track attributes
assigned inside a free helper function, so the previous form raised E1101
(no-member) in CI; dict subscripting is pylint-clean and keeps the helper DRY.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it intentional to commit these images to the PR branch? Not sure I see their value here.

}


class CircuitMaintenanceFilterSetTestCase(TestCase):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should inherit from nautobot.apps.testing.FilterTestCases.FilterTestCase

Comment on lines +74 to +86
def test_provider_filter(self):
"""Filtering maintenances by the related circuit's provider (natural key) resolves and is scoped."""
params = {"provider": [self.data["provider_1"].name]}
result = self.filterset(params, self.queryset).qs
self.assertIn(self.data["maintenance_1"], result)
self.assertNotIn(self.data["maintenance_2"], result)

def test_circuit_filter(self):
"""Filtering maintenances by related circuit (cid natural key) resolves and is scoped."""
params = {"circuit": [self.data["circuit_1"].cid]}
result = self.filterset(params, self.queryset).qs
self.assertIn(self.data["maintenance_1"], result)
self.assertNotIn(self.data["maintenance_2"], result)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be covered instead (after addressing the inheritance comment above) by defining appropriate generic_filter_tests on the class, reducing the need for bespoke test code.

queryset=Provider.objects.all(),
to_field_name="name",
label="Provider",
distinct=True,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm whether distinct=True is actually needed here and below? It does add some overhead if unneeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix Incorrect Filters and Sort Columns on List Views

2 participants