Skip to content

Commit

Permalink
[req-change] Eliminated extra query
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 11, 2024
1 parent c1849b1 commit 4e891cc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ class Migration(migrations.Migration):
],
default="modified",
help_text=(
'"modified" means the configuration is not applied yet;'
' \n"applied" means the configuration is applied successfully;'
' \n"error" means the configuration caused issues and it was'
' rolled back;'
'"deactivating" means the device has been deactivated and all the'
' configuration is being removed; \n'
'"deactivated" means the configuration has been removed from'
' the device;'
'"modified" means the configuration is not applied yet; \n'
'"applied" means the configuration is applied successfully; \n'
'"error" means the configuration caused issues and it was'
' rolled back; \n"deactivating" means the device has been'
' deactivated and the configuration is being removed; \n'
'"deactivated" means the configuration has been removed '
'from the device;'
),
max_length=100,
no_check_for_status=True,
Expand Down
41 changes: 0 additions & 41 deletions openwisp_controller/config/migrations/0056_alter_config_status.py

This file was deleted.

6 changes: 3 additions & 3 deletions openwisp_controller/connection/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def test_put_devceconnection_detail(self):
'enabled': False,
'failure_reason': '',
}
with self.assertNumQueries(16):
with self.assertNumQueries(15):
response = self.client.put(path, data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
Expand All @@ -517,7 +517,7 @@ def test_patch_deviceconnectoin_detail(self):
path = reverse('connection_api:deviceconnection_detail', args=(d1, dc.pk))
self.assertEqual(dc.update_strategy, app_settings.UPDATE_STRATEGIES[0][0])
data = {'update_strategy': app_settings.UPDATE_STRATEGIES[1][0]}
with self.assertNumQueries(15):
with self.assertNumQueries(14):
response = self.client.patch(path, data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
Expand All @@ -528,7 +528,7 @@ def test_delete_deviceconnection_detail(self):
dc = self._create_device_connection()
d1 = dc.device.id
path = reverse('connection_api:deviceconnection_detail', args=(d1, dc.pk))
with self.assertNumQueries(11):
with self.assertNumQueries(10):
response = self.client.delete(path)
self.assertEqual(response.status_code, 204)

Expand Down
1 change: 1 addition & 0 deletions openwisp_controller/geo/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class DeviceLocationView(
lookup_field = 'content_object'
lookup_url_kwarg = 'pk'
organization_field = 'content_object__organization'
_device_field = 'content_object'

def get_queryset(self):
qs = super().get_queryset()
Expand Down
8 changes: 4 additions & 4 deletions openwisp_controller/geo/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ def test_update_devicelocation_change_location_outdoor_to_indoor(self):
}
self.assertEqual(device_location.location.type, 'outdoor')
self.assertEqual(device_location.floorplan, None)
with self.assertNumQueries(22):
with self.assertNumQueries(21):
response = self.client.put(
path, encode_multipart(BOUNDARY, data), content_type=MULTIPART_CONTENT
)
Expand All @@ -882,7 +882,7 @@ def test_update_devicelocation_patch_indoor(self):
'indoor': '0,0',
}
self.assertEqual(device_location.indoor, '-140.38620,40.369227')
with self.assertNumQueries(11):
with self.assertNumQueries(10):
response = self.client.patch(path, data, content_type='application/json')
self.assertEqual(response.status_code, 200)
device_location.refresh_from_db()
Expand All @@ -899,7 +899,7 @@ def test_update_devicelocation_floorplan_related_id(self):
data = {
'floorplan': str(floor2.id),
}
with self.assertNumQueries(13):
with self.assertNumQueries(12):
response = self.client.patch(path, data, content_type='application/json')
self.assertEqual(response.status_code, 200)
device_location.refresh_from_db()
Expand All @@ -913,7 +913,7 @@ def test_update_devicelocation_location_related_id(self):
data = {
'location': str(location2.id),
}
with self.assertNumQueries(10):
with self.assertNumQueries(9):
response = self.client.patch(path, data, content_type='application/json')
self.assertEqual(response.status_code, 200)
device_location.refresh_from_db()
Expand Down
12 changes: 9 additions & 3 deletions openwisp_controller/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@


class RelatedDeviceModelPermission(DjangoModelPermissions):
def _has_permissions(self, request, view, perm):
_device_field = 'device'

def _has_permissions(self, request, view, perm, obj=None):
if request.method in self.READ_ONLY_METHOD:
return perm
return perm and not view.get_parent_queryset()[0].is_deactivated()
if obj:
device = getattr(obj, self._device_field)
else:
device = view.get_parent_queryset()[0]
return perm and not device.is_deactivated()

def has_permission(self, request, view):
perm = super().has_permission(request, view)
return self._has_permissions(request, view, perm)

def has_object_permission(self, request, view, obj):
perm = super().has_object_permission(request, view, obj)
return self._has_permissions(request, view, perm)
return self._has_permissions(request, view, perm, obj)


class RelatedDeviceProtectedAPIMixin(
Expand Down

0 comments on commit 4e891cc

Please sign in to comment.