Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate shadow fields and correct filter names #2847

Merged
merged 17 commits into from Nov 18, 2022

Conversation

HanlinMiao
Copy link
Contributor

@HanlinMiao HanlinMiao commented Nov 16, 2022

Closes: #2804

What's Changed

This PR is dedicated to:

  1. Collapse some of the *_id filters with NaturalKeyOrPKMultipleChoiceFilter
  2. Correct shadowed filters e.g. interfaces should not be the same as has_interfaces and should be its own NaturalKeyOrPKMultipleChoiceFilter
  3. Rename some of the filters and filter types.
  4. Not going to add missing filters

See also related PRs -
#2845 -- closed as duplicate to #2798
#2846 -- collapse site_id and region_id to site and region NaturalKeyOrPKMultipleChoiceFilter.

TODO

  • Explanation of Change(s)
  • Added change log fragment(s) (for more information see the documentation)
  • Attached Screenshots, Payload Example
  • Unit, Integration Tests
  • Documentation Updates (when adding/changing features)
  • Example Plugin Updates (when adding/changing features)
  • Outline Remaining Work, Constraints from Design

Tactical PR to merge into next via #2849

@@ -2253,6 +2253,11 @@ class DeviceFilterForm(
label="Has a primary IP",
widget=StaticSelect2(choices=BOOLEAN_WITH_BLANK_CHOICES),
)
has_interfaces = forms.NullBooleanField(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to add this to make DynamicGroupModelTest pass since DG derives the filters from filterset_form.

Copy link
Contributor

Choose a reason for hiding this comment

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

Something seems off here. You didn't change the has_interfaces filter definition, so why did this need to be added? And are there a bunch of other filters that also need to be added/updated in the forms now?

Copy link
Contributor

Choose a reason for hiding this comment

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

has_interfaces is a special case, since it was the one that sparked the “we shouldn’t shadow related names and change their intent/type” initiative when it was originally named interfaces. All of the other has_foo filters were added later if they didn't already exist. For DeviceFilterSet, however, I think this is the only one that didn't already have a has_foo form field because it would have been confusing in v1.x.

@bryanculver bryanculver mentioned this pull request Nov 16, 2022
6 tasks
@@ -1210,7 +1210,7 @@ def test_query_device_role_filter(self):
# pylint: disable=consider-using-f-string
"""
query {
devices(role: "%s") {
devices(device_role: "%s") {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering - with the proposed shift to a generic Role model replacing DeviceRole and others, maybe this should stay as role? Food for thought.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seconded.

queryset=RackGroup.objects.all(),
field_name="rack__group",
label="Rack group (ID)",
label="Rack group (slug or ID)",
)
rack_id = django_filters.ModelMultipleChoiceFilter(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should rack become a NaturalKeyOrPKMultipleChoiceFilter?

@@ -1002,11 +931,10 @@ class DeviceFilterSet(
queryset=Cluster.objects.all(),
label="VM cluster (ID)",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should cluster become a NaturalKeyOrPKMultipleChoiceFilter?

@@ -1002,11 +931,10 @@ class DeviceFilterSet(
queryset=Cluster.objects.all(),
label="VM cluster (ID)",
)
model = django_filters.ModelMultipleChoiceFilter(
field_name="device_type__slug",
model = NaturalKeyOrPKMultipleChoiceFilter(
Copy link
Contributor

Choose a reason for hiding this comment

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

This should become device_type, I believe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it does have a device_type filter defined on line 912? Could this be something different or a duplicate? Kinda looks like a duplicate.

queryset=SecretsGroup.objects.all(),
to_field_name="slug",
label="Secrets group (slug)",
label="Secrets group (slug or ID)",
)
virtual_chassis_id = django_filters.ModelMultipleChoiceFilter(
Copy link
Contributor

Choose a reason for hiding this comment

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

virtual_chassis as NaturalKeyOrPKMultipleChoiceFilter?

@@ -1156,23 +1118,19 @@ class DeviceComponentFilterSet(CustomFieldModelFilterSet):
to_field_name="slug",
Copy link
Contributor

Choose a reason for hiding this comment

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

Not changing the region and site filters? (Ah, I see - that's in #2846. It would be good to update the description of this PR to clearly indicate what it is and is not targeting for change.)

to_field_name="slug",
label="Role (slug)",
)
role = NaturalKeyOrPKMultipleChoiceFilter(queryset=RackRole.objects.all(), label="Role (slug or ID)")
serial = django_filters.CharFilter(lookup_expr="iexact")
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't serial need to be changed to a MultiValueCharFilter?

@@ -552,22 +526,17 @@ class RackReservationFilterSet(NautobotFilterSet, TenancyFilterSet):
field_name="rack__group",
Copy link
Contributor

Choose a reason for hiding this comment

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

elimination of group_id filter?

to_field_name="slug",
label="Manufacturer (slug)",
manufacturer = NaturalKeyOrPKMultipleChoiceFilter(
queryset=Manufacturer.objects.all(), label="Manufacturer (slug or ID)"
)
has_devices = RelatedMembershipBooleanFilter(
Copy link
Contributor

Choose a reason for hiding this comment

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

Addition of devices and virtual_machines filters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the scope of the PR is to fix incorrect filters and not adding missing ones. I will elaborate this in the description.

@@ -2253,6 +2253,11 @@ class DeviceFilterForm(
label="Has a primary IP",
widget=StaticSelect2(choices=BOOLEAN_WITH_BLANK_CHOICES),
)
has_interfaces = forms.NullBooleanField(
Copy link
Contributor

Choose a reason for hiding this comment

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

Something seems off here. You didn't change the has_interfaces filter definition, so why did this need to be added? And are there a bunch of other filters that also need to be added/updated in the forms now?

@@ -254,7 +248,7 @@ class SiteFilterSet(NautobotFilterSet, TenancyFilterSet, StatusModelFilterSetMix
label="Time zone",
null_value="",
)
tag = TagFilter()
tags = TagFilter()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please humor me and remove this definition from this filterset and see if it behaves the same?

It's what I referenced yesterday in standup, that TaggableManager fields are already automatically mapped to TagFilter so I'm hoping it should Just Work™. See:

TaggableManager: {"filter_class": TagFilter},

In other words, as long as "tags" is provided in Meta.fields for the filterset, I'm pretty certain that it won't need to be overloaded on the filterset class itself. Thanks!

@jathanism
Copy link
Contributor

jathanism commented Nov 16, 2022

In the 2.0 TODO review this came up. Possible related here?

nautobot/extras/tests/test_dynamicgroups.py

  • # 2.0 TODO(jathan): When "serial" becomes a multi-value filter, this will need to be revised or removed.

nautobot/dcim/filters.py

Copy link
Contributor

@jathanism jathanism left a comment

Choose a reason for hiding this comment

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

I have some minor asks for style, but this looks good-to-go! 🚢 🇮🇹

nautobot/extras/tests/test_dynamicgroups.py Show resolved Hide resolved
nautobot/utilities/filters.py Outdated Show resolved Hide resolved
…ittest using pass_throgh_ports filter that no longer exists
@HanlinMiao HanlinMiao merged commit 2e80839 into hanlin-#2804 Nov 18, 2022
@bryanculver bryanculver deleted the hanlin-#2804-eliminate-shadow-filters branch December 5, 2022 14:36
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.

Change Shadowed Fields on Existing DCIM FilterSets
3 participants