netbox-sqids 0.1.1__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.
- netbox_sqids-0.1.1/LICENSE +200 -0
- netbox_sqids-0.1.1/PKG-INFO +115 -0
- netbox_sqids-0.1.1/README.md +84 -0
- netbox_sqids-0.1.1/netbox_sqids/__init__.py +64 -0
- netbox_sqids-0.1.1/netbox_sqids/api/__init__.py +0 -0
- netbox_sqids-0.1.1/netbox_sqids/api/urls.py +7 -0
- netbox_sqids-0.1.1/netbox_sqids/api/views.py +27 -0
- netbox_sqids-0.1.1/netbox_sqids/sqids.py +83 -0
- netbox_sqids-0.1.1/netbox_sqids/urls.py +7 -0
- netbox_sqids-0.1.1/netbox_sqids/views.py +22 -0
- netbox_sqids-0.1.1/netbox_sqids.egg-info/PKG-INFO +115 -0
- netbox_sqids-0.1.1/netbox_sqids.egg-info/SOURCES.txt +17 -0
- netbox_sqids-0.1.1/netbox_sqids.egg-info/dependency_links.txt +1 -0
- netbox_sqids-0.1.1/netbox_sqids.egg-info/requires.txt +12 -0
- netbox_sqids-0.1.1/netbox_sqids.egg-info/top_level.txt +1 -0
- netbox_sqids-0.1.1/pyproject.toml +97 -0
- netbox_sqids-0.1.1/setup.cfg +4 -0
- netbox_sqids-0.1.1/tests/test_sqids.py +132 -0
- netbox_sqids-0.1.1/tests/test_views.py +113 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. Please also get an OpenPGP
|
|
186
|
+
key ID from a public key server, and include that with the notice.
|
|
187
|
+
|
|
188
|
+
Copyright 2026 Jonathan Senecal
|
|
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,115 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: netbox-sqids
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: NetBox plugin that adds short, URL-safe, globally unique SQID identifiers to every model
|
|
5
|
+
Author-email: Jonathan Senecal <contact@jonathansenecal.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/jsenecal/netbox-sqids
|
|
7
|
+
Project-URL: Source, https://github.com/jsenecal/netbox-sqids
|
|
8
|
+
Project-URL: Tracker, https://github.com/jsenecal/netbox-sqids/issues
|
|
9
|
+
Project-URL: Documentation, https://jsenecal.github.io/netbox-sqids/
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: sqids>=0.4.1
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: bumpver; extra == "dev"
|
|
23
|
+
Requires-Dist: pre-commit>=4.0.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-django>=4.5.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest-cov>=3.0.0; extra == "dev"
|
|
27
|
+
Requires-Dist: ruff; extra == "dev"
|
|
28
|
+
Provides-Extra: docs
|
|
29
|
+
Requires-Dist: zensical; extra == "docs"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# NetBox SQIDs
|
|
33
|
+
|
|
34
|
+
[](https://github.com/jsenecal/netbox-sqids/actions/workflows/ci.yml)
|
|
35
|
+
|
|
36
|
+
> **Alpha** — this plugin is functional but under active development. The API and SQID encoding may change before 1.0.
|
|
37
|
+
|
|
38
|
+
Short, URL-safe, globally unique identifiers for every NetBox object.
|
|
39
|
+
|
|
40
|
+
NetBox SQIDs adds a computed `sqid` property to every model in your NetBox instance. SQIDs encode the content type and primary key into a compact string that can be shared, bookmarked, and resolved back to the original object — no database changes required.
|
|
41
|
+
|
|
42
|
+
## Quick Example
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
>>> device = Device.objects.first()
|
|
46
|
+
>>> device.sqid
|
|
47
|
+
'WK1J'
|
|
48
|
+
|
|
49
|
+
>>> from netbox_sqids.sqids import resolve_sqid
|
|
50
|
+
>>> resolve_sqid('WK1J')
|
|
51
|
+
<Device: my-switch>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Visit `/s/WK1J/` in your browser to be redirected to the device's detail page.
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
- **Zero-config** — install and every model gets a `.sqid` property
|
|
59
|
+
- **No migrations** — pure computed properties, no database changes
|
|
60
|
+
- **Globally unique** — SQIDs are unique across all models
|
|
61
|
+
- **URL-safe** — curated 33-character alphabet, no ambiguous characters
|
|
62
|
+
- **Redirect views** — resolve any SQID via browser or API
|
|
63
|
+
- **Short URLs** — optional root-level routes like `/s/WK1J/`
|
|
64
|
+
|
|
65
|
+
## Requirements
|
|
66
|
+
|
|
67
|
+
| Dependency | Version |
|
|
68
|
+
|------------|----------|
|
|
69
|
+
| NetBox | >= 4.5.0 |
|
|
70
|
+
| Python | >= 3.12 |
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install netbox-sqids
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Add to your NetBox `configuration.py`:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
PLUGINS = ["netbox_sqids"]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Restart NetBox. That's it.
|
|
85
|
+
|
|
86
|
+
## Configuration
|
|
87
|
+
|
|
88
|
+
All settings are optional:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
PLUGINS_CONFIG = {
|
|
92
|
+
"netbox_sqids": {
|
|
93
|
+
"min_length": 4, # Minimum SQID length (default: 4)
|
|
94
|
+
"blocklist": None, # Word blocklist (None = extended default)
|
|
95
|
+
"monkeypatched_url_prefix": "s", # Short URL prefix (None to disable)
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## URL Routes
|
|
101
|
+
|
|
102
|
+
| Route | Description |
|
|
103
|
+
|-------|-------------|
|
|
104
|
+
| `/plugins/sqids/<sqid>/` | Browser redirect to object |
|
|
105
|
+
| `/api/plugins/sqids/<sqid>/` | API redirect to object |
|
|
106
|
+
| `/s/<sqid>/` | Short browser redirect (configurable) |
|
|
107
|
+
| `/api/s/<sqid>/` | Short API redirect (configurable) |
|
|
108
|
+
|
|
109
|
+
## Documentation
|
|
110
|
+
|
|
111
|
+
Full documentation: [jsenecal.github.io/netbox-sqids](https://jsenecal.github.io/netbox-sqids/)
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# NetBox SQIDs
|
|
2
|
+
|
|
3
|
+
[](https://github.com/jsenecal/netbox-sqids/actions/workflows/ci.yml)
|
|
4
|
+
|
|
5
|
+
> **Alpha** — this plugin is functional but under active development. The API and SQID encoding may change before 1.0.
|
|
6
|
+
|
|
7
|
+
Short, URL-safe, globally unique identifiers for every NetBox object.
|
|
8
|
+
|
|
9
|
+
NetBox SQIDs adds a computed `sqid` property to every model in your NetBox instance. SQIDs encode the content type and primary key into a compact string that can be shared, bookmarked, and resolved back to the original object — no database changes required.
|
|
10
|
+
|
|
11
|
+
## Quick Example
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
>>> device = Device.objects.first()
|
|
15
|
+
>>> device.sqid
|
|
16
|
+
'WK1J'
|
|
17
|
+
|
|
18
|
+
>>> from netbox_sqids.sqids import resolve_sqid
|
|
19
|
+
>>> resolve_sqid('WK1J')
|
|
20
|
+
<Device: my-switch>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Visit `/s/WK1J/` in your browser to be redirected to the device's detail page.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- **Zero-config** — install and every model gets a `.sqid` property
|
|
28
|
+
- **No migrations** — pure computed properties, no database changes
|
|
29
|
+
- **Globally unique** — SQIDs are unique across all models
|
|
30
|
+
- **URL-safe** — curated 33-character alphabet, no ambiguous characters
|
|
31
|
+
- **Redirect views** — resolve any SQID via browser or API
|
|
32
|
+
- **Short URLs** — optional root-level routes like `/s/WK1J/`
|
|
33
|
+
|
|
34
|
+
## Requirements
|
|
35
|
+
|
|
36
|
+
| Dependency | Version |
|
|
37
|
+
|------------|----------|
|
|
38
|
+
| NetBox | >= 4.5.0 |
|
|
39
|
+
| Python | >= 3.12 |
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install netbox-sqids
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Add to your NetBox `configuration.py`:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
PLUGINS = ["netbox_sqids"]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Restart NetBox. That's it.
|
|
54
|
+
|
|
55
|
+
## Configuration
|
|
56
|
+
|
|
57
|
+
All settings are optional:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
PLUGINS_CONFIG = {
|
|
61
|
+
"netbox_sqids": {
|
|
62
|
+
"min_length": 4, # Minimum SQID length (default: 4)
|
|
63
|
+
"blocklist": None, # Word blocklist (None = extended default)
|
|
64
|
+
"monkeypatched_url_prefix": "s", # Short URL prefix (None to disable)
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## URL Routes
|
|
70
|
+
|
|
71
|
+
| Route | Description |
|
|
72
|
+
|-------|-------------|
|
|
73
|
+
| `/plugins/sqids/<sqid>/` | Browser redirect to object |
|
|
74
|
+
| `/api/plugins/sqids/<sqid>/` | API redirect to object |
|
|
75
|
+
| `/s/<sqid>/` | Short browser redirect (configurable) |
|
|
76
|
+
| `/api/s/<sqid>/` | Short API redirect (configurable) |
|
|
77
|
+
|
|
78
|
+
## Documentation
|
|
79
|
+
|
|
80
|
+
Full documentation: [jsenecal.github.io/netbox-sqids](https://jsenecal.github.io/netbox-sqids/)
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from netbox.plugins import PluginConfig
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.1"
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class NetBoxSqidsConfig(PluginConfig):
|
|
11
|
+
name = "netbox_sqids"
|
|
12
|
+
verbose_name = "NetBox SQIDs"
|
|
13
|
+
description = "Short, URL-safe, globally unique identifiers for every NetBox object"
|
|
14
|
+
version = __version__
|
|
15
|
+
author = "Jonathan Senecal"
|
|
16
|
+
author_email = "contact@jonathansenecal.com"
|
|
17
|
+
base_url = "sqids"
|
|
18
|
+
default_settings = {
|
|
19
|
+
"min_length": 4,
|
|
20
|
+
"blocklist": None,
|
|
21
|
+
"monkeypatched_url_prefix": "s",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
def ready(self):
|
|
25
|
+
super().ready()
|
|
26
|
+
self._patch_models()
|
|
27
|
+
self._patch_urls()
|
|
28
|
+
|
|
29
|
+
def _patch_models(self):
|
|
30
|
+
from django.apps import apps
|
|
31
|
+
|
|
32
|
+
from netbox_sqids.sqids import SqidDescriptor
|
|
33
|
+
|
|
34
|
+
for model in apps.get_models():
|
|
35
|
+
model.add_to_class("sqid", SqidDescriptor())
|
|
36
|
+
|
|
37
|
+
def _patch_urls(self):
|
|
38
|
+
from django.conf import settings
|
|
39
|
+
|
|
40
|
+
plugin_settings = settings.PLUGINS_CONFIG.get("netbox_sqids", {})
|
|
41
|
+
prefix = plugin_settings.get("monkeypatched_url_prefix", self.default_settings["monkeypatched_url_prefix"])
|
|
42
|
+
if prefix is None:
|
|
43
|
+
return
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
import netbox.urls
|
|
47
|
+
from django.urls import path
|
|
48
|
+
|
|
49
|
+
from netbox_sqids.api.views import SqidApiRedirectView
|
|
50
|
+
from netbox_sqids.views import SqidRedirectView
|
|
51
|
+
|
|
52
|
+
netbox.urls.urlpatterns.append(
|
|
53
|
+
path(f"{prefix}/<str:sqid>/", SqidRedirectView.as_view(), name="sqid_short_redirect")
|
|
54
|
+
)
|
|
55
|
+
netbox.urls.urlpatterns.append(
|
|
56
|
+
path(f"api/{prefix}/<str:sqid>/", SqidApiRedirectView.as_view(), name="sqid_short_api_redirect")
|
|
57
|
+
)
|
|
58
|
+
except Exception:
|
|
59
|
+
logger.warning(
|
|
60
|
+
"Failed to monkey-patch short SQID URLs. The standard /plugins/sqids/ routes still work.", exc_info=True
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
config = NetBoxSqidsConfig
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""API redirect view for resolving SQIDs to REST API detail endpoints."""
|
|
2
|
+
|
|
3
|
+
from django.core.exceptions import ObjectDoesNotExist
|
|
4
|
+
from django.http import Http404, HttpResponseRedirect
|
|
5
|
+
from django.urls import reverse
|
|
6
|
+
from django.views import View
|
|
7
|
+
from utilities.views import get_viewname
|
|
8
|
+
|
|
9
|
+
from netbox_sqids.sqids import resolve_sqid
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SqidApiRedirectView(View):
|
|
13
|
+
"""Resolve a SQID and 302 redirect to the object's REST API detail endpoint."""
|
|
14
|
+
|
|
15
|
+
def get(self, request, sqid):
|
|
16
|
+
try:
|
|
17
|
+
obj = resolve_sqid(sqid)
|
|
18
|
+
except (ValueError, ObjectDoesNotExist) as exc:
|
|
19
|
+
raise Http404 from exc
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
viewname = get_viewname(obj, action="detail", rest_api=True)
|
|
23
|
+
url = reverse(viewname, kwargs={"pk": obj.pk})
|
|
24
|
+
except (AttributeError, Exception) as exc:
|
|
25
|
+
raise Http404 from exc
|
|
26
|
+
|
|
27
|
+
return HttpResponseRedirect(url)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""SQID encoding, decoding, and descriptor for NetBox models.
|
|
2
|
+
|
|
3
|
+
This module provides the core SQID logic: a factory for creating Sqids
|
|
4
|
+
encoder instances, a Python descriptor for computed sqid properties,
|
|
5
|
+
and a resolver function for decoding SQIDs back to model instances.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from django.contrib.contenttypes.models import ContentType
|
|
9
|
+
from sqids import Sqids
|
|
10
|
+
|
|
11
|
+
ALPHABET = "0123456789ACDEFGHJKLMNPQRSTUVWXYZ"
|
|
12
|
+
"""33-character alphabet excluding B, I, O to avoid visual ambiguity."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_sqids_instance(min_length: int = 4, blocklist: list[str] | None = None) -> Sqids:
|
|
16
|
+
"""Create a configured Sqids encoder/decoder instance.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
min_length: Minimum output string length.
|
|
20
|
+
blocklist: Words to filter from generated SQIDs. None uses the
|
|
21
|
+
extended default blocklist.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
A Sqids instance configured with the custom alphabet.
|
|
25
|
+
"""
|
|
26
|
+
if blocklist is None:
|
|
27
|
+
from sqids.constants import DEFAULT_BLOCKLIST
|
|
28
|
+
|
|
29
|
+
blocklist = list(DEFAULT_BLOCKLIST) + ["ck", "sex", "butt"]
|
|
30
|
+
return Sqids(alphabet=ALPHABET, min_length=min_length, blocklist=blocklist)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
_sqids_instance = None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _get_instance() -> Sqids:
|
|
37
|
+
"""Return the module-level Sqids singleton, creating it on first use."""
|
|
38
|
+
global _sqids_instance
|
|
39
|
+
if _sqids_instance is None:
|
|
40
|
+
from django.conf import settings
|
|
41
|
+
|
|
42
|
+
plugin_settings = settings.PLUGINS_CONFIG.get("netbox_sqids", {})
|
|
43
|
+
_sqids_instance = get_sqids_instance(
|
|
44
|
+
min_length=plugin_settings.get("min_length", 4),
|
|
45
|
+
blocklist=plugin_settings.get("blocklist", None),
|
|
46
|
+
)
|
|
47
|
+
return _sqids_instance
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class SqidDescriptor:
|
|
51
|
+
"""Read-only descriptor that computes a SQID from an object's content type and PK.
|
|
52
|
+
|
|
53
|
+
Attached to models via ``add_to_class('sqid', SqidDescriptor())`` in
|
|
54
|
+
``PluginConfig.ready()``. Returns None for unsaved objects and non-integer PKs.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
def __get__(self, obj, objtype=None):
|
|
58
|
+
if obj is None:
|
|
59
|
+
return self
|
|
60
|
+
if obj.pk is None:
|
|
61
|
+
return None
|
|
62
|
+
if not isinstance(obj.pk, int):
|
|
63
|
+
return None
|
|
64
|
+
ct_id = ContentType.objects.get_for_model(obj).pk
|
|
65
|
+
return _get_instance().encode([ct_id, obj.pk])
|
|
66
|
+
|
|
67
|
+
def __set__(self, obj, value):
|
|
68
|
+
raise AttributeError("sqid is read-only")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def resolve_sqid(sqid_str: str):
|
|
72
|
+
"""Decode a SQID string and return the corresponding model instance.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
ValueError: if sqid_str is empty or decodes to unexpected length
|
|
76
|
+
ObjectDoesNotExist: if the content type or object doesn't exist
|
|
77
|
+
"""
|
|
78
|
+
ids = _get_instance().decode(sqid_str)
|
|
79
|
+
if len(ids) != 2:
|
|
80
|
+
raise ValueError(f"Invalid SQID: {sqid_str}")
|
|
81
|
+
ct_id, obj_id = ids
|
|
82
|
+
ct = ContentType.objects.get_for_id(ct_id)
|
|
83
|
+
return ct.get_object_for_this_type(pk=obj_id)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Browser redirect view for resolving SQIDs to object detail pages."""
|
|
2
|
+
|
|
3
|
+
from django.core.exceptions import ObjectDoesNotExist
|
|
4
|
+
from django.http import Http404, HttpResponseRedirect
|
|
5
|
+
from django.views import View
|
|
6
|
+
|
|
7
|
+
from netbox_sqids.sqids import resolve_sqid
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SqidRedirectView(View):
|
|
11
|
+
"""Resolve a SQID and 302 redirect to the object's ``get_absolute_url()``."""
|
|
12
|
+
|
|
13
|
+
def get(self, request, sqid):
|
|
14
|
+
try:
|
|
15
|
+
obj = resolve_sqid(sqid)
|
|
16
|
+
except (ValueError, ObjectDoesNotExist) as exc:
|
|
17
|
+
raise Http404 from exc
|
|
18
|
+
|
|
19
|
+
if not hasattr(obj, "get_absolute_url"):
|
|
20
|
+
raise Http404
|
|
21
|
+
|
|
22
|
+
return HttpResponseRedirect(obj.get_absolute_url())
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: netbox-sqids
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: NetBox plugin that adds short, URL-safe, globally unique SQID identifiers to every model
|
|
5
|
+
Author-email: Jonathan Senecal <contact@jonathansenecal.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/jsenecal/netbox-sqids
|
|
7
|
+
Project-URL: Source, https://github.com/jsenecal/netbox-sqids
|
|
8
|
+
Project-URL: Tracker, https://github.com/jsenecal/netbox-sqids/issues
|
|
9
|
+
Project-URL: Documentation, https://jsenecal.github.io/netbox-sqids/
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: sqids>=0.4.1
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: bumpver; extra == "dev"
|
|
23
|
+
Requires-Dist: pre-commit>=4.0.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-django>=4.5.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest-cov>=3.0.0; extra == "dev"
|
|
27
|
+
Requires-Dist: ruff; extra == "dev"
|
|
28
|
+
Provides-Extra: docs
|
|
29
|
+
Requires-Dist: zensical; extra == "docs"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# NetBox SQIDs
|
|
33
|
+
|
|
34
|
+
[](https://github.com/jsenecal/netbox-sqids/actions/workflows/ci.yml)
|
|
35
|
+
|
|
36
|
+
> **Alpha** — this plugin is functional but under active development. The API and SQID encoding may change before 1.0.
|
|
37
|
+
|
|
38
|
+
Short, URL-safe, globally unique identifiers for every NetBox object.
|
|
39
|
+
|
|
40
|
+
NetBox SQIDs adds a computed `sqid` property to every model in your NetBox instance. SQIDs encode the content type and primary key into a compact string that can be shared, bookmarked, and resolved back to the original object — no database changes required.
|
|
41
|
+
|
|
42
|
+
## Quick Example
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
>>> device = Device.objects.first()
|
|
46
|
+
>>> device.sqid
|
|
47
|
+
'WK1J'
|
|
48
|
+
|
|
49
|
+
>>> from netbox_sqids.sqids import resolve_sqid
|
|
50
|
+
>>> resolve_sqid('WK1J')
|
|
51
|
+
<Device: my-switch>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Visit `/s/WK1J/` in your browser to be redirected to the device's detail page.
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
- **Zero-config** — install and every model gets a `.sqid` property
|
|
59
|
+
- **No migrations** — pure computed properties, no database changes
|
|
60
|
+
- **Globally unique** — SQIDs are unique across all models
|
|
61
|
+
- **URL-safe** — curated 33-character alphabet, no ambiguous characters
|
|
62
|
+
- **Redirect views** — resolve any SQID via browser or API
|
|
63
|
+
- **Short URLs** — optional root-level routes like `/s/WK1J/`
|
|
64
|
+
|
|
65
|
+
## Requirements
|
|
66
|
+
|
|
67
|
+
| Dependency | Version |
|
|
68
|
+
|------------|----------|
|
|
69
|
+
| NetBox | >= 4.5.0 |
|
|
70
|
+
| Python | >= 3.12 |
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install netbox-sqids
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Add to your NetBox `configuration.py`:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
PLUGINS = ["netbox_sqids"]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Restart NetBox. That's it.
|
|
85
|
+
|
|
86
|
+
## Configuration
|
|
87
|
+
|
|
88
|
+
All settings are optional:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
PLUGINS_CONFIG = {
|
|
92
|
+
"netbox_sqids": {
|
|
93
|
+
"min_length": 4, # Minimum SQID length (default: 4)
|
|
94
|
+
"blocklist": None, # Word blocklist (None = extended default)
|
|
95
|
+
"monkeypatched_url_prefix": "s", # Short URL prefix (None to disable)
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## URL Routes
|
|
101
|
+
|
|
102
|
+
| Route | Description |
|
|
103
|
+
|-------|-------------|
|
|
104
|
+
| `/plugins/sqids/<sqid>/` | Browser redirect to object |
|
|
105
|
+
| `/api/plugins/sqids/<sqid>/` | API redirect to object |
|
|
106
|
+
| `/s/<sqid>/` | Short browser redirect (configurable) |
|
|
107
|
+
| `/api/s/<sqid>/` | Short API redirect (configurable) |
|
|
108
|
+
|
|
109
|
+
## Documentation
|
|
110
|
+
|
|
111
|
+
Full documentation: [jsenecal.github.io/netbox-sqids](https://jsenecal.github.io/netbox-sqids/)
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
netbox_sqids/__init__.py
|
|
5
|
+
netbox_sqids/sqids.py
|
|
6
|
+
netbox_sqids/urls.py
|
|
7
|
+
netbox_sqids/views.py
|
|
8
|
+
netbox_sqids.egg-info/PKG-INFO
|
|
9
|
+
netbox_sqids.egg-info/SOURCES.txt
|
|
10
|
+
netbox_sqids.egg-info/dependency_links.txt
|
|
11
|
+
netbox_sqids.egg-info/requires.txt
|
|
12
|
+
netbox_sqids.egg-info/top_level.txt
|
|
13
|
+
netbox_sqids/api/__init__.py
|
|
14
|
+
netbox_sqids/api/urls.py
|
|
15
|
+
netbox_sqids/api/views.py
|
|
16
|
+
tests/test_sqids.py
|
|
17
|
+
tests/test_views.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
netbox_sqids
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[tool.setuptools.packages.find]
|
|
6
|
+
include = ["netbox_sqids", "netbox_sqids.*"]
|
|
7
|
+
|
|
8
|
+
[project]
|
|
9
|
+
name = "netbox-sqids"
|
|
10
|
+
version = "0.1.1"
|
|
11
|
+
description = "NetBox plugin that adds short, URL-safe, globally unique SQID identifiers to every model"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Jonathan Senecal", email = "contact@jonathansenecal.com" },
|
|
15
|
+
]
|
|
16
|
+
license-files = ["LICENSE"]
|
|
17
|
+
requires-python = ">=3.12"
|
|
18
|
+
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 3 - Alpha",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Natural Language :: English",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
dependencies = [
|
|
30
|
+
"sqids>=0.4.1",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
dev = [
|
|
35
|
+
"bumpver",
|
|
36
|
+
"pre-commit>=4.0.0",
|
|
37
|
+
"pytest",
|
|
38
|
+
"pytest-django>=4.5.0",
|
|
39
|
+
"pytest-cov>=3.0.0",
|
|
40
|
+
"ruff",
|
|
41
|
+
]
|
|
42
|
+
docs = [
|
|
43
|
+
"zensical",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[project.urls]
|
|
47
|
+
Homepage = "https://github.com/jsenecal/netbox-sqids"
|
|
48
|
+
Source = "https://github.com/jsenecal/netbox-sqids"
|
|
49
|
+
Tracker = "https://github.com/jsenecal/netbox-sqids/issues"
|
|
50
|
+
Documentation = "https://jsenecal.github.io/netbox-sqids/"
|
|
51
|
+
|
|
52
|
+
[tool.bumpver]
|
|
53
|
+
current_version = "0.1.1"
|
|
54
|
+
version_pattern = "MAJOR.MINOR.PATCH"
|
|
55
|
+
commit_message = "chore: bump version {old_version} -> {new_version}"
|
|
56
|
+
tag_pattern = "vMAJOR.MINOR.PATCH"
|
|
57
|
+
commit = true
|
|
58
|
+
tag = true
|
|
59
|
+
push = true
|
|
60
|
+
|
|
61
|
+
[tool.bumpver.file_patterns]
|
|
62
|
+
"pyproject.toml" = [
|
|
63
|
+
'current_version = "{version}"',
|
|
64
|
+
'version = "{version}"',
|
|
65
|
+
]
|
|
66
|
+
"netbox_sqids/__init__.py" = [
|
|
67
|
+
'__version__ = "{version}"',
|
|
68
|
+
]
|
|
69
|
+
"CHANGELOG.md" = [
|
|
70
|
+
'## \[Unreleased\]',
|
|
71
|
+
'## \[{version}\] - {now:%Y-%m-%d}',
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
[tool.ruff]
|
|
75
|
+
line-length = 120
|
|
76
|
+
target-version = "py312"
|
|
77
|
+
extend-exclude = ["**/migrations/*.py"]
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint]
|
|
80
|
+
select = ["E", "F", "W", "I", "N", "UP", "S", "B", "A", "C4", "DJ", "PIE"]
|
|
81
|
+
ignore = ["E501", "N806", "S101", "DJ01"]
|
|
82
|
+
|
|
83
|
+
[tool.ruff.lint.per-file-ignores]
|
|
84
|
+
"tests/*" = ["S101", "S105", "S106", "E402", "F841"]
|
|
85
|
+
"migrations/*" = ["N806", "N999"]
|
|
86
|
+
|
|
87
|
+
[tool.ruff.format]
|
|
88
|
+
quote-style = "double"
|
|
89
|
+
indent-style = "space"
|
|
90
|
+
|
|
91
|
+
[tool.pytest.ini_options]
|
|
92
|
+
DJANGO_SETTINGS_MODULE = "netbox.settings"
|
|
93
|
+
testpaths = ["tests"]
|
|
94
|
+
python_files = ["test_*.py"]
|
|
95
|
+
python_classes = ["Test*"]
|
|
96
|
+
python_functions = ["test_*"]
|
|
97
|
+
addopts = "-v --cov=netbox_sqids --cov-report=term-missing --reuse-db"
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from unittest.mock import MagicMock, patch
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from django.core.exceptions import ObjectDoesNotExist
|
|
5
|
+
|
|
6
|
+
from netbox_sqids.sqids import SqidDescriptor, get_sqids_instance, resolve_sqid
|
|
7
|
+
|
|
8
|
+
ALPHABET = "0123456789ACDEFGHJKLMNPQRSTUVWXYZ"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestSqidsInstance:
|
|
12
|
+
def test_encodes_two_integers(self):
|
|
13
|
+
instance = get_sqids_instance(min_length=4, blocklist=[])
|
|
14
|
+
result = instance.encode([1, 1])
|
|
15
|
+
assert isinstance(result, str)
|
|
16
|
+
assert len(result) >= 4
|
|
17
|
+
|
|
18
|
+
def test_roundtrip(self):
|
|
19
|
+
instance = get_sqids_instance(min_length=4, blocklist=[])
|
|
20
|
+
encoded = instance.encode([42, 100])
|
|
21
|
+
decoded = instance.decode(encoded)
|
|
22
|
+
assert decoded == [42, 100]
|
|
23
|
+
|
|
24
|
+
def test_uses_custom_alphabet(self):
|
|
25
|
+
instance = get_sqids_instance(min_length=4, blocklist=[])
|
|
26
|
+
encoded = instance.encode([1, 1])
|
|
27
|
+
for char in encoded:
|
|
28
|
+
assert char in ALPHABET
|
|
29
|
+
|
|
30
|
+
def test_min_length_respected(self):
|
|
31
|
+
instance = get_sqids_instance(min_length=8, blocklist=[])
|
|
32
|
+
encoded = instance.encode([1, 1])
|
|
33
|
+
assert len(encoded) >= 8
|
|
34
|
+
|
|
35
|
+
def test_blocklist_filters_words(self):
|
|
36
|
+
instance = get_sqids_instance(min_length=1, blocklist=["sex"])
|
|
37
|
+
encoded = instance.encode([1, 1])
|
|
38
|
+
assert "sex" not in encoded.lower()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class TestSqidDescriptor:
|
|
42
|
+
def test_class_access_returns_descriptor(self):
|
|
43
|
+
class FakeModel:
|
|
44
|
+
sqid = SqidDescriptor()
|
|
45
|
+
|
|
46
|
+
assert isinstance(FakeModel.sqid, SqidDescriptor)
|
|
47
|
+
|
|
48
|
+
def test_unsaved_object_returns_none(self):
|
|
49
|
+
class FakeModel:
|
|
50
|
+
pk = None
|
|
51
|
+
sqid = SqidDescriptor()
|
|
52
|
+
|
|
53
|
+
obj = FakeModel()
|
|
54
|
+
assert obj.sqid is None
|
|
55
|
+
|
|
56
|
+
@patch("netbox_sqids.sqids.ContentType")
|
|
57
|
+
def test_returns_encoded_sqid(self, mock_ct_class):
|
|
58
|
+
mock_ct = MagicMock()
|
|
59
|
+
mock_ct.pk = 5
|
|
60
|
+
mock_ct_class.objects.get_for_model.return_value = mock_ct
|
|
61
|
+
|
|
62
|
+
class FakeModel:
|
|
63
|
+
pk = 42
|
|
64
|
+
sqid = SqidDescriptor()
|
|
65
|
+
|
|
66
|
+
obj = FakeModel()
|
|
67
|
+
|
|
68
|
+
result = obj.sqid
|
|
69
|
+
assert isinstance(result, str)
|
|
70
|
+
assert len(result) >= 4
|
|
71
|
+
|
|
72
|
+
# Verify roundtrip
|
|
73
|
+
instance = get_sqids_instance()
|
|
74
|
+
decoded = instance.decode(result)
|
|
75
|
+
assert decoded == [5, 42]
|
|
76
|
+
|
|
77
|
+
@patch("netbox_sqids.sqids.ContentType")
|
|
78
|
+
def test_read_only(self, mock_ct_class):
|
|
79
|
+
class FakeModel:
|
|
80
|
+
pk = 1
|
|
81
|
+
sqid = SqidDescriptor()
|
|
82
|
+
|
|
83
|
+
obj = FakeModel()
|
|
84
|
+
with pytest.raises(AttributeError, match="read-only"):
|
|
85
|
+
obj.sqid = "something"
|
|
86
|
+
|
|
87
|
+
def test_non_integer_pk_returns_none(self):
|
|
88
|
+
class FakeModel:
|
|
89
|
+
pk = "uuid-string"
|
|
90
|
+
sqid = SqidDescriptor()
|
|
91
|
+
|
|
92
|
+
obj = FakeModel()
|
|
93
|
+
assert obj.sqid is None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class TestResolveSqid:
|
|
97
|
+
def test_empty_string_raises_value_error(self):
|
|
98
|
+
with pytest.raises(ValueError, match="Invalid SQID"):
|
|
99
|
+
resolve_sqid("")
|
|
100
|
+
|
|
101
|
+
def test_invalid_sqid_raises_value_error(self):
|
|
102
|
+
# encode a single integer — should fail the len!=2 check
|
|
103
|
+
instance = get_sqids_instance()
|
|
104
|
+
bad_sqid = instance.encode([1])
|
|
105
|
+
with pytest.raises(ValueError, match="Invalid SQID"):
|
|
106
|
+
resolve_sqid(bad_sqid)
|
|
107
|
+
|
|
108
|
+
@patch("netbox_sqids.sqids.ContentType")
|
|
109
|
+
def test_resolves_valid_sqid(self, mock_ct_class):
|
|
110
|
+
instance = get_sqids_instance()
|
|
111
|
+
sqid_str = instance.encode([5, 42])
|
|
112
|
+
|
|
113
|
+
mock_obj = MagicMock()
|
|
114
|
+
mock_ct = MagicMock()
|
|
115
|
+
mock_ct_class.objects.get_for_id.return_value = mock_ct
|
|
116
|
+
mock_ct.get_object_for_this_type.return_value = mock_obj
|
|
117
|
+
|
|
118
|
+
result = resolve_sqid(sqid_str)
|
|
119
|
+
|
|
120
|
+
assert result is mock_obj
|
|
121
|
+
mock_ct_class.objects.get_for_id.assert_called_once_with(5)
|
|
122
|
+
mock_ct.get_object_for_this_type.assert_called_once_with(pk=42)
|
|
123
|
+
|
|
124
|
+
@patch("netbox_sqids.sqids.ContentType")
|
|
125
|
+
def test_nonexistent_content_type_raises(self, mock_ct_class):
|
|
126
|
+
instance = get_sqids_instance()
|
|
127
|
+
sqid_str = instance.encode([9999, 1])
|
|
128
|
+
|
|
129
|
+
mock_ct_class.objects.get_for_id.side_effect = ObjectDoesNotExist()
|
|
130
|
+
|
|
131
|
+
with pytest.raises(ObjectDoesNotExist):
|
|
132
|
+
resolve_sqid(sqid_str)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
from unittest.mock import MagicMock, patch
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from django.test import RequestFactory
|
|
5
|
+
|
|
6
|
+
from netbox_sqids.api.views import SqidApiRedirectView
|
|
7
|
+
from netbox_sqids.views import SqidRedirectView
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TestSqidRedirectView:
|
|
11
|
+
def setup_method(self):
|
|
12
|
+
self.factory = RequestFactory()
|
|
13
|
+
|
|
14
|
+
@patch("netbox_sqids.views.resolve_sqid")
|
|
15
|
+
def test_redirects_to_absolute_url(self, mock_resolve):
|
|
16
|
+
mock_obj = MagicMock()
|
|
17
|
+
mock_obj.get_absolute_url.return_value = "/dcim/devices/42/"
|
|
18
|
+
mock_resolve.return_value = mock_obj
|
|
19
|
+
|
|
20
|
+
request = self.factory.get("/plugins/sqids/ABCD/")
|
|
21
|
+
response = SqidRedirectView.as_view()(request, sqid="ABCD")
|
|
22
|
+
|
|
23
|
+
assert response.status_code == 302
|
|
24
|
+
assert response.url == "/dcim/devices/42/"
|
|
25
|
+
|
|
26
|
+
@patch("netbox_sqids.views.resolve_sqid")
|
|
27
|
+
def test_invalid_sqid_returns_404(self, mock_resolve):
|
|
28
|
+
from django.http import Http404
|
|
29
|
+
|
|
30
|
+
mock_resolve.side_effect = ValueError("Invalid SQID")
|
|
31
|
+
|
|
32
|
+
request = self.factory.get("/plugins/sqids/XXXX/")
|
|
33
|
+
with pytest.raises(Http404):
|
|
34
|
+
SqidRedirectView.as_view()(request, sqid="XXXX")
|
|
35
|
+
|
|
36
|
+
@patch("netbox_sqids.views.resolve_sqid")
|
|
37
|
+
def test_object_not_found_returns_404(self, mock_resolve):
|
|
38
|
+
from django.core.exceptions import ObjectDoesNotExist
|
|
39
|
+
from django.http import Http404
|
|
40
|
+
|
|
41
|
+
mock_resolve.side_effect = ObjectDoesNotExist()
|
|
42
|
+
|
|
43
|
+
request = self.factory.get("/plugins/sqids/ABCD/")
|
|
44
|
+
with pytest.raises(Http404):
|
|
45
|
+
SqidRedirectView.as_view()(request, sqid="ABCD")
|
|
46
|
+
|
|
47
|
+
@patch("netbox_sqids.views.resolve_sqid")
|
|
48
|
+
def test_no_get_absolute_url_returns_404(self, mock_resolve):
|
|
49
|
+
from django.http import Http404
|
|
50
|
+
|
|
51
|
+
mock_obj = MagicMock(spec=[]) # no get_absolute_url
|
|
52
|
+
mock_resolve.return_value = mock_obj
|
|
53
|
+
|
|
54
|
+
request = self.factory.get("/plugins/sqids/ABCD/")
|
|
55
|
+
with pytest.raises(Http404):
|
|
56
|
+
SqidRedirectView.as_view()(request, sqid="ABCD")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class TestSqidApiRedirectView:
|
|
60
|
+
def setup_method(self):
|
|
61
|
+
self.factory = RequestFactory()
|
|
62
|
+
|
|
63
|
+
@patch("netbox_sqids.api.views.reverse")
|
|
64
|
+
@patch("netbox_sqids.api.views.get_viewname")
|
|
65
|
+
@patch("netbox_sqids.api.views.resolve_sqid")
|
|
66
|
+
def test_redirects_to_api_url(self, mock_resolve, mock_get_viewname, mock_reverse):
|
|
67
|
+
mock_obj = MagicMock()
|
|
68
|
+
mock_obj.pk = 42
|
|
69
|
+
mock_resolve.return_value = mock_obj
|
|
70
|
+
mock_get_viewname.return_value = "dcim-api:device-detail"
|
|
71
|
+
mock_reverse.return_value = "/api/dcim/devices/42/"
|
|
72
|
+
|
|
73
|
+
request = self.factory.get("/api/plugins/sqids/ABCD/")
|
|
74
|
+
response = SqidApiRedirectView.as_view()(request, sqid="ABCD")
|
|
75
|
+
|
|
76
|
+
assert response.status_code == 302
|
|
77
|
+
assert response.url == "/api/dcim/devices/42/"
|
|
78
|
+
mock_get_viewname.assert_called_once_with(mock_obj, action="detail", rest_api=True)
|
|
79
|
+
mock_reverse.assert_called_once_with("dcim-api:device-detail", kwargs={"pk": 42})
|
|
80
|
+
|
|
81
|
+
@patch("netbox_sqids.api.views.resolve_sqid")
|
|
82
|
+
def test_invalid_sqid_returns_404(self, mock_resolve):
|
|
83
|
+
from django.http import Http404
|
|
84
|
+
|
|
85
|
+
mock_resolve.side_effect = ValueError("Invalid SQID")
|
|
86
|
+
|
|
87
|
+
request = self.factory.get("/api/plugins/sqids/XXXX/")
|
|
88
|
+
with pytest.raises(Http404):
|
|
89
|
+
SqidApiRedirectView.as_view()(request, sqid="XXXX")
|
|
90
|
+
|
|
91
|
+
@patch("netbox_sqids.api.views.resolve_sqid")
|
|
92
|
+
def test_object_not_found_returns_404(self, mock_resolve):
|
|
93
|
+
from django.core.exceptions import ObjectDoesNotExist
|
|
94
|
+
from django.http import Http404
|
|
95
|
+
|
|
96
|
+
mock_resolve.side_effect = ObjectDoesNotExist()
|
|
97
|
+
|
|
98
|
+
request = self.factory.get("/api/plugins/sqids/ABCD/")
|
|
99
|
+
with pytest.raises(Http404):
|
|
100
|
+
SqidApiRedirectView.as_view()(request, sqid="ABCD")
|
|
101
|
+
|
|
102
|
+
@patch("netbox_sqids.api.views.get_viewname")
|
|
103
|
+
@patch("netbox_sqids.api.views.resolve_sqid")
|
|
104
|
+
def test_no_api_viewname_returns_404(self, mock_resolve, mock_get_viewname):
|
|
105
|
+
from django.http import Http404
|
|
106
|
+
|
|
107
|
+
mock_obj = MagicMock()
|
|
108
|
+
mock_resolve.return_value = mock_obj
|
|
109
|
+
mock_get_viewname.side_effect = AttributeError()
|
|
110
|
+
|
|
111
|
+
request = self.factory.get("/api/plugins/sqids/ABCD/")
|
|
112
|
+
with pytest.raises(Http404):
|
|
113
|
+
SqidApiRedirectView.as_view()(request, sqid="ABCD")
|