Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions netbox_device_map/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DeviceMapFilterForm(BootstrapMixin, forms.Form):
)
vlan = DynamicModelChoiceField(
queryset=VLAN.objects.all(),
required=False,
label="VLAN",
help_text="Filter devices by VLAN attached to any device interface",
query_params={"group_id": "$vlan_group"}
Expand Down
5 changes: 4 additions & 1 deletion netbox_device_map/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@


def get_device_location(device: Device) -> LatLon | None:
"""Extract device geolocation from special custom field"""
"""If netbox latitude and longitude fields are populated for a device then use them."""
if device.latitude and device.longitude:
return (device.latitude, device.longitude)
"""... Otherwise extract device geolocation from special custom field"""
if location_cf := device.custom_field_data.get(LOCATION_CF_NAME):
return tuple(map(float, location_cf.replace(' ', '').split(',', maxsplit=1)))

Expand Down
21 changes: 11 additions & 10 deletions netbox_device_map/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ def get(self, request):
"""Device map view"""
form = self.form(request.GET)
if form.is_valid():
interfaces = Interface.objects.all()
vlan = form.cleaned_data['vlan']
#interfaces = Interface.objects.all()
#vlan = form.cleaned_data['vlan']

interfaces = interfaces.filter(Q(untagged_vlan=vlan) | Q(tagged_vlans=vlan))
devices = Device.objects.filter(interfaces__in=interfaces).distinct()
#interfaces = interfaces.filter(Q(untagged_vlan=vlan) | Q(tagged_vlans=vlan))
devices = Device.objects.all().distinct()
if device_roles := form.cleaned_data['device_roles']:
devices = devices.filter(device_role__in=device_roles)
devices = devices.filter(role__in=device_roles)
print(f"Device roles are :{device_roles}")

geolocated_devices = {d: coords for d in devices if (coords := get_device_location(d))}
non_geolocated_devices = set(devices) - set(geolocated_devices.keys())

map_data = configure_leaflet_map("geomap", geolocated_devices, form.cleaned_data['calculate_connections'])
map_data['vlan'] = vlan.id
#map_data['vlan'] = vlan.id
return render(request, self.template_name, context=dict(
filter_form=form, map_data=map_data, non_geolocated_devices=non_geolocated_devices
))
Expand All @@ -61,12 +62,12 @@ def get(self, request, **kwargs):
form = self.form(request.GET)
if form.is_valid():
data = form.cleaned_data
connected_devices_qs = get_connected_devices(device, vlan=data['vlan'])\
.filter(device_role__name=plugin_settings['cpe_device_role']).order_by()
connected_devices_qs = get_connected_devices(device, vlan=data['vlan'])
#\
# .filter(device_role__name=plugin_settings['cpe_device_role']).order_by()
connected_devices = [dict(id=d.id, name=d.name, url=d.get_absolute_url(), comments=d.comments)
for d in connected_devices_qs]
# Sorting list of CPE devices by the sequence of integers contained in the comments
connected_devices.sort(key=lambda d: tuple(int(n) for n in INTEGER_REGEXP.findall(d['comments'])))

return JsonResponse(dict(status=True, cpe_devices=connected_devices,
device_type=f'{device.device_type.manufacturer.name} {device.device_type.model}'))
else:
Expand Down