Skip to content

Commit 1bdfcb3

Browse files
committed
[chores] Minor improvements and nitpicks suggested by @coderabbitai
1 parent f0c2069 commit 1bdfcb3

7 files changed

Lines changed: 46 additions & 65 deletions

File tree

openwisp_controller/config/base/whois.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def clean(self):
8282
raise ValidationError(
8383
{"cidr": _("Invalid CIDR format: %(error)s") % {"error": str(e)}}
8484
)
85-
8685
if self.coordinates:
8786
if not (-90 <= self.coordinates.y <= 90):
8887
raise ValidationError(
@@ -105,7 +104,6 @@ def device_whois_info_delete_handler(instance, **kwargs):
105104
when device is deleted.
106105
"""
107106
Device = load_model("config", "Device")
108-
109107
last_ip = instance.last_ip
110108
existing_devices = Device.objects.filter(_is_deactivated=False).filter(
111109
last_ip=last_ip

openwisp_controller/config/management/commands/clear_last_ip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class Command(BaseCommand):
77
help = (
8-
"Clears the last IP address (if set) for every active device"
8+
"Clears the last IP address (if set) for active devices without WHOIS records"
99
" across all organizations."
1010
)
1111

@@ -19,7 +19,7 @@ def add_arguments(self, parser):
1919
)
2020
return super().add_arguments(parser)
2121

22-
def handle(self, *args, **options):
22+
def handle(self, *_args, **options):
2323
Device = load_model("config", "Device")
2424
WHOISInfo = load_model("config", "WHOISInfo")
2525

openwisp_controller/config/whois/service.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ def process_whois_details(self, ip_address):
174174
message = EXCEPTION_MESSAGES.get(exc_type)
175175
if exc_type is errors.AddressNotFoundError:
176176
message = message.format(ip_address=ip_address)
177-
raise exc_type(message)
178-
except requests.RequestException as e:
179-
raise e
177+
raise exc_type(message) from e
178+
except requests.RequestException:
179+
raise
180180
else:
181181
# The attributes are always present in the response,
182182
# but they can be None, so added fallbacks.
@@ -284,7 +284,6 @@ def _create_or_update_whois(self, whois_details, whois_instance=None):
284284
Returns the updated or created WHOIS instance along with update fields.
285285
"""
286286
WHOISInfo = load_model("config", "WHOISInfo")
287-
288287
update_fields = []
289288
if whois_instance:
290289
for attr, value in whois_details.items():

openwisp_controller/config/whois/tests/tests.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,11 @@ def test_whois_enabled(self):
155155
app_settings, "WHOIS_CONFIGURED", False
156156
), self.assertRaises(ValidationError) as context_manager:
157157
org_settings_obj.full_clean()
158-
try:
159-
self.assertEqual(
160-
context_manager.exception.message_dict["whois_enabled"][0],
161-
"WHOIS_GEOIP_ACCOUNT and WHOIS_GEOIP_KEY must be set "
162-
+ "before enabling WHOIS feature.",
163-
)
164-
except AssertionError:
165-
self.fail("ValidationError message not equal to expected message.")
158+
self.assertEqual(
159+
context_manager.exception.message_dict["whois_enabled"][0],
160+
"WHOIS_GEOIP_ACCOUNT and WHOIS_GEOIP_KEY must be set "
161+
+ "before enabling WHOIS feature.",
162+
)
166163

167164
with mock.patch.object(app_settings, "WHOIS_CONFIGURED", True):
168165
org_settings_obj.full_clean()
@@ -173,13 +170,13 @@ def test_whois_enabled(self):
173170
org_settings_obj.whois_enabled = True
174171
org_settings_obj.save(update_fields=["whois_enabled"])
175172
org_settings_obj.refresh_from_db(fields=["whois_enabled"])
176-
self.assertEqual(getattr(org_settings_obj, "whois_enabled"), True)
173+
self.assertTrue(org_settings_obj.whois_enabled)
177174

178175
with self.subTest("Test setting WHOIS enabled to False"):
179176
org_settings_obj.whois_enabled = False
180177
org_settings_obj.save(update_fields=["whois_enabled"])
181178
org_settings_obj.refresh_from_db(fields=["whois_enabled"])
182-
self.assertEqual(getattr(org_settings_obj, "whois_enabled"), False)
179+
self.assertFalse(org_settings_obj.whois_enabled)
183180

