Skip to content

Commit

Permalink
[fix] Fixed DeviceMetricView: allow readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 13, 2024
1 parent b7051b0 commit c821a2f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
8 changes: 7 additions & 1 deletion openwisp_monitoring/device/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 0 additions & 10 deletions openwisp_monitoring/device/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 7 additions & 8 deletions openwisp_monitoring/device/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c821a2f

Please sign in to comment.