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

Fixes 8553: Fix contacts and ASNs missing in the search dropdown and … #8573

Merged
merged 1 commit into from
Mar 17, 2022

Conversation

991jo
Copy link
Contributor

@991jo 991jo commented Feb 7, 2022

…contacts not showing the number of assigned objects

Fixes: #8553

The contact and ASN object were not shown in the global object search dropdown.
Also the contant queryset used in the global search was not annotated with the assigned_count

docs/release-notes/version-3.1.md Outdated Show resolved Hide resolved
netbox/netbox/constants.py Outdated Show resolved Hide resolved
netbox/netbox/forms.py Outdated Show resolved Hide resolved
'table': table,
'url': f"{reverse(url)}?q={form.cleaned_data.get('q')}"
})
for _, value in SEARCH_TYPE_HIERARCHY.items():
Copy link
Member

Choose a reason for hiding this comment

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

We want to avoid looping with for to find a key. We should be looking up a key directly in a dict.

Rather than breaking SEARCH_TYPES into a separate dict per app, I would keep it as-is (actually, I'd change it to a regular dict), and create a separate tuple of nested two-tuples to be used for the search type choices (as with any ChoiceField). This way we can use SEARCH_TYPES to directly reference the bits we need to perform a search, and use the nested choices just to render the form fields.

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 understand that the for loop is not a very nice solution.

Regarding having the hierarchy separate:
I thought the fundamental problem of this bug is that 2 partially redundant data structures have to be maintained by hand.
Separating the hierarchy from the dict containing the search types still results in 2 different places holding the information. Or did I misunderstand you?

I could keep the SEARCH_TYPE_HIERARCHY as is, but add code to build a flattened version for building the view. This should solve the loop problem while not having to maintain partially redundant data structures by hand.

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 prepared it as described above, the SEARCH_TYPE_HIERARCHY still exists, and then OBJECT_CHOICES and SEARCH_TYPES are generated from that at program start.
The hierarchy is split into per-app dicts to keep in readable. That many layers of brackets and indentation would get to messy. Also the OrderedDicts are required because we might want to preserve the order in OBJECT_CHOICES

The result is that I don't even have to touch the code in the view and therefore not introduce a for-loop there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jeremystretch This PR is waiting for a response from you.

netbox/netbox/views/__init__.py Outdated Show resolved Hide resolved
Location.objects.all(),
Device,

CIRCUIT_TYPES = OrderedDict(
Copy link
Member

Choose a reason for hiding this comment

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

FYI there's no need to use OrderedDict in this case on Python 3.7+ since dictionary ordering is now guaranteed out of the box. It's fine as-is though, I'll clean it up along with some other stuff in a project-wide sweep at some point.

@jeremystretch jeremystretch merged commit db52fe4 into netbox-community:develop Mar 17, 2022
@jeremystretch
Copy link
Member

Thanks for this!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provider network, ASN, and contact options missins from global search selector
2 participants