From 5ed611adb00be5aae63b4a34da82da1202ffa16d Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Fri, 16 Jun 2023 21:27:03 +0530 Subject: [PATCH] [fix] Fixed "receive_url" field in TopologyAdmin Bug: When WiFi Mesh integration is enabled, opening the topology add view was generating Server 500 Error. The issue was caused because the topology instance for the ModelForm didn't have primary key set. Fix: Do not show "receive_url" field on add view. --- openwisp_network_topology/admin.py | 8 ++++++++ .../integrations/device/tests/test_wifi_mesh.py | 16 +++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/openwisp_network_topology/admin.py b/openwisp_network_topology/admin.py index d4579603..1b81ad46 100644 --- a/openwisp_network_topology/admin.py +++ b/openwisp_network_topology/admin.py @@ -104,6 +104,14 @@ class TopologyAdmin( receive_url_baseurl = app_settings.TOPOLOGY_API_BASEURL change_form_template = 'admin/topology/topology/change_form.html' + def get_fields(self, request, obj=None): + fields = list(super().get_fields(request, obj)) + if not obj: + # Receive URL cannot be created without an object. + # Hence, remove the "receive_url" field. + fields.remove('receive_url') + return fields + def get_actions(self, request): """ move delete action to last position diff --git a/openwisp_network_topology/integrations/device/tests/test_wifi_mesh.py b/openwisp_network_topology/integrations/device/tests/test_wifi_mesh.py index 5b0c289f..79693990 100644 --- a/openwisp_network_topology/integrations/device/tests/test_wifi_mesh.py +++ b/openwisp_network_topology/integrations/device/tests/test_wifi_mesh.py @@ -266,8 +266,14 @@ def test_topology_admin(self): """ admin = self._create_admin() self.client.force_login(admin) - topology = self._create_topology() - response = self.client.get( - reverse(f'{self.prefix}_topology_change', args=[topology.id]) - ) - self.assertContains(response, 'Wifi mesh') + + with self.subTest('Test add form'): + response = self.client.get(reverse(f'{self.prefix}_topology_add')) + self.assertContains(response, 'Wifi mesh') + + with self.subTest('Test change form'): + topology = self._create_topology() + response = self.client.get( + reverse(f'{self.prefix}_topology_change', args=[topology.id]) + ) + self.assertContains(response, 'Wifi mesh')