netbox-spatial-lens 0.4.0__py3-none-any.whl

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 (109) hide show
  1. netbox_spatial_lens/__init__.py +87 -0
  2. netbox_spatial_lens/api/__init__.py +0 -0
  3. netbox_spatial_lens/api/serializers.py +98 -0
  4. netbox_spatial_lens/api/urls.py +12 -0
  5. netbox_spatial_lens/api/views.py +164 -0
  6. netbox_spatial_lens/cabling.py +329 -0
  7. netbox_spatial_lens/choices.py +18 -0
  8. netbox_spatial_lens/device_overlays.py +195 -0
  9. netbox_spatial_lens/elevation.py +383 -0
  10. netbox_spatial_lens/field_filters.py +152 -0
  11. netbox_spatial_lens/filtersets.py +92 -0
  12. netbox_spatial_lens/floor_cabling.py +404 -0
  13. netbox_spatial_lens/floor_scene.py +293 -0
  14. netbox_spatial_lens/forms.py +187 -0
  15. netbox_spatial_lens/geometry.py +347 -0
  16. netbox_spatial_lens/integrations/__init__.py +0 -0
  17. netbox_spatial_lens/integrations/floorplan.py +90 -0
  18. netbox_spatial_lens/layout.py +305 -0
  19. netbox_spatial_lens/management/__init__.py +0 -0
  20. netbox_spatial_lens/management/commands/__init__.py +0 -0
  21. netbox_spatial_lens/management/commands/lens_autoplace.py +160 -0
  22. netbox_spatial_lens/management/commands/lens_enrich.py +684 -0
  23. netbox_spatial_lens/migrations/0001_initial.py +74 -0
  24. netbox_spatial_lens/migrations/0002_floorlayer.py +48 -0
  25. netbox_spatial_lens/migrations/0003_alter_floorlayer_file_alter_floorlayer_opacity.py +26 -0
  26. netbox_spatial_lens/migrations/__init__.py +0 -0
  27. netbox_spatial_lens/models.py +397 -0
  28. netbox_spatial_lens/navigation.py +50 -0
  29. netbox_spatial_lens/overlays.py +608 -0
  30. netbox_spatial_lens/palette.py +286 -0
  31. netbox_spatial_lens/ports.py +131 -0
  32. netbox_spatial_lens/power.py +218 -0
  33. netbox_spatial_lens/scene.py +483 -0
  34. netbox_spatial_lens/search.py +14 -0
  35. netbox_spatial_lens/signals.py +44 -0
  36. netbox_spatial_lens/site_overlays.py +131 -0
  37. netbox_spatial_lens/static/netbox_spatial_lens/cabinet3d.js +232 -0
  38. netbox_spatial_lens/static/netbox_spatial_lens/editor.js +399 -0
  39. netbox_spatial_lens/static/netbox_spatial_lens/finder.js +196 -0
  40. netbox_spatial_lens/static/netbox_spatial_lens/floor.js +285 -0
  41. netbox_spatial_lens/static/netbox_spatial_lens/floor3d.js +466 -0
  42. netbox_spatial_lens/static/netbox_spatial_lens/legend.js +129 -0
  43. netbox_spatial_lens/static/netbox_spatial_lens/lens.css +1507 -0
  44. netbox_spatial_lens/static/netbox_spatial_lens/rack3d.js +513 -0
  45. netbox_spatial_lens/static/netbox_spatial_lens/rack_panel.js +305 -0
  46. netbox_spatial_lens/static/netbox_spatial_lens/stage3d.js +592 -0
  47. netbox_spatial_lens/static/netbox_spatial_lens/state.js +32 -0
  48. netbox_spatial_lens/static/netbox_spatial_lens/world.js +654 -0
  49. netbox_spatial_lens/static/netbox_spatial_lens/zoom.js +146 -0
  50. netbox_spatial_lens/tables.py +86 -0
  51. netbox_spatial_lens/tags.py +36 -0
  52. netbox_spatial_lens/templates/netbox_spatial_lens/floor.html +284 -0
  53. netbox_spatial_lens/templates/netbox_spatial_lens/floor_layout.html +84 -0
  54. netbox_spatial_lens/templates/netbox_spatial_lens/inc/alloc_row.html +16 -0
  55. netbox_spatial_lens/templates/netbox_spatial_lens/inc/cable_row.html +22 -0
  56. netbox_spatial_lens/templates/netbox_spatial_lens/inc/device_cabling.html +37 -0
  57. netbox_spatial_lens/templates/netbox_spatial_lens/inc/field_finders.html +9 -0
  58. netbox_spatial_lens/templates/netbox_spatial_lens/inc/finder.html +43 -0
  59. netbox_spatial_lens/templates/netbox_spatial_lens/inc/finder_cable.html +23 -0
  60. netbox_spatial_lens/templates/netbox_spatial_lens/inc/finder_circuit.html +16 -0
  61. netbox_spatial_lens/templates/netbox_spatial_lens/inc/finder_field.html +11 -0
  62. netbox_spatial_lens/templates/netbox_spatial_lens/inc/finder_site.html +16 -0
  63. netbox_spatial_lens/templates/netbox_spatial_lens/inc/finder_tag.html +10 -0
  64. netbox_spatial_lens/templates/netbox_spatial_lens/inc/floor_svg.html +187 -0
  65. netbox_spatial_lens/templates/netbox_spatial_lens/inc/help.html +10 -0
  66. netbox_spatial_lens/templates/netbox_spatial_lens/inc/legend.html +65 -0
  67. netbox_spatial_lens/templates/netbox_spatial_lens/inc/levels.html +39 -0
  68. netbox_spatial_lens/templates/netbox_spatial_lens/inc/palette.html +8 -0
  69. netbox_spatial_lens/templates/netbox_spatial_lens/inc/stats.html +20 -0
  70. netbox_spatial_lens/templates/netbox_spatial_lens/inc/zoom.html +15 -0
  71. netbox_spatial_lens/templates/netbox_spatial_lens/rack.html +288 -0
  72. netbox_spatial_lens/templates/netbox_spatial_lens/site.html +88 -0
  73. netbox_spatial_lens/templates/netbox_spatial_lens/world.html +146 -0
  74. netbox_spatial_lens/templatetags/__init__.py +0 -0
  75. netbox_spatial_lens/templatetags/lens.py +85 -0
  76. netbox_spatial_lens/tests/__init__.py +0 -0
  77. netbox_spatial_lens/tests/base.py +143 -0
  78. netbox_spatial_lens/tests/test_autoplace.py +53 -0
  79. netbox_spatial_lens/tests/test_branching.py +142 -0
  80. netbox_spatial_lens/tests/test_device_overlays.py +140 -0
  81. netbox_spatial_lens/tests/test_docs.py +148 -0
  82. netbox_spatial_lens/tests/test_elevation.py +337 -0
  83. netbox_spatial_lens/tests/test_field_filters.py +130 -0
  84. netbox_spatial_lens/tests/test_floor_cabling.py +212 -0
  85. netbox_spatial_lens/tests/test_floor_scene.py +306 -0
  86. netbox_spatial_lens/tests/test_floorplan_import.py +64 -0
  87. netbox_spatial_lens/tests/test_geometry.py +100 -0
  88. netbox_spatial_lens/tests/test_layers.py +183 -0
  89. netbox_spatial_lens/tests/test_legend_bands.py +53 -0
  90. netbox_spatial_lens/tests/test_models.py +54 -0
  91. netbox_spatial_lens/tests/test_overlays.py +295 -0
  92. netbox_spatial_lens/tests/test_placement_rules.py +119 -0
  93. netbox_spatial_lens/tests/test_plan.py +283 -0
  94. netbox_spatial_lens/tests/test_rear_devices.py +52 -0
  95. netbox_spatial_lens/tests/test_scene.py +269 -0
  96. netbox_spatial_lens/tests/test_site_overlays.py +186 -0
  97. netbox_spatial_lens/tests/test_tracing.py +80 -0
  98. netbox_spatial_lens/tests/test_ui.py +210 -0
  99. netbox_spatial_lens/tests/test_views.py +768 -0
  100. netbox_spatial_lens/tests/test_visibility.py +85 -0
  101. netbox_spatial_lens/tracing.py +276 -0
  102. netbox_spatial_lens/urls.py +37 -0
  103. netbox_spatial_lens/views.py +597 -0
  104. netbox_spatial_lens/world.py +472 -0
  105. netbox_spatial_lens-0.4.0.dist-info/METADATA +129 -0
  106. netbox_spatial_lens-0.4.0.dist-info/RECORD +109 -0
  107. netbox_spatial_lens-0.4.0.dist-info/WHEEL +5 -0
  108. netbox_spatial_lens-0.4.0.dist-info/licenses/LICENSE +21 -0
  109. netbox_spatial_lens-0.4.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,87 @@
1
+ from netbox.plugins import PluginConfig
2
+
3
+ __version__ = '0.4.0'
4
+
5
+
6
+ class SpatialLensConfig(PluginConfig):
7
+ name = 'netbox_spatial_lens'
8
+ verbose_name = 'NetBox Spatial Lens'
9
+ description = 'World map, floor plans and rack elevations for NetBox, recoloured by power, cooling and space'
10
+ version = __version__
11
+ base_url = 'spatial-lens'
12
+ # The rack cooling fields this plugin colours by (cooling_capability, cooling_capacity)
13
+ # arrived in NetBox 4.7, so there is no earlier release to support.
14
+ min_version = '4.7.0'
15
+ max_version = '4.7.99'
16
+ default_settings = { # noqa: RUF012
17
+ # Footprint used when a rack declares no outer_width or outer_depth, in millimetres.
18
+ # 600x1070 is a standard 19-inch cabinet. A rack drawn at this size is reported as
19
+ # estimated rather than silently passed off as measured.
20
+ 'default_rack_width': 600,
21
+ 'default_rack_depth': 1070,
22
+ # Grid the editor snaps to, in centimetres. 0 disables snapping.
23
+ 'grid_size': 10,
24
+ # Custom fields offered as filters, the way tags are, on the floor (racks) and in the rack
25
+ # view (devices): a list of field names. Only select and multiselect fields are used.
26
+ 'filter_custom_fields': [],
27
+ # Which built-in overlays to register. True for all, False for none, or a list of
28
+ # names from 'power', 'cooling', 'space' and 'role'.
29
+ 'enable_builtin_overlays': True,
30
+ # Overlay selected when a floor is opened without one named.
31
+ 'default_overlay': 'power',
32
+ # Which built-in device colourings a rack offers. True for all, False for none, or a
33
+ # list from 'role', 'status', 'tenant', 'cabling' and 'power'.
34
+ 'enable_builtin_device_overlays': True,
35
+ # Colouring selected when a rack is opened without one named.
36
+ 'default_device_overlay': 'role',
37
+ # Which built-in site colourings the world map offers. True for all, False for none, or
38
+ # a list from 'group', 'status', 'tenant' and 'region'.
39
+ 'enable_builtin_site_overlays': True,
40
+ # Colouring selected when the map is opened without one named.
41
+ 'default_site_overlay': 'group',
42
+ # Tiles for the world map. OpenStreetMap's own tiles are the default because they
43
+ # need no API key; CARTO's basemaps now watermark every tile without one. Point this
44
+ # at your own tile server, or set it to None, on a site with no egress. Without tiles
45
+ # the globe is drawn on a plain ground, with the same sites and circuits on it.
46
+ #
47
+ # A URL containing {theme} gets 'light' or 'dark' substituted, so a basemap offering
48
+ # both follows the NetBox theme.
49
+ 'map_tile_url': 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
50
+ 'map_attribution': '© OpenStreetMap contributors',
51
+ # Where MapLibre is served from. Bundling it would put a minified library in a Python
52
+ # package; naming it here lets a deployment host its own copy instead.
53
+ #
54
+ # MapLibre rather than Leaflet because it draws a globe: zoomed out, an estate on two
55
+ # continents reads as one earth rather than as a strip of repeating flat maps with the
56
+ # same site on it three times, and there is no antimeridian to fall off. It needs WebGL:
57
+ # a browser without it, or one that cannot load these two files, is told so on the page,
58
+ # and the figures, legends and lists there still work.
59
+ 'map_css': 'https://cdnjs.cloudflare.com/ajax/libs/maplibre-gl/5.6.1/maplibre-gl.min.css',
60
+ 'map_js': 'https://cdnjs.cloudflare.com/ajax/libs/maplibre-gl/5.6.1/maplibre-gl.min.js',
61
+ # Where the map's letters come from. A raster basemap carries no fonts of its own, and a
62
+ # renderer that draws its own labels needs them. Point this at your own glyph server on
63
+ # a site with no egress; without it the markers are drawn and the names are not.
64
+ 'map_glyphs': 'https://fonts.openmaptiles.org/{fontstack}/{range}.pbf',
65
+ # Where the 3D rack and floor drawings load Three.js from: a copy of the `three` npm
66
+ # package, laid out as published, so `build/three.module.js` and `examples/jsm/` sit under
67
+ # it. Pinned, because the addons under `examples/jsm/` must come from the same release as
68
+ # the core. Empty turns both drawings off: the rack page keeps its panels and figures, and
69
+ # the floor opens on its 2D plan.
70
+ 'three_base': 'https://cdn.jsdelivr.net/npm/three@0.186.0/',
71
+ }
72
+
73
+ def ready(self) -> None:
74
+ super().ready()
75
+
76
+ from . import (
77
+ device_overlays,
78
+ overlays,
79
+ signals, # noqa: F401 (connects the receivers)
80
+ site_overlays,
81
+ )
82
+
83
+ for level in (overlays, device_overlays, site_overlays):
84
+ level.registry.register_configured_builtins()
85
+
86
+
87
+ config = SpatialLensConfig
File without changes
@@ -0,0 +1,98 @@
1
+ from dcim.api.serializers import LocationSerializer, RackSerializer, SiteSerializer
2
+ from netbox.api.serializers import NetBoxModelSerializer
3
+ from rest_framework import serializers
4
+
5
+ from netbox_spatial_lens.models import Floor, FloorLayer, RackPlacement
6
+
7
+ __all__ = (
8
+ 'FloorLayerSerializer',
9
+ 'FloorSerializer',
10
+ 'RackPlacementSerializer',
11
+ )
12
+
13
+
14
+ class FloorSerializer(NetBoxModelSerializer):
15
+ url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_spatial_lens-api:floor-detail')
16
+ site = SiteSerializer(nested=True, required=False, allow_null=True)
17
+ location = LocationSerializer(nested=True, required=False, allow_null=True)
18
+ placement_count = serializers.IntegerField(read_only=True)
19
+
20
+ class Meta:
21
+ model = Floor
22
+ fields = (
23
+ 'id',
24
+ 'url',
25
+ 'display',
26
+ 'name',
27
+ 'site',
28
+ 'location',
29
+ 'width',
30
+ 'depth',
31
+ 'unit',
32
+ 'placement_count',
33
+ 'description',
34
+ 'comments',
35
+ 'tags',
36
+ 'custom_fields',
37
+ 'created',
38
+ 'last_updated',
39
+ )
40
+ brief_fields = ('id', 'url', 'display', 'name')
41
+
42
+
43
+ class RackPlacementSerializer(NetBoxModelSerializer):
44
+ url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_spatial_lens-api:rackplacement-detail')
45
+ floor = FloorSerializer(nested=True)
46
+ rack = RackSerializer(nested=True)
47
+
48
+ class Meta:
49
+ model = RackPlacement
50
+ fields = (
51
+ 'id',
52
+ 'url',
53
+ 'display',
54
+ 'floor',
55
+ 'rack',
56
+ 'x',
57
+ 'y',
58
+ 'rotation',
59
+ 'tags',
60
+ 'custom_fields',
61
+ 'created',
62
+ 'last_updated',
63
+ )
64
+ brief_fields = ('id', 'url', 'display', 'rack', 'x', 'y', 'rotation')
65
+
66
+
67
+ class FloorLayerSerializer(NetBoxModelSerializer):
68
+ url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_spatial_lens-api:floorlayer-detail')
69
+ floor = FloorSerializer(nested=True)
70
+ # Where the browser actually fetches the image, whether it was uploaded or linked. A
71
+ # client should not have to know which of the two fields was filled in.
72
+ source = serializers.CharField(read_only=True)
73
+
74
+ class Meta:
75
+ model = FloorLayer
76
+ fields = (
77
+ 'id',
78
+ 'url',
79
+ 'display',
80
+ 'floor',
81
+ 'name',
82
+ 'file',
83
+ 'external_url',
84
+ 'source',
85
+ 'x',
86
+ 'y',
87
+ 'width',
88
+ 'height',
89
+ 'rotation',
90
+ 'opacity',
91
+ 'weight',
92
+ 'enabled',
93
+ 'tags',
94
+ 'custom_fields',
95
+ 'created',
96
+ 'last_updated',
97
+ )
98
+ brief_fields = ('id', 'url', 'display', 'name', 'source')
@@ -0,0 +1,12 @@
1
+ from netbox.api.routers import NetBoxRouter
2
+
3
+ from netbox_spatial_lens.api import views
4
+
5
+ app_name = 'netbox_spatial_lens-api'
6
+
7
+ router = NetBoxRouter()
8
+ router.register('floors', views.FloorViewSet)
9
+ router.register('floor-layers', views.FloorLayerViewSet)
10
+ router.register('rack-placements', views.RackPlacementViewSet)
11
+
12
+ urlpatterns = router.urls
@@ -0,0 +1,164 @@
1
+ """
2
+ REST API.
3
+
4
+ The placement editor writes through these endpoints rather than through a private one of its
5
+ own, so there is a single way to move a rack and a single permission guarding it.
6
+ """
7
+
8
+ from django.db.models import Count
9
+ from netbox.api.viewsets import NetBoxModelViewSet
10
+ from rest_framework.decorators import action
11
+ from rest_framework.response import Response
12
+
13
+ from netbox_spatial_lens import filtersets
14
+ from netbox_spatial_lens.api.serializers import FloorLayerSerializer, FloorSerializer, RackPlacementSerializer
15
+ from netbox_spatial_lens.models import Floor, FloorLayer, RackPlacement
16
+
17
+ __all__ = (
18
+ 'FloorLayerViewSet',
19
+ 'FloorViewSet',
20
+ 'RackPlacementViewSet',
21
+ )
22
+
23
+
24
+ class FloorViewSet(NetBoxModelViewSet):
25
+ queryset = Floor.objects.prefetch_related('tags').annotate(placement_count=Count('placements'))
26
+ serializer_class = FloorSerializer
27
+ filterset_class = filtersets.FloorFilterSet
28
+
29
+ @action(detail=True, methods=['get'])
30
+ def layout(self, request, pk=None):
31
+ """
32
+ Everything one render of this floor needs, in a single response.
33
+
34
+ The web view builds the same structure server-side. This endpoint exists so a second
35
+ renderer, such as a 3D projection, consumes the floor without restating how a
36
+ footprint is resolved or how an overlay is evaluated.
37
+ """
38
+ from dcim.models import Device, Rack
39
+
40
+ from netbox_spatial_lens.floor_cabling import build_floor_exits, build_floor_runs
41
+ from netbox_spatial_lens.layout import build_layout, resolve_overlay
42
+
43
+ floor = self.get_object()
44
+ overlay = resolve_overlay(request.GET.get('overlay'))
45
+ # Restricted as the floor page is: a placement is the plugin's own object, the rack and
46
+ # the devices behind it are not, and the API must not show what the page hides.
47
+ placed = build_layout(
48
+ floor,
49
+ overlay,
50
+ racks=Rack.objects.restrict(request.user, 'view'),
51
+ devices=Device.objects.restrict(request.user, 'view'),
52
+ )
53
+ # As on the page: the runs cost a query, so they are sent only when asked for.
54
+ show_runs = request.GET.get('runs') == '1'
55
+ runs = build_floor_runs(placed) if show_runs else []
56
+ exits = build_floor_exits(placed, floor, user=request.user) if show_runs else []
57
+
58
+ return Response(
59
+ {
60
+ 'floor': {
61
+ 'id': floor.pk,
62
+ 'name': floor.name,
63
+ 'width_cm': floor.width_cm,
64
+ 'depth_cm': floor.depth_cm,
65
+ },
66
+ 'overlay': {
67
+ 'name': overlay.name,
68
+ 'label': overlay.label,
69
+ 'legend': [
70
+ {'colour': e.colour, 'label': e.label} for e in overlay.legend_for([p.value for p in placed])
71
+ ],
72
+ }
73
+ if overlay
74
+ else None,
75
+ 'racks': [
76
+ {
77
+ 'rack_id': p.rack.pk,
78
+ 'name': p.rack.name,
79
+ 'x': p.x,
80
+ 'y': p.y,
81
+ 'rotation': p.rotation,
82
+ 'width': p.width,
83
+ 'depth': p.depth,
84
+ 'u_height': p.rack.u_height,
85
+ 'estimated_footprint': p.estimated,
86
+ 'colour': p.colour,
87
+ 'label': p.label,
88
+ # Where this rack sits between empty and full, or null where the
89
+ # overlay measures a name rather than a quantity. A second renderer
90
+ # needs the magnitude for the same reason the plan does: a band alone
91
+ # cannot tell 12% from 48%.
92
+ 'fraction': p.fraction,
93
+ 'facts': [[label, value] for label, value in p.facts],
94
+ }
95
+ for p in placed
96
+ ],
97
+ 'layers': [
98
+ {
99
+ 'id': layer.pk,
100
+ 'name': layer.name,
101
+ 'source': layer.source,
102
+ 'x': float(layer.x),
103
+ 'y': float(layer.y),
104
+ 'width': float(layer.width),
105
+ 'height': float(layer.height),
106
+ 'rotation': float(layer.rotation),
107
+ 'opacity': float(layer.opacity),
108
+ }
109
+ for layer in floor.layers.filter(enabled=True)
110
+ ],
111
+ 'runs': [
112
+ {
113
+ 'rack_a': run.a.rack.pk,
114
+ 'rack_b': run.b.rack.pk,
115
+ 'count': run.count,
116
+ }
117
+ for run in runs
118
+ ],
119
+ 'exits': [
120
+ {
121
+ 'rack': item.rack.rack.pk,
122
+ 'label': item.label,
123
+ 'kind': item.kind,
124
+ 'count': item.count,
125
+ 'x': item.x,
126
+ 'y': item.y,
127
+ }
128
+ for item in exits
129
+ ],
130
+ }
131
+ )
132
+
133
+ @action(detail=True, methods=['get'])
134
+ def devices(self, request, pk=None):
135
+ """
136
+ The devices in every rack on this floor, placed in their cabinets.
137
+
138
+ What the floor's Devices view draws, fetched when the reader switches to it rather than
139
+ rendered into every floor page. Restricted like the layout: racks and devices the caller
140
+ may not view are left out.
141
+ """
142
+ from dcim.models import Device, Rack
143
+
144
+ from netbox_spatial_lens.floor_scene import build_floor_devices
145
+
146
+ return Response(
147
+ build_floor_devices(
148
+ self.get_object(),
149
+ racks=Rack.objects.restrict(request.user, 'view'),
150
+ devices=Device.objects.restrict(request.user, 'view'),
151
+ )
152
+ )
153
+
154
+
155
+ class RackPlacementViewSet(NetBoxModelViewSet):
156
+ queryset = RackPlacement.objects.prefetch_related('tags').select_related('floor', 'rack')
157
+ serializer_class = RackPlacementSerializer
158
+ filterset_class = filtersets.RackPlacementFilterSet
159
+
160
+
161
+ class FloorLayerViewSet(NetBoxModelViewSet):
162
+ queryset = FloorLayer.objects.prefetch_related('tags').select_related('floor')
163
+ serializer_class = FloorLayerSerializer
164
+ filterset_class = filtersets.FloorLayerFilterSet