184181
with self.subTest(
185182
"Test setting WHOIS enabled to None fallbacks to global setting"
@@ -190,7 +187,7 @@ def test_whois_enabled(self):
190187
org_settings_obj.save(update_fields=["whois_enabled"])
191188
org_settings_obj.refresh_from_db(fields=["whois_enabled"])
192189
self.assertEqual(
193-
getattr(org_settings_obj, "whois_enabled"),
190+
org_settings_obj.whois_enabled,
194191
app_settings.WHOIS_ENABLED,
195192
)
196193

@@ -335,28 +332,22 @@ def test_whois_model_fields_validation(self):
335332

336333
with self.assertRaises(ValidationError) as context_manager:
337334
self._create_whois_info(ip_address="127.0.0.1")
338-
try:
339-
self.assertEqual(
340-
context_manager.exception.message_dict["ip_address"][0],
341-
"WHOIS information cannot be created for private IP addresses.",
342-
)
343-
except AssertionError:
344-
self.fail("ValidationError message not equal to expected message.")
335+
self.assertEqual(
336+
context_manager.exception.message_dict["ip_address"][0],
337+
"WHOIS information cannot be created for private IP addresses.",
338+
)
345339

346340
with self.assertRaises(ValidationError):
347341
self._create_whois_info(timezone="a" * 36)
348342

349343
with self.assertRaises(ValidationError) as context_manager:
350344
self._create_whois_info(cidr="InvalidCIDR")
351-
try:
352-
# Not using assertEqual here because we are adding error message raised by
353-
# ipaddress module to the ValidationError message.
354-
self.assertIn(
355-
"Invalid CIDR format: 'InvalidCIDR'",
356-
context_manager.exception.message_dict["cidr"][0],
357-
)
358-
except AssertionError:
359-
self.fail("ValidationError message not equal to expected message.")
345+
# Not using assertEqual here because we are adding error message raised by
346+
# ipaddress module to the ValidationError message.
347+
self.assertIn(
348+
"Invalid CIDR format: 'InvalidCIDR'",
349+
context_manager.exception.message_dict["cidr"][0],
350+
)
360351

361352
with self.assertRaises(ValidationError):
362353
self._create_whois_info(asn="InvalidASNNumber")
@@ -372,13 +363,10 @@ def test_whois_model_fields_validation(self):
372363
with self.assertRaises(ValidationError) as context_manager:
373364
point = Point(longitude, latitude, srid=4326)
374365
self._create_whois_info(coordinates=point)
375-
try:
376-
self.assertEqual(
377-
context_manager.exception.message_dict["coordinates"][0],
378-
expected_msg,
379-
)
380-
except AssertionError:
381-
self.fail("ValidationError message not equal to expected message.")
366+
self.assertEqual(
367+
context_manager.exception.message_dict["coordinates"][0],
368+
expected_msg,
369+
)
382370

383371

384372
class TestWHOISTransaction(

openwisp_controller/config/whois/tests/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ def _task_called(self, mocked_task, task_name="WHOIS lookup"):
130130
mocked_task.reset_mock()
131131

132132
with self.subTest(
133-
f"{task_name} task not called via DeviceChecksumView for no WHOIS record"
133+
f"{task_name} task not called via DeviceChecksumView "
134+
"if no WHOIS record and IP unchanged"
134135
):
135136
WHOISInfo.objects.all().delete()
136137
device.refresh_from_db()

openwisp_controller/geo/base/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def save(self, *args, _set_estimated=False, **kwargs):
9696
changed_fields = {"is_estimated"}
9797
if update_fields := kwargs.get("update_fields"):
9898
kwargs["update_fields"] = set(update_fields) | changed_fields
99-
return super().save(*args, **kwargs)
99+
result = super().save(*args, **kwargs)
100+
self._set_initial_values_for_changed_checked_fields()
101+
return result
100102

101103

102104
class BaseFloorPlan(OrgMixin, AbstractFloorPlan):

openwisp_controller/geo/estimated_location/tests/tests.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,11 @@ def test_estimated_location_configuration_setting(self):
5959
org_settings_obj.whois_enabled = False
6060
org_settings_obj.estimated_location_enabled = True
6161
org_settings_obj.full_clean()
62-
try:
63-
self.assertEqual(
64-
context_manager.exception.message_dict[
65-
"estimated_location_enabled"
66-
][0],
67-
"Estimated Location feature requires "
68-
+ "WHOIS Lookup feature to be enabled.",
69-
)
70-
except AssertionError:
71-
self.fail("ValidationError message not equal to expected message.")
62+
self.assertEqual(
63+
context_manager.exception.message_dict["estimated_location_enabled"][0],
64+
"Estimated Location feature requires "
65+
+ "WHOIS Lookup feature to be enabled.",
66+
)
7267

7368
with self.subTest(
7469
"Test Estimated Location field visible on admin when "
@@ -116,13 +111,10 @@ def test_estimated_location_field(self):
116111
org.refresh_from_db()
117112
with self.assertRaises(ValidationError) as context_manager:
118113
self._create_location(organization=org, is_estimated=True)
119-
try:
120-
self.assertEqual(
121-
context_manager.exception.message_dict["is_estimated"][0],
122-
"Estimated Location feature required to be configured.",
123-
)
124-
except AssertionError:
125-
self.fail("ValidationError message not equal to expected message.")
114+
self.assertEqual(
115+
context_manager.exception.message_dict["is_estimated"][0],
116+
"Estimated Location feature required to be configured.",
117+
)
126118

127119
@mock.patch.object(config_app_settings, "WHOIS_CONFIGURED", True)
128120
def test_estimated_location_admin(self):
@@ -865,7 +857,8 @@ def test_estimated_location_filter_admin(self):
865857
" when WHOIS not configured"
866858
):
867859
for i in ["estimated", "outdoor", "indoor"]:
868-
response = self.client.get(path, {"with_geo": i})
869-
self.assertContains(response, estimated_device.id)
870-
self.assertContains(response, outdoor_device.id)
871-
self.assertContains(response, indoor_device.id)
860+
with self.subTest(filter_value=i):
861+
response = self.client.get(path, {"with_geo": i})
862+
self.assertContains(response, estimated_device.id)
863+
self.assertContains(response, outdoor_device.id)
864+
self.assertContains(response, indoor_device.id)

0 commit comments

Comments
 (0)