Skip to content

Commit 8d38dee

Browse files
committed
[fix] Fixed the OrganizationGeoSettingsSerializer
1 parent 9f4b5f3 commit 8d38dee

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

openwisp_controller/geo/api/serializers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from copy import copy
2+
13
from django.contrib.humanize.templatetags.humanize import ordinal
24
from django.core.exceptions import ValidationError
3-
from django.db import transaction
5+
from django.db import models, transaction
46
from django.urls import reverse
57
from django.utils.translation import gettext_lazy as _
68
from rest_framework import serializers
@@ -29,6 +31,18 @@ class Meta:
2931
fields = "__all__"
3032
read_only_fields = ["id", "organization"]
3133

34+
# Workaround for https://github.com/openwisp/openwisp-utils/issues/633
35+
# TODO: Remove when the Bug is fixed in openwisp-utils
36+
def validate(self, data):
37+
Model = self.Meta.model
38+
instance = copy(self.instance) if self.instance else Model()
39+
for key, value in data.items():
40+
# avoid direct assignment for m2m (not allowed)
41+
if not isinstance(Model._meta.get_field(key), models.ManyToManyField):
42+
setattr(instance, key, value)
43+
instance.full_clean(exclude=self.exclude_validation)
44+
return data
45+
3246

3347
class LocationDeviceSerializer(ValidatedModelSerializer):
3448
admin_edit_url = SerializerMethodField("get_admin_edit_url")

openwisp_controller/geo/tests/test_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,26 @@ def test_organization_geo_settings_update(self):
15101510
)
15111511
self.assertEqual(response.status_code, 404)
15121512

1513+
with self.subTest("Validation error when WHOIS not configured"):
1514+
with patch.object(config_app_settings, "WHOIS_CONFIGURED", False):
1515+
response = self.client.put(
1516+
url,
1517+
{"estimated_location_enabled": True},
1518+
content_type="application/json",
1519+
)
1520+
self.assertEqual(response.status_code, 400)
1521+
self.assertIn("estimated_location_enabled", response.data)
1522+
org1_geo_settings.refresh_from_db()
1523+
self.assertEqual(org1_geo_settings.estimated_location_enabled, False)
1524+
1525+
response = self.client.patch(
1526+
url,
1527+
{"estimated_location_enabled": True},
1528+
content_type="application/json",
1529+
)
1530+
self.assertEqual(response.status_code, 400)
1531+
self.assertIn("estimated_location_enabled", response.data)
1532+
15131533
with self.subTest("Superuser can update any organization's geo settings"):
15141534
superuser = self._get_admin()
15151535
self.client.force_login(user=superuser)

0 commit comments

Comments
 (0)