Skip to content

Commit 4e8bf0a

Browse files
committed
[chores:fix] Allow enabling estimated location only if whois enabled
1 parent 755a66f commit 4e8bf0a

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

openwisp_controller/geo/base/models.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212
from swapper import get_model_name
1313

14-
from openwisp_controller.config import settings as config_settings
14+
from openwisp_controller.config import settings as config_app_settings
1515
from openwisp_controller.geo import settings as geo_settings
1616
from openwisp_controller.geo.estimated_location.service import EstimatedLocationService
1717
from openwisp_users.mixins import OrgMixin, ValidateOrgMixin
@@ -185,7 +185,7 @@ def __str__(self):
185185
}
186186

187187
def clean(self):
188-
if not config_settings.WHOIS_CONFIGURED and self.estimated_location_enabled:
188+
if not config_app_settings.WHOIS_CONFIGURED and self.estimated_location_enabled:
189189
raise ValidationError(
190190
{
191191
"estimated_location_enabled": _(
@@ -194,6 +194,20 @@ def clean(self):
194194
)
195195
}
196196
)
197+
config_settings = getattr(self.organization, "config_settings", None)
198+
if (
199+
config_settings
200+
and not config_settings.whois_enabled
201+
and self.estimated_location_enabled
202+
):
203+
raise ValidationError(
204+
{
205+
"estimated_location_enabled": _(
206+
"The WHOIS feature must be enabled for this organization "
207+
"before enabling the Estimated Location feature."
208+
)
209+
}
210+
)
197211

198212
@classmethod
199213
def organization_post_save_receiver(cls, sender, instance, created, **kwargs):

openwisp_controller/geo/estimated_location/tests/tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ def test_organization_geo_settings_validation(self):
115115
# Should not raise
116116
geo_settings.full_clean()
117117

118+
with self.subTest(
119+
"Estimated location cannot be enabled when WHOIS is disabled for the org"
120+
):
121+
org.config_settings.whois_enabled = False
122+
org.config_settings.save()
123+
geo_settings = org.geo_settings
124+
geo_settings.estimated_location_enabled = True
125+
with self.assertRaises(ValidationError) as context_manager:
126+
geo_settings.full_clean()
127+
self.assertIn(
128+
"estimated_location_enabled",
129+
context_manager.exception.message_dict,
130+
)
131+
118132
location_model = Location
119133

120134
def test_estimated_location_field(self):

0 commit comments

Comments
 (0)