diff --git a/netbox_device_map/forms.py b/netbox_device_map/forms.py index 980ea1a..2d5b9a5 100644 --- a/netbox_device_map/forms.py +++ b/netbox_device_map/forms.py @@ -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"} diff --git a/netbox_device_map/helpers.py b/netbox_device_map/helpers.py index 6215ead..b85db72 100644 --- a/netbox_device_map/helpers.py +++ b/netbox_device_map/helpers.py @@ -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))) diff --git a/netbox_device_map/views.py b/netbox_device_map/views.py index 61c7b58..b3fe485 100644 --- a/netbox_device_map/views.py +++ b/netbox_device_map/views.py @@ -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 )) @@ -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: