Skip to content

Commit 3816541

Browse files
committed
[fix] Fixes by @coderabbitai
1 parent 57807fd commit 3816541

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

openwisp_controller/geo/api/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django_filters import rest_framework as filters
66
from rest_framework import generics, pagination, status
77
from rest_framework.exceptions import NotFound, PermissionDenied
8+
from rest_framework.generics import get_object_or_404
89
from rest_framework.permissions import BasePermission
910
from rest_framework.request import clone_request
1011
from rest_framework.response import Response
@@ -352,10 +353,9 @@ class OrganizationGeoSettingsView(ProtectedAPIMixin, generics.RetrieveUpdateAPIV
352353

353354
def get_object(self):
354355
org_id = self.kwargs.get("organization_pk")
355-
try:
356-
return self.get_queryset().get(organization_id=org_id)
357-
except OrganizationGeoSettings.DoesNotExist:
358-
raise Http404(_("Geo settings not found for this organization."))
356+
obj = get_object_or_404(self.get_queryset(), organization_id=org_id)
357+
self.check_object_permissions(self.request, obj)
358+
return obj
359359

360360

361361
# add with_geo filter to device API

openwisp_controller/geo/estimated_location/service.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ def _create_or_update_estimated_location(
8989
device_location = DeviceLocation(content_object=self.device)
9090

9191
current_location = device_location.location
92+
# Re-check whether estimated locations are enabled for the device's
93+
# organization. The check is needed here so the celery worker
94+
# honors current org settings and avoids persisting estimated
95+
# locations when the feature has been disabled since the task was
96+
# enqueued.
97+
if not self.check_estimated_location_enabled(self.device.organization_id):
98+
return current_location
9299
if not current_location or (
93100
attached_devices_exists and current_location.is_estimated
94101
):

openwisp_controller/geo/migrations/0006_create_geo_settings_for_existing_orgs.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ def create_geo_settings_for_existing_orgs(apps, schema_editor):
1111
Create OrganizationGeoSettings for all existing organizations
1212
that don't have one yet.
1313
"""
14-
Organization = apps.get_model("openwisp_users", "Organization")
14+
Organization = get_swapped_model(apps, "openwisp_users", "Organization")
1515
OrganizationGeoSettings = get_swapped_model(apps, "geo", "OrganizationGeoSettings")
1616

17-
for org in Organization.objects.iterator():
18-
OrganizationGeoSettings.objects.get_or_create(organization_id=org.pk)
17+
# Use the migration's DB alias so non-default databases are respected
18+
db_alias = schema_editor.connection.alias
19+
for org in Organization.objects.using(db_alias).iterator():
20+
OrganizationGeoSettings.objects.using(db_alias).get_or_create(
21+
organization_id=org.pk
22+
)
1923

2024

2125
def copy_estimated_location_enabled(apps, schema_editor):
@@ -29,17 +33,20 @@ def copy_estimated_location_enabled(apps, schema_editor):
2933
)
3034
OrganizationGeoSettings = get_swapped_model(apps, "geo", "OrganizationGeoSettings")
3135

36+
# Use the migration's DB alias so non-default databases are respected
37+
db_alias = schema_editor.connection.alias
3238
# Iterate using iterator() to avoid loading all rows into memory.
33-
for cfg in OrganizationConfigSettings.objects.iterator():
34-
geo, _ = OrganizationGeoSettings.objects.get_or_create(
39+
for cfg in OrganizationConfigSettings.objects.using(db_alias).iterator():
40+
geo, _ = OrganizationGeoSettings.objects.using(db_alias).get_or_create(
3541
organization_id=cfg.organization_id
3642
)
3743
# Copy the value if different
3844
if getattr(geo, "estimated_location_enabled", None) != getattr(
3945
cfg, "estimated_location_enabled", None
4046
):
4147
geo.estimated_location_enabled = cfg.estimated_location_enabled
42-
geo.save(update_fields=["estimated_location_enabled"])
48+
# Ensure save uses the same DB alias
49+
geo.save(update_fields=["estimated_location_enabled"], using=db_alias)
4350

4451

4552
class Migration(migrations.Migration):

openwisp_controller/geo/tests/test_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,8 @@ def test_organization_geo_settings_update(self):
15291529
)
15301530
self.assertEqual(response.status_code, 400)
15311531
self.assertIn("estimated_location_enabled", response.data)
1532+
org1_geo_settings.refresh_from_db()
1533+
self.assertEqual(org1_geo_settings.estimated_location_enabled, False)
15321534

15331535
with self.subTest("Superuser can update any organization's geo settings"):
15341536
superuser = self._get_admin()

0 commit comments

Comments
 (0)