diff --git a/openwisp_monitoring/device/api/views.py b/openwisp_monitoring/device/api/views.py index aa9f22f1..a97cae9f 100644 --- a/openwisp_monitoring/device/api/views.py +++ b/openwisp_monitoring/device/api/views.py @@ -8,6 +8,7 @@ from django.core.exceptions import ValidationError from django.db.models import Count, Q from django.db.models.functions import Round +from django.http import Http404 from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from django_filters.rest_framework import DjangoFilterBackend @@ -111,7 +112,7 @@ class DeviceMetricView( model = DeviceData queryset = ( - DeviceData.objects.filter(organization__is_active=True, _is_deactivated=False) + DeviceData.objects.filter(organization__is_active=True) .only( 'id', 'key', @@ -173,6 +174,11 @@ def get_object(self, pk): def post(self, request, pk): self.instance = self.get_object(pk) + if self.instance._is_deactivated: + # If the device is deactivated, do not accept data. + # We don't use "Device.is_deactivated()" to avoid + # generating query for the related config. + raise Http404 self.instance.data = request.data # validate incoming data try: diff --git a/openwisp_monitoring/device/apps.py b/openwisp_monitoring/device/apps.py index 13f804a2..4cbbd347 100644 --- a/openwisp_monitoring/device/apps.py +++ b/openwisp_monitoring/device/apps.py @@ -155,16 +155,6 @@ def connect_device_signals(self): sender=Device, dispatch_uid='device_deactivated_update_devicemonitoring', ) - device_deactivated.connect( - DeviceMetricView.invalidate_get_device_cache, - sender=Device, - dispatch_uid=('deactivate_device_invalidate_view_device_cache'), - ) - device_deactivated.connect( - DeviceMetricView.invalidate_get_charts_cache, - sender=Device, - dispatch_uid=('deactivate_device_invalidate_view_charts_cache'), - ) @classmethod def device_post_save_receiver(cls, instance, created, **kwargs): diff --git a/openwisp_monitoring/device/tests/test_api.py b/openwisp_monitoring/device/tests/test_api.py index 877307e2..3d6c3799 100644 --- a/openwisp_monitoring/device/tests/test_api.py +++ b/openwisp_monitoring/device/tests/test_api.py @@ -177,16 +177,15 @@ def test_200_none(self): def test_200_create(self): self.create_test_data(no_resources=True) - def test_device_deactivate_cache_invalidation(self): - device = self._create_device() - # Ensure device ID is cached by the view - data = {'type': 'DeviceMonitoring'} - response = self._post_data(device.id, device.key, data) - self.assertEqual(response.status_code, 200) + def test_deactivated_device_read_only(self): + self.create_test_data(no_resources=True) + device = self.device_model.objects.first() device.deactivate() - - response = self._post_data(device.id, device.key, data) + response = self._post_data(device.id, device.key, {'type': 'DeviceMonitoring'}) self.assertEqual(response.status_code, 404) + # Read requests should continue to work + response = self.client.get(self._url(device.pk, device.key)) + self.assertEqual(response.status_code, 200) @patch('openwisp_monitoring.device.tasks.write_device_metrics.delay') def test_background_write(self, mocked_task):