Explorar el Código

Handle malformed MAPS_URL format strings; add Decimal and fallback tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Brian Tiemann hace 2 días
padre
commit
66e5c2941c
Se han modificado 2 ficheros con 28 adiciones y 1 borrados
  1. 24 0
      netbox/netbox/tests/test_ui.py
  2. 4 1
      netbox/netbox/ui/attrs.py

+ 24 - 0
netbox/netbox/tests/test_ui.py

@@ -1,3 +1,4 @@
+from decimal import Decimal
 from types import SimpleNamespace
 from types import SimpleNamespace
 
 
 from django.test import RequestFactory, TestCase
 from django.test import RequestFactory, TestCase
@@ -459,6 +460,29 @@ class GPSCoordinatesAttrTestCase(TestCase):
         url = attrs.GPSCoordinatesAttr._build_coords_url('https://example.com/?lon={lon}', 48.858, 2.294)
         url = attrs.GPSCoordinatesAttr._build_coords_url('https://example.com/?lon={lon}', 48.858, 2.294)
         self.assertEqual(url, 'https://example.com/?lon=2.294')
         self.assertEqual(url, 'https://example.com/?lon=2.294')
 
 
+    def test_build_coords_url_unknown_placeholder_falls_back_to_legacy(self):
+        # Unknown placeholder: should not raise, fall back to appending lat,lon
+        url = attrs.GPSCoordinatesAttr._build_coords_url('https://example.com/?q={unknown}', 48.858, 2.294)
+        self.assertEqual(url, 'https://example.com/?q={unknown}48.858,2.294')
+
+    def test_build_coords_url_decimal_values_no_locale_separator(self):
+        # Decimal field values must format with '.' as the decimal separator regardless of locale;
+        # a locale-style comma separator would produce e.g. '48,858258' and break the URL
+        url = attrs.GPSCoordinatesAttr._build_coords_url(
+            'https://maps.google.com/?q=',
+            Decimal('48.858258'),
+            Decimal('2.294498'),
+        )
+        self.assertEqual(url, 'https://maps.google.com/?q=48.858258,2.294498')
+
+    def test_build_coords_url_decimal_with_placeholders_no_locale_separator(self):
+        url = attrs.GPSCoordinatesAttr._build_coords_url(
+            'https://www.openstreetmap.org/?mlat={lat}&mlon={lon}',
+            Decimal('48.858258'),
+            Decimal('2.294498'),
+        )
+        self.assertEqual(url, 'https://www.openstreetmap.org/?mlat=48.858258&mlon=2.294498')
+
 
 
 class DateTimeAttrTestCase(TestCase):
 class DateTimeAttrTestCase(TestCase):
 
 

+ 4 - 1
netbox/netbox/ui/attrs.py

@@ -513,7 +513,10 @@ class GPSCoordinatesAttr(MapURLMixin, ObjectAttribute):
     @staticmethod
     @staticmethod
     def _build_coords_url(map_url, latitude, longitude):
     def _build_coords_url(map_url, latitude, longitude):
         if '{lat}' in map_url or '{lon}' in map_url:
         if '{lat}' in map_url or '{lon}' in map_url:
-            return map_url.format(lat=latitude, lon=longitude)
+            try:
+                return map_url.format(lat=latitude, lon=longitude)
+            except (KeyError, IndexError, ValueError):
+                pass
         return f'{map_url}{latitude},{longitude}'
         return f'{map_url}{latitude},{longitude}'