Skip to content

Commit d83d03a

Browse files
committed
[req-change] Eliminated extra query
1 parent c1849b1 commit d83d03a

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

openwisp_controller/connection/tests/test_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def test_put_devceconnection_detail(self):
503503
'enabled': False,
504504
'failure_reason': '',
505505
}
506-
with self.assertNumQueries(16):
506+
with self.assertNumQueries(15):
507507
response = self.client.put(path, data, content_type='application/json')
508508
self.assertEqual(response.status_code, 200)
509509
self.assertEqual(
@@ -517,7 +517,7 @@ def test_patch_deviceconnectoin_detail(self):
517517
path = reverse('connection_api:deviceconnection_detail', args=(d1, dc.pk))
518518
self.assertEqual(dc.update_strategy, app_settings.UPDATE_STRATEGIES[0][0])
519519
data = {'update_strategy': app_settings.UPDATE_STRATEGIES[1][0]}
520-
with self.assertNumQueries(15):
520+
with self.assertNumQueries(14):
521521
response = self.client.patch(path, data, content_type='application/json')
522522
self.assertEqual(response.status_code, 200)
523523
self.assertEqual(
@@ -528,7 +528,7 @@ def test_delete_deviceconnection_detail(self):
528528
dc = self._create_device_connection()
529529
d1 = dc.device.id
530530
path = reverse('connection_api:deviceconnection_detail', args=(d1, dc.pk))
531-
with self.assertNumQueries(11):
531+
with self.assertNumQueries(10):
532532
response = self.client.delete(path)
533533
self.assertEqual(response.status_code, 204)
534534

openwisp_controller/geo/api/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class DeviceLocationView(
114114
lookup_field = 'content_object'
115115
lookup_url_kwarg = 'pk'
116116
organization_field = 'content_object__organization'
117+
_device_field = 'content_object'
117118

118119
def get_queryset(self):
119120
qs = super().get_queryset()

openwisp_controller/geo/tests/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ def test_update_devicelocation_change_location_outdoor_to_indoor(self):
863863
}
864864
self.assertEqual(device_location.location.type, 'outdoor')
865865
self.assertEqual(device_location.floorplan, None)
866-
with self.assertNumQueries(22):
866+
with self.assertNumQueries(21):
867867
response = self.client.put(
868868
path, encode_multipart(BOUNDARY, data), content_type=MULTIPART_CONTENT
869869
)
@@ -882,7 +882,7 @@ def test_update_devicelocation_patch_indoor(self):
882882
'indoor': '0,0',
883883
}
884884
self.assertEqual(device_location.indoor, '-140.38620,40.369227')
885-
with self.assertNumQueries(11):
885+
with self.assertNumQueries(10):
886886
response = self.client.patch(path, data, content_type='application/json')
887887
self.assertEqual(response.status_code, 200)
888888
device_location.refresh_from_db()
@@ -899,7 +899,7 @@ def test_update_devicelocation_floorplan_related_id(self):
899899
data = {
900900
'floorplan': str(floor2.id),
901901
}
902-
with self.assertNumQueries(13):
902+
with self.assertNumQueries(12):
903903
response = self.client.patch(path, data, content_type='application/json')
904904
self.assertEqual(response.status_code, 200)
905905
device_location.refresh_from_db()
@@ -913,7 +913,7 @@ def test_update_devicelocation_location_related_id(self):
913913
data = {
914914
'location': str(location2.id),
915915
}
916-
with self.assertNumQueries(10):
916+
with self.assertNumQueries(9):
917917
response = self.client.patch(path, data, content_type='application/json')
918918
self.assertEqual(response.status_code, 200)
919919
device_location.refresh_from_db()

openwisp_controller/mixins.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55

66
class RelatedDeviceModelPermission(DjangoModelPermissions):
7-
def _has_permissions(self, request, view, perm):
7+
_device_field = 'device'
8+
9+
def _has_permissions(self, request, view, perm, obj=None):
810
if request.method in self.READ_ONLY_METHOD:
911
return perm
10-
return perm and not view.get_parent_queryset()[0].is_deactivated()
12+
if obj:
13+
device = getattr(obj, self._device_field)
14+
else:
15+
device = view.get_parent_queryset()[0]
16+
return perm and not device.is_deactivated()
1117

1218
def has_permission(self, request, view):
1319
perm = super().has_permission(request, view)
1420
return self._has_permissions(request, view, perm)
1521

1622
def has_object_permission(self, request, view, obj):
1723
perm = super().has_object_permission(request, view, obj)
18-
return self._has_permissions(request, view, perm)
24+
return self._has_permissions(request, view, perm, obj)
1925

2026

2127
class RelatedDeviceProtectedAPIMixin(

0 commit comments

Comments
 (0)