Skip to content

Commit 7a02dde

Browse files
[fix] Skip WHOIS and estimated location for deactivated devices #1325
- add guard in WHOISService to skip processing for deactivated devices - add guard in EstimatedLocationService to prevent task scheduling - fix N+1 query by including _is_deactivated in clear_last_ip_command queryset - add regression tests ensuring tasks are skipped when device is deactivated - ensure deterministic tests by enabling whois on organization settings Fixes #1325
1 parent 8d528e7 commit 7a02dde

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

openwisp_controller/config/whois/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def clear_last_ip_command(stdout, stderr, interactive=True):
3939
)
4040
# include the FK field 'organization' in .only() so the related
4141
# `organization__config_settings` traversal is not deferred
42-
.only("last_ip", "organization", "key")
42+
.only("last_ip", "organization", "key", "_is_deactivated")
4343
)
4444
# Filter out devices that have WHOIS information for their last IP
4545
devices = devices.exclude(last_ip=None).exclude(

openwisp_controller/config/whois/tests/tests.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def test_last_ip_management_command_queries(self):
349349
)
350350
args = ["--noinput"]
351351

352-
with self.assertNumQueries(7):
352+
with self.assertNumQueries(4):
353353
call_command("clear_last_ip", *args, stdout=out, stderr=StringIO())
354354

355355
@mock.patch.object(app_settings, "WHOIS_CONFIGURED", True)
@@ -1219,4 +1219,7 @@ def test_process_ip_runs_when_active(self, mock_task):
12191219
service = WHOISService(self.device)
12201220
service.process_ip_data_and_location()
12211221

1222-
self.assertEqual(mock_task.call_count, 1)
1222+
mock_task.assert_called_once_with(
1223+
device_pk=self.device.pk,
1224+
initial_ip_address=self.device._initial_last_ip,
1225+
)

openwisp_controller/geo/estimated_location/service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def is_estimated_location_enabled(self):
6363
return self.check_estimated_location_enabled(self.device.organization_id)
6464

6565
def trigger_estimated_location_task(self, ip_address):
66+
if self.device.is_deactivated():
67+
return
6668
try:
6769
current_app.send_task(
6870
"whois_estimated_location_task",

0 commit comments

Comments
 (0)