File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from copy import copy
2+
13from django .contrib .humanize .templatetags .humanize import ordinal
24from django .core .exceptions import ValidationError
3- from django .db import transaction
5+ from django .db import models , transaction
46from django .urls import reverse
57from django .utils .translation import gettext_lazy as _
68from 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
3347class LocationDeviceSerializer (ValidatedModelSerializer ):
3448 admin_edit_url = SerializerMethodField ("get_admin_edit_url" )
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments