netbox-scope-switcher 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. netbox_scope_switcher-0.1.0/LICENSE +200 -0
  2. netbox_scope_switcher-0.1.0/PKG-INFO +221 -0
  3. netbox_scope_switcher-0.1.0/README.md +194 -0
  4. netbox_scope_switcher-0.1.0/netbox_scope_switcher/__init__.py +33 -0
  5. netbox_scope_switcher-0.1.0/netbox_scope_switcher/applicability.py +53 -0
  6. netbox_scope_switcher-0.1.0/netbox_scope_switcher/cascade.py +199 -0
  7. netbox_scope_switcher-0.1.0/netbox_scope_switcher/config.py +144 -0
  8. netbox_scope_switcher-0.1.0/netbox_scope_switcher/middleware.py +87 -0
  9. netbox_scope_switcher-0.1.0/netbox_scope_switcher/template_content.py +667 -0
  10. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/__init__.py +0 -0
  11. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/test_applicability.py +37 -0
  12. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/test_cascade.py +88 -0
  13. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/test_config.py +58 -0
  14. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/test_middleware.py +115 -0
  15. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/test_navbar.py +105 -0
  16. netbox_scope_switcher-0.1.0/netbox_scope_switcher/tests/test_views.py +106 -0
  17. netbox_scope_switcher-0.1.0/netbox_scope_switcher/urls.py +8 -0
  18. netbox_scope_switcher-0.1.0/netbox_scope_switcher/views.py +141 -0
  19. netbox_scope_switcher-0.1.0/netbox_scope_switcher.egg-info/PKG-INFO +221 -0
  20. netbox_scope_switcher-0.1.0/netbox_scope_switcher.egg-info/SOURCES.txt +22 -0
  21. netbox_scope_switcher-0.1.0/netbox_scope_switcher.egg-info/dependency_links.txt +1 -0
  22. netbox_scope_switcher-0.1.0/netbox_scope_switcher.egg-info/top_level.txt +1 -0
  23. netbox_scope_switcher-0.1.0/pyproject.toml +38 -0
  24. netbox_scope_switcher-0.1.0/setup.cfg +4 -0
