Skip to content

Commit e34cb1e

Browse files
committed
[chores] More fixes and small improvements suggested by @coderabbitai
1 parent 1bdfcb3 commit e34cb1e

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

openwisp_controller/config/base/whois.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def clean(self):
8181
except ValueError as e:
8282
raise ValidationError(
8383
{"cidr": _("Invalid CIDR format: %(error)s") % {"error": str(e)}}
84-
)
84+
) from e
8585
if self.coordinates:
8686
if not (-90 <= self.coordinates.y <= 90):
8787
raise ValidationError(
@@ -153,8 +153,13 @@ def _location_name(self):
153153
if address:
154154
parts = [part.strip() for part in address.split(",")[:2] if part.strip()]
155155
location = ", ".join(parts)
156-
return _("{} ~Estimated Location: {}~".format(location, self.ip_address))
157-
return _("Estimated Location: {}".format(self.ip_address))
156+
# Use named placeholders so translators receive the template
157+
return _("%(location)s ~Estimated Location: %(ip)s~") % {
158+
"location": location,
159+
"ip": self.ip_address,
160+
}
161+
# Use named placeholder for consistency
162+
return _("Estimated Location: %(ip)s") % {"ip": self.ip_address}
158163

159164
def _get_defaults_for_estimated_location(self):
160165
"""

openwisp_controller/config/management/commands/clear_last_ip.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ def handle(self, *_args, **options):
3636
raise CommandError("Operation cancelled by user.")
3737

3838
devices = (
39-
Device.objects.filter(_is_deactivated=False)
40-
.select_related("organization__config_settings")
41-
.only("last_ip", "organization__config_settings", "key")
39+
Device.objects.filter(_is_deactivated=False).select_related(
40+
"organization__config_settings"
41+
)
42+
# include the FK field 'organization' in .only() so the related
43+
# `organization__config_settings` traversal is not deferred
44+
.only("last_ip", "organization", "key")
4245
)
4346
# Filter out devices that have WHOIS information for their last IP
4447
devices = devices.exclude(last_ip=None).exclude(

openwisp_controller/config/whois/service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def is_valid_public_ip_address(ip):
7171
try:
7272
return ip and ip_addr(ip).is_global
7373
except ValueError:
74+
# ip_address() from the stdlib raises ValueError for malformed strings
7475
return False
7576

7677
@staticmethod

openwisp_controller/geo/base/models.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from typing import ClassVar
23

34
from django.contrib.gis.db import models
45
from django.core.exceptions import ValidationError
@@ -15,7 +16,11 @@
1516

1617

1718
class BaseLocation(OrgMixin, AbstractLocation):
18-
_changed_checked_fields = ["is_estimated", "address", "geometry"]
19+
_changed_checked_fields: ClassVar[list[str]] = [
20+
"is_estimated",
21+
"address",
22+
"geometry",
23+
]
1924

2025
is_estimated = models.BooleanField(
2126
default=False,
@@ -87,8 +92,8 @@ def save(self, *args, _set_estimated=False, **kwargs):
8792
if not _set_estimated and (address_changed or geometry_changed):
8893
self.is_estimated = False
8994
if self.name:
90-
# remove estimated status between '~'
91-
self.name = re.sub(r"~[^~]*~", "", self.name)
95+
# remove estimated status between '~' and trim extra spaces
96+
self.name = re.sub(r"~[^~]*~", "", self.name).strip()
9297
changed_fields = {"is_estimated", "name"}
9398
# Manual changes to is_estimated discarded if feature not enabled
9499
elif self._initial_is_estimated is not models.DEFERRED:

0 commit comments

Comments
 (0)