@@ -0,0 +1,200 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative Works
95
+ a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works that
101
+ You distribute, all copyright, patent, trademark, and attribution
102
+ notices from the Source form of the Work, excluding those notices
103
+ that do not pertain to any part of the Derivative Works; and
104
+
105
+ (d) If the Work includes a "NOTICE" text file as part of its
106
+ distribution, then any Derivative Works that You distribute must
107
+ include a readable copy of the attribution notices contained
108
+ within such NOTICE file, excluding those notices that do not
109
+ pertain to any part of the Derivative Works, in at least one
110
+ of the following places: within a NOTICE text file distributed
111
+ as part of the Derivative Works; within the Source form or
112
+ documentation, if provided along with the Derivative Works; or,
113
+ within a display generated by the Derivative Works, if and
114
+ wherever such third-party notices normally appear. The contents
115
+ of the NOTICE file are for informational purposes only and do
116
+ not modify the License. You may add Your own attribution notices
117
+ within Derivative Works that You distribute, alongside or as an
118
+ addendum to the NOTICE text from the Work, provided that such
119
+ additional attribution notices cannot be construed as modifying
120
+ the License.
121
+
122
+ You may add Your own copyright statement to Your modifications and
123
+ may provide additional or different license terms and conditions
124
+ for use, reproduction, or distribution of Your modifications, or
125
+ for any such Derivative Works as a whole, provided Your use,
126
+ reproduction, and distribution of the Work otherwise complies with
127
+ the conditions stated in this License.
128
+
129
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
130
+ any Contribution intentionally submitted for inclusion in the Work
131
+ by You to the Licensor shall be under the terms and conditions of
132
+ this License, without any additional terms or conditions.
133
+ Notwithstanding the above, nothing herein shall supersede or modify
134
+ the terms of any separate license agreement you may have executed
135
+ with Licensor regarding such Contributions.
136
+
137
+ 6. Trademarks. This License does not grant permission to use the trade
138
+ names, trademarks, service marks, or product names of the Licensor,
139
+ except as required for reasonable and customary use in describing the
140
+ origin of the Work and reproducing the content of the NOTICE file.
141
+
142
+ 7. Disclaimer of Warranty. Unless required by applicable law or
143
+ agreed to in writing, Licensor provides the Work (and each
144
+ Contributor provides its Contributions) on an "AS IS" BASIS,
145
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
146
+ implied, including, without limitation, any warranties or conditions
147
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
148
+ PARTICULAR PURPOSE. You are solely responsible for determining the
149
+ appropriateness of using or redistributing the Work and assume any
150
+ risks associated with Your exercise of permissions under this License.
151
+
152
+ 8. Limitation of Liability. In no event and under no legal theory,
153
+ whether in tort (including negligence), contract, or otherwise,
154
+ unless required by applicable law (such as deliberate and grossly
155
+ negligent acts) or agreed to in writing, shall any Contributor be
156
+ liable to You for damages, including any direct, indirect, special,
157
+ incidental, or consequential damages of any character arising as a
158
+ result of this License or out of the use or inability to use the
159
+ Work (including but not limited to damages for loss of goodwill,
160
+ work stoppage, computer failure or malfunction, or any and all
161
+ other commercial damages or losses), even if such Contributor
162
+ has been advised of the possibility of such damages.
163
+
164
+ 9. Accepting Warranty or Additional Liability. While redistributing
165
+ the Work or Derivative Works thereof, You may choose to offer,
166
+ and charge a fee for, acceptance of support, warranty, indemnity,
167
+ or other liability obligations and/or rights consistent with this
168
+ License. However, in accepting such obligations, You may act only
169
+ on Your own behalf and on Your sole responsibility, not on behalf
170
+ of any other Contributor, and only if You agree to indemnify,
171
+ defend, and hold each Contributor harmless for any liability
172
+ incurred by, or claims asserted against, such Contributor by reason
173
+ of your accepting any such warranty or additional liability.
174
+
175
+ END OF TERMS AND CONDITIONS
176
+
177
+ APPENDIX: How to apply the Apache License to your work.
178
+
179
+ To apply the Apache License to your work, attach the following
180
+ boilerplate notice, with the fields enclosed by brackets "[]"
181
+ replaced with your own identifying information. (Don't include
182
+ the brackets!) The text should be enclosed in the appropriate
183
+ comment syntax for the file format. We also recommend that a
184
+ file or class name and description of purpose be included on the
185
+ same "printed page" as the copyright notice for easier
186
+ identification within third-party archives.
187
+
188
+ Copyright 2026 Du10777
189
+
190
+ Licensed under the Apache License, Version 2.0 (the "License");
191
+ you may not use this file except in compliance with the License.
192
+ You may obtain a copy of the License at
193
+
194
+ http://www.apache.org/licenses/LICENSE-2.0
195
+
196
+ Unless required by applicable law or agreed to in writing, software
197
+ distributed under the License is distributed on an "AS IS" BASIS,
198
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
199
+ See the License for the specific language governing permissions and
200
+ limitations under the License.
@@ -0,0 +1,221 @@
1
+ Metadata-Version: 2.4
2
+ Name: netbox-scope-switcher
3
+ Version: 0.1.0
4
+ Summary: Configurable multi-dimension scope selector in the NetBox navbar that scopes list views by tenant, site, region, tag, and more.
5
+ Author-email: Du10777 <du103682@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/Du10777/netbox-scope-switcher
8
+ Project-URL: Repository, https://github.com/Du10777/netbox-scope-switcher
9
+ Project-URL: Documentation, https://github.com/Du10777/netbox-scope-switcher#readme
10
+ Project-URL: Issues, https://github.com/Du10777/netbox-scope-switcher/issues
11
+ Project-URL: Changelog, https://github.com/Du10777/netbox-scope-switcher/blob/main/CHANGELOG.md
12
+ Keywords: netbox,netbox-plugin,plugin,scope,tenant,site,region,tag,multitenancy,filter,dcim,ipam
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Environment :: Web Environment
15
+ Classifier: Framework :: Django
16
+ Classifier: Intended Audience :: System Administrators
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Topic :: System :: Networking
23
+ Requires-Python: >=3.10
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Dynamic: license-file
27
+
28
+ # NetBox Scope Switcher
29
+
30
+ **Pick your scope once — tenant, region, site, tag, whatever you configure — and
31
+ every list in NetBox narrows to it, with the filters cascading on each other.**
32
+
33
+ `netbox-scope-switcher` adds one compact, configurable selector to the top
34
+ navigation bar. Choose values across any number of *scope dimensions* (grouped
35
+ however you like) and, as you move between sections (Devices, Prefixes, VLANs,
36
+ Sites, …), each list view is automatically scoped to your selection.
37
+
38
+ It generalizes
39
+ [`netbox-tenant-switcher`](https://github.com/Du10777/netbox-tenant-switcher):
40
+ tenant is just the default dimension.
41
+
42
+ ## Screenshots
43
+
44
+ ![Grouped scope panel with a tenant selected; the Site list has cascaded to that tenant](docs/img/cascade.png)
45
+
46
+ <p>
47
+ <img src="docs/img/panel.png" width="340" alt="Grouped dimensions: Tenant / Location / Tag">
48
+ <img src="docs/img/i18n.png" width="340" alt="Labels follow the NetBox UI language">
49
+ </p>
50
+
51
+ ## Features
52
+
53
+ - **Configurable, grouped dimensions.** Scope by tenant, tenant group, region,
54
+ site group, site, location, tag, or any filter you declare in `PLUGINS_CONFIG`
55
+ — arranged under your own group headings. No code changes.
56
+ - **Cascading dependent filters.** Selecting values in one dimension narrows the
57
+ option lists of the others, in both directions: pick a *Tenant Group* and only
58
+ its tenants remain; pick a *Tenant* and Regions/Sites/Racks shrink to where that
59
+ tenant is present. Powered by NetBox's own filtersets (so tree descendants and
60
+ every relationship are honoured), with no hard-coded model knowledge.
61
+ - **Token-style control.** One dropdown with a section per dimension: selected
62
+ values shown as removable chips, a `+ add` picker with instant search, an `Any`
63
+ placeholder, a `None` option (for objects with no value), per-dimension and
64
+ global *clear*. Chips stay name-sorted. No external JS dependencies.
65
+ - **Localized.** Dimension labels come straight from each model's NetBox
66
+ `verbose_name`, and the service words are translated via NetBox's catalog, so
67
+ the panel follows the active UI language automatically.
68
+ - **Resizable & viewport-bounded panel.** Drag the corner grip to resize (the
69
+ size is remembered for the session); the panel never grows past the screen and
70
+ scrolls internally.
71
+ - **Honest URLs.** By default the plugin issues a single redirect that adds the
72
+ filter params to the address bar, so links are shareable, NetBox's own filter
73
+ panel shows them, and *Clear filters* behaves predictably. A `silent` mode is
74
+ available.
75
+ - **Per-scope applicability.** Each dimension is applied only on list views whose
76
+ filterset actually supports it — a prefixes list may scope by tenant and tag but
77
+ not region — computed by introspection, not hard-coding. The control dims when
78
+ nothing applies to the current page.
79
+ - **Combining semantics:** several values within a dimension are OR-combined;
80
+ different dimensions are AND-combined; `None` maps to `?param=null`.
81
+ - **Permission-aware.** Only values the user may view are listed and selectable,
82
+ and scoping only ever *narrows* results — NetBox object permissions still apply
83
+ on top.
84
+ - **No models, no migrations.** Selection lives in the session.
85
+
86
+ ## Compatibility
87
+
88
+ | Plugin version | NetBox version | Python |
89
+ | -------------- | -------------- | ----------- |
90
+ | 0.1.x | 4.6 | 3.10 – 3.12 |
91
+
92
+ Developed and verified against **NetBox 4.6**, using only stable plugin APIs
93
+ (`PluginTemplateExtension.navbar()` / `head()`, plugin middleware). The CI matrix
94
+ (`.github/workflows/ci.yml`) additionally exercises 4.4 and 4.5.
95
+
96
+ ## Installation
97
+
98
+ ```bash
99
+ source /opt/netbox/venv/bin/activate
100
+ pip install netbox-scope-switcher
101
+ ```
102
+
103
+ Enable it in `configuration.py`:
104
+
105
+ ```python
106
+ PLUGINS = ["netbox_scope_switcher"]
107
+ ```
108
+
109
+ Restart NetBox (`sudo systemctl restart netbox netbox-rq`). No migrations needed.
110
+
111
+ ### Docker (netbox-docker)
112
+
113
+ See [`deploy/`](deploy/) for a ready-to-use `Dockerfile-Plugins`,
114
+ `docker-compose.override.yml`, and `plugins.py`.
115
+
116
+ ## Configuration
117
+
118
+ Out of the box the plugin scopes by **tenant only** (matching
119
+ `netbox-tenant-switcher`). To configure dimensions and grouping, set
120
+ `PLUGINS_CONFIG`:
121
+
122
+ ```python
123
+ PLUGINS_CONFIG = {
124
+ "netbox_scope_switcher": {
125
+ "mode": "redirect", # or "silent"
126
+ "scopes": [
127
+ {"param": "tenant_group_id", "model": "tenancy.TenantGroup", "group": "Tenant"},
128
+ {"param": "tenant_id", "model": "tenancy.Tenant", "group": "Tenant"},
129
+ {"param": "region_id", "model": "dcim.Region", "group": "Location"},
130
+ {"param": "site_group_id", "model": "dcim.SiteGroup", "group": "Location"},
131
+ {"param": "site_id", "model": "dcim.Site", "group": "Location"},
132
+ {"param": "location_id", "model": "dcim.Location", "group": "Location"},
133
+ {"param": "tag", "model": "extras.Tag", "group": "Tag",
134
+ "value_field": "slug"},
135
+ ],
136
+ }
137
+ }
138
+ ```
139
+
140
+ ### `PLUGINS_CONFIG` keys
141
+
142
+ | Key | Type | Default | Description |
143
+ | -------- | ------ | ----------- | ----------- |
144
+ | `scopes` | list | tenant only | Ordered list of scope dimensions (below). |
145
+ | `mode` | string | `redirect` | `redirect` (add params to the URL via a 302) or `silent` (rewrite the request in place). |
146
+
147
+ Each entry in `scopes`:
148
+
149
+ | Field | Required | Default | Description |
150
+ | ------------- | -------- | ------------------ | ----------- |
151
+ | `param` | yes | — | GET parameter of the filter to apply (e.g. `tenant_id`, `tag`). |
152
+ | `model` | yes | — | `app_label.ModelName` the dropdown values come from. |
153
+ | `group` | no | — | Group heading to list this dimension under (translated via NetBox). |
154
+ | `label` | no | model verbose name | Section heading. Omit it to follow the active UI language. |
155
+ | `value_field` | no | `pk` | Value to put in the param: `pk` or `slug` (tags filter by slug). |
156
+
157
+ The configuration is validated at startup: an unknown model or field raises a
158
+ clear `ImproperlyConfigured` error on load, rather than a 500 at runtime.
159
+
160
+ ## How it works
161
+
162
+ - **Navbar widget** (`template_content.py`) — a `PluginTemplateExtension` renders
163
+ the control + a section per dimension via `navbar()`; CSS/JS are injected once
164
+ via `head()`. Options are fetched from the values endpoint on open (so they can
165
+ cascade); only the static `None` option is server-rendered.
166
+ - **Set endpoint** (`views.py`) — `GET /plugins/scope-switcher/set/` stores the
167
+ chosen values per dimension in the session and redirects back. `next` is
168
+ validated against the current host (no open redirects); every value is checked
169
+ against the user's view permission.
170
+ - **Values endpoint** (`views.py`) —
171
+ `GET /plugins/scope-switcher/values/<param>/?q=…&<other>=…` returns a
172
+ permission-scoped, cascaded, searchable JSON option list.
173
+ - **Cascade** (`cascade.py`) — narrows a dimension's options to those consistent
174
+ with the current selection in the others. A *forward* pass filters a model by
175
+ every other scope its own filterset supports (Site by Region, Rack by Location,
176
+ …); a *reverse* pass handles the "upward" direction by pivoting through a bridge
177
+ model that has a FK to the target and whose filterset does support the source
178
+ (Site bridges Region↔Tenant, Tenant bridges Tenant Group↔Tenant, …).
179
+ - **Applicability** (`applicability.py`) — introspects the current list view's
180
+ filterset (`get_filters()` / `base_filters`) to decide which configured params
181
+ it supports; used to dim the control and to choose what to apply.
182
+ - **Middleware** (`middleware.py`) — for `ObjectListView` pages, appends one
183
+ `?param=<value>` per selected value for each supported, not-already-present
184
+ dimension. `redirect` mode issues a single 302; `silent` mode rewrites
185
+ `request.GET`. API, GraphQL, and HTMX requests are skipped; existing query
186
+ params (pagination, sort, per_page) are preserved.
187
+
188
+ ## Differences from netbox-tenant-switcher
189
+
190
+ | | `netbox-tenant-switcher` | `netbox-scope-switcher` |
191
+ | --- | --- | --- |
192
+ | Dimensions | tenant only (hard-coded) | any, configurable and grouped |
193
+ | Cascade | — | filters narrow each other, both directions |
194
+ | Control | one tenant field | one control, a section + chips per dimension |
195
+ | Localization | — | labels/service words follow the UI language |
196
+ | URL handling | redirect | redirect (default) or silent |
197
+ | Session state | list of tenant ids | `{param: [values]}` dict |
198
+
199
+ ### Migrating
200
+
201
+ 1. `pip uninstall netbox-tenant-switcher` and remove it from `PLUGINS`.
202
+ 2. `pip install netbox-scope-switcher`, add `"netbox_scope_switcher"` to `PLUGINS`.
203
+ 3. Do nothing else for identical behaviour — the default config is the tenant
204
+ scope. Add more dimensions to `PLUGINS_CONFIG` when you want them.
205
+
206
+ Sessions do not carry over (different key/shape); users simply re-pick their
207
+ scope once.
208
+
209
+ ## Testing
210
+
211
+ Tests live in `netbox_scope_switcher/tests/` and run inside a NetBox checkout:
212
+
213
+ ```bash
214
+ python netbox/netbox/manage.py test netbox_scope_switcher -v 2
215
+ ```
216
+
217
+ See `.github/workflows/ci.yml` for the full CI setup.
218
+
219
+ ## License
220
+
221
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,194 @@
1
+ # NetBox Scope Switcher
2
+
3
+ **Pick your scope once — tenant, region, site, tag, whatever you configure — and
4
+ every list in NetBox narrows to it, with the filters cascading on each other.**
5
+
6
+ `netbox-scope-switcher` adds one compact, configurable selector to the top
7
+ navigation bar. Choose values across any number of *scope dimensions* (grouped
8
+ however you like) and, as you move between sections (Devices, Prefixes, VLANs,
9
+ Sites, …), each list view is automatically scoped to your selection.
10
+
11
+ It generalizes
12
+ [`netbox-tenant-switcher`](https://github.com/Du10777/netbox-tenant-switcher):
13
+ tenant is just the default dimension.
14
+
15
+ ## Screenshots
16
+
17
+ ![Grouped scope panel with a tenant selected; the Site list has cascaded to that tenant](docs/img/cascade.png)
18
+
19
+ <p>
20
+ <img src="docs/img/panel.png" width="340" alt="Grouped dimensions: Tenant / Location / Tag">
21
+ <img src="docs/img/i18n.png" width="340" alt="Labels follow the NetBox UI language">
22
+ </p>
23
+
24
+ ## Features
25
+
26
+ - **Configurable, grouped dimensions.** Scope by tenant, tenant group, region,
27
+ site group, site, location, tag, or any filter you declare in `PLUGINS_CONFIG`
28
+ — arranged under your own group headings. No code changes.
29
+ - **Cascading dependent filters.** Selecting values in one dimension narrows the
30
+ option lists of the others, in both directions: pick a *Tenant Group* and only
31
+ its tenants remain; pick a *Tenant* and Regions/Sites/Racks shrink to where that
32
+ tenant is present. Powered by NetBox's own filtersets (so tree descendants and
33
+ every relationship are honoured), with no hard-coded model knowledge.
34
+ - **Token-style control.** One dropdown with a section per dimension: selected
35
+ values shown as removable chips, a `+ add` picker with instant search, an `Any`
36
+ placeholder, a `None` option (for objects with no value), per-dimension and
37
+ global *clear*. Chips stay name-sorted. No external JS dependencies.
38
+ - **Localized.** Dimension labels come straight from each model's NetBox
39
+ `verbose_name`, and the service words are translated via NetBox's catalog, so
40
+ the panel follows the active UI language automatically.
41
+ - **Resizable & viewport-bounded panel.** Drag the corner grip to resize (the
42
+ size is remembered for the session); the panel never grows past the screen and
43
+ scrolls internally.
44
+ - **Honest URLs.** By default the plugin issues a single redirect that adds the
45
+ filter params to the address bar, so links are shareable, NetBox's own filter
46
+ panel shows them, and *Clear filters* behaves predictably. A `silent` mode is
47
+ available.
48
+ - **Per-scope applicability.** Each dimension is applied only on list views whose
49
+ filterset actually supports it — a prefixes list may scope by tenant and tag but
50
+ not region — computed by introspection, not hard-coding. The control dims when
51
+ nothing applies to the current page.
52
+ - **Combining semantics:** several values within a dimension are OR-combined;
53
+ different dimensions are AND-combined; `None` maps to `?param=null`.
54
+ - **Permission-aware.** Only values the user may view are listed and selectable,
55
+ and scoping only ever *narrows* results — NetBox object permissions still apply
56
+ on top.
57
+ - **No models, no migrations.** Selection lives in the session.
58
+
59
+ ## Compatibility
60
+
61
+ | Plugin version | NetBox version | Python |
62
+ | -------------- | -------------- | ----------- |
63
+ | 0.1.x | 4.6 | 3.10 – 3.12 |
64
+
65
+ Developed and verified against **NetBox 4.6**, using only stable plugin APIs
66
+ (`PluginTemplateExtension.navbar()` / `head()`, plugin middleware). The CI matrix
67
+ (`.github/workflows/ci.yml`) additionally exercises 4.4 and 4.5.
68
+
69
+ ## Installation
70
+
71
+ ```bash
72
+ source /opt/netbox/venv/bin/activate
73
+ pip install netbox-scope-switcher
74
+ ```
75
+
76
+ Enable it in `configuration.py`:
77
+
78
+ ```python
79
+ PLUGINS = ["netbox_scope_switcher"]
80
+ ```
81
+
82
+ Restart NetBox (`sudo systemctl restart netbox netbox-rq`). No migrations needed.
83
+
84
+ ### Docker (netbox-docker)
85
+
86
+ See [`deploy/`](deploy/) for a ready-to-use `Dockerfile-Plugins`,
87
+ `docker-compose.override.yml`, and `plugins.py`.
88
+
89
+ ## Configuration
90
+
91
+ Out of the box the plugin scopes by **tenant only** (matching
92
+ `netbox-tenant-switcher`). To configure dimensions and grouping, set
93
+ `PLUGINS_CONFIG`:
94
+
95
+ ```python
96
+ PLUGINS_CONFIG = {
97
+ "netbox_scope_switcher": {
98
+ "mode": "redirect", # or "silent"
99
+ "scopes": [
100
+ {"param": "tenant_group_id", "model": "tenancy.TenantGroup", "group": "Tenant"},
101
+ {"param": "tenant_id", "model": "tenancy.Tenant", "group": "Tenant"},
102
+ {"param": "region_id", "model": "dcim.Region", "group": "Location"},
103
+ {"param": "site_group_id", "model": "dcim.SiteGroup", "group": "Location"},
104
+ {"param": "site_id", "model": "dcim.Site", "group": "Location"},
105
+ {"param": "location_id", "model": "dcim.Location", "group": "Location"},
106
+ {"param": "tag", "model": "extras.Tag", "group": "Tag",
107
+ "value_field": "slug"},
108
+ ],
109
+ }
110
+ }
111
+ ```
112
+
113
+ ### `PLUGINS_CONFIG` keys
114
+
115
+ | Key | Type | Default | Description |
116
+ | -------- | ------ | ----------- | ----------- |
117
+ | `scopes` | list | tenant only | Ordered list of scope dimensions (below). |
118
+ | `mode` | string | `redirect` | `redirect` (add params to the URL via a 302) or `silent` (rewrite the request in place). |
119
+
120
+ Each entry in `scopes`:
121
+
122
+ | Field | Required | Default | Description |
123
+ | ------------- | -------- | ------------------ | ----------- |
124
+ | `param` | yes | — | GET parameter of the filter to apply (e.g. `tenant_id`, `tag`). |
125
+ | `model` | yes | — | `app_label.ModelName` the dropdown values come from. |
126
+ | `group` | no | — | Group heading to list this dimension under (translated via NetBox). |
127
+ | `label` | no | model verbose name | Section heading. Omit it to follow the active UI language. |
128
+ | `value_field` | no | `pk` | Value to put in the param: `pk` or `slug` (tags filter by slug). |
129
+
130
+ The configuration is validated at startup: an unknown model or field raises a
131
+ clear `ImproperlyConfigured` error on load, rather than a 500 at runtime.
132
+
133
+ ## How it works
134
+
135
+ - **Navbar widget** (`template_content.py`) — a `PluginTemplateExtension` renders
136
+ the control + a section per dimension via `navbar()`; CSS/JS are injected once
137
+ via `head()`. Options are fetched from the values endpoint on open (so they can
138
+ cascade); only the static `None` option is server-rendered.
139
+ - **Set endpoint** (`views.py`) — `GET /plugins/scope-switcher/set/` stores the
140
+ chosen values per dimension in the session and redirects back. `next` is
141
+ validated against the current host (no open redirects); every value is checked
142
+ against the user's view permission.
143
+ - **Values endpoint** (`views.py`) —
144
+ `GET /plugins/scope-switcher/values/<param>/?q=…&<other>=…` returns a
145
+ permission-scoped, cascaded, searchable JSON option list.
146
+ - **Cascade** (`cascade.py`) — narrows a dimension's options to those consistent
147
+ with the current selection in the others. A *forward* pass filters a model by
148
+ every other scope its own filterset supports (Site by Region, Rack by Location,
149
+ …); a *reverse* pass handles the "upward" direction by pivoting through a bridge
150
+ model that has a FK to the target and whose filterset does support the source
151
+ (Site bridges Region↔Tenant, Tenant bridges Tenant Group↔Tenant, …).
152
+ - **Applicability** (`applicability.py`) — introspects the current list view's
153
+ filterset (`get_filters()` / `base_filters`) to decide which configured params
154
+ it supports; used to dim the control and to choose what to apply.
155
+ - **Middleware** (`middleware.py`) — for `ObjectListView` pages, appends one
156
+ `?param=<value>` per selected value for each supported, not-already-present
157
+ dimension. `redirect` mode issues a single 302; `silent` mode rewrites
158
+ `request.GET`. API, GraphQL, and HTMX requests are skipped; existing query
159
+ params (pagination, sort, per_page) are preserved.
160
+
161
+ ## Differences from netbox-tenant-switcher
162
+
163
+ | | `netbox-tenant-switcher` | `netbox-scope-switcher` |
164
+ | --- | --- | --- |
165
+ | Dimensions | tenant only (hard-coded) | any, configurable and grouped |
166
+ | Cascade | — | filters narrow each other, both directions |
167
+ | Control | one tenant field | one control, a section + chips per dimension |
168
+ | Localization | — | labels/service words follow the UI language |
169
+ | URL handling | redirect | redirect (default) or silent |
170
+ | Session state | list of tenant ids | `{param: [values]}` dict |
171
+
172
+ ### Migrating
173
+
174
+ 1. `pip uninstall netbox-tenant-switcher` and remove it from `PLUGINS`.
175
+ 2. `pip install netbox-scope-switcher`, add `"netbox_scope_switcher"` to `PLUGINS`.
176
+ 3. Do nothing else for identical behaviour — the default config is the tenant
177
+ scope. Add more dimensions to `PLUGINS_CONFIG` when you want them.
178
+
179
+ Sessions do not carry over (different key/shape); users simply re-pick their
180
+ scope once.
181
+
182
+ ## Testing
183
+
184
+ Tests live in `netbox_scope_switcher/tests/` and run inside a NetBox checkout:
185
+
186
+ ```bash
187
+ python netbox/netbox/manage.py test netbox_scope_switcher -v 2
188
+ ```
189
+
190
+ See `.github/workflows/ci.yml` for the full CI setup.
191
+
192
+ ## License
193
+
194
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,33 @@
1
+ from netbox.plugins import PluginConfig
2
+
3
+
4
+ class ScopeSwitcherConfig(PluginConfig):
5
+ name = 'netbox_scope_switcher'
6
+ verbose_name = 'Scope Switcher'
7
+ description = ('Adds a configurable, multi-dimension scope selector to the top '
8
+ 'navbar. Selecting values scopes list views by tenant, site, '
9
+ 'region, tag, and any other filter you configure.')
10
+ version = '0.1.0'
11
+ author = 'Du10777'
12
+ base_url = 'scope-switcher'
13
+ min_version = '4.4.0'
14
+ # Request-scoped filtering of list views by the selected scope values.
15
+ middleware = ['netbox_scope_switcher.middleware.ScopeSwitcherMiddleware']
16
+ # Defaults to a single tenant scope so the out-of-the-box behaviour matches
17
+ # netbox-tenant-switcher; override `scopes`/`mode` in PLUGINS_CONFIG.
18
+ default_settings = {
19
+ 'scopes': [
20
+ {'param': 'tenant_id', 'model': 'tenancy.Tenant'},
21
+ ],
22
+ 'mode': 'redirect',
23
+ }
24
+
25
+ def ready(self):
26
+ super().ready()
27
+ # Fail fast at startup with a clear message on a bad configuration,
28
+ # rather than raising a 500 somewhere in the request path.
29
+ from .config import validate_config
30
+ validate_config()
31
+
32
+
33
+ config = ScopeSwitcherConfig