mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-21 08:21:48 +02:00
Merge branch 'develop' into 4164-object-list-template
This commit is contained in:
@@ -27,3 +27,14 @@ COLOR_CHOICES = (
|
||||
('111111', 'Black'),
|
||||
('ffffff', 'White'),
|
||||
)
|
||||
|
||||
# Keys for PostgreSQL advisory locks. These are arbitrary bigints used by
|
||||
# the advisory_lock contextmanager. When a lock is acquired,
|
||||
# one of these keys will be used to identify said lock.
|
||||
#
|
||||
# When adding a new key, pick something arbitrary and unique so
|
||||
# that it is easily searchable in query logs.
|
||||
ADVISORY_LOCK_KEYS = {
|
||||
'available-prefixes': 100100,
|
||||
'available-ips': 100200,
|
||||
}
|
||||
|
||||
@@ -309,12 +309,17 @@ class APISelect(SelectWithDisabled):
|
||||
|
||||
def add_additional_query_param(self, name, value):
|
||||
"""
|
||||
Add details for an additional query param in the form of a data-* attribute.
|
||||
Add details for an additional query param in the form of a data-* JSON-encoded list attribute.
|
||||
|
||||
:param name: The name of the query param
|
||||
:param value: The value of the query param
|
||||
"""
|
||||
self.attrs['data-additional-query-param-{}'.format(name)] = value
|
||||
key = 'data-additional-query-param-{}'.format(name)
|
||||
|
||||
values = json.loads(self.attrs.get(key, '[]'))
|
||||
values.append(value)
|
||||
|
||||
self.attrs[key] = json.dumps(values)
|
||||
|
||||
def add_conditional_query_param(self, condition, value):
|
||||
"""
|
||||
|
||||
@@ -10,7 +10,7 @@ INTERFACE_NAME_REGEX = r'(^(?P<type>[^\d\.:]+)?)' \
|
||||
r'(.(?P<vc>\d+)$)?'
|
||||
|
||||
|
||||
def naturalize(value, max_length=None, integer_places=8):
|
||||
def naturalize(value, max_length, integer_places=8):
|
||||
"""
|
||||
Take an alphanumeric string and prepend all integers to `integer_places` places to ensure the strings
|
||||
are ordered naturally. For example:
|
||||
@@ -39,10 +39,10 @@ def naturalize(value, max_length=None, integer_places=8):
|
||||
output.append(segment)
|
||||
ret = ''.join(output)
|
||||
|
||||
return ret[:max_length] if max_length else ret
|
||||
return ret[:max_length]
|
||||
|
||||
|
||||
def naturalize_interface(value, max_length=None):
|
||||
def naturalize_interface(value, max_length):
|
||||
"""
|
||||
Similar in nature to naturalize(), but takes into account a particular naming format adapted from the old
|
||||
InterfaceManager.
|
||||
@@ -68,7 +68,7 @@ def naturalize_interface(value, max_length=None):
|
||||
if match.group('type') is not None:
|
||||
output.append(match.group('type'))
|
||||
|
||||
# Finally, append any remaining fields, left-padding to eight digits each.
|
||||
# Finally, append any remaining fields, left-padding to six digits each.
|
||||
for part_name in ('id', 'channel', 'vc'):
|
||||
part = match.group(part_name)
|
||||
if part is not None:
|
||||
@@ -77,4 +77,4 @@ def naturalize_interface(value, max_length=None):
|
||||
output.append('000000')
|
||||
|
||||
ret = ''.join(output)
|
||||
return ret[:max_length] if max_length else ret
|
||||
return ret[:max_length]
|
||||
|
||||
49
netbox/utilities/tests/test_ordering.py
Normal file
49
netbox/utilities/tests/test_ordering.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from utilities.ordering import naturalize, naturalize_interface
|
||||
|
||||
|
||||
class NaturalizationTestCase(TestCase):
|
||||
"""
|
||||
Validate the operation of the functions which generate values suitable for natural ordering.
|
||||
"""
|
||||
def test_naturalize(self):
|
||||
|
||||
data = (
|
||||
# Original, naturalized
|
||||
('abc', 'abc'),
|
||||
('123', '00000123'),
|
||||
('abc123', 'abc00000123'),
|
||||
('123abc', '00000123abc'),
|
||||
('123abc456', '00000123abc00000456'),
|
||||
('abc123def', 'abc00000123def'),
|
||||
('abc123def456', 'abc00000123def00000456'),
|
||||
)
|
||||
|
||||
for origin, naturalized in data:
|
||||
self.assertEqual(naturalize(origin, max_length=50), naturalized)
|
||||
|
||||
def test_naturalize_max_length(self):
|
||||
self.assertEqual(naturalize('abc123def456', max_length=10), 'abc0000012')
|
||||
|
||||
def test_naturalize_interface(self):
|
||||
|
||||
data = (
|
||||
# Original, naturalized
|
||||
('Gi', '9999999999999999Gi000000000000000000'),
|
||||
('Gi1', '9999999999999999Gi000001000000000000'),
|
||||
('Gi1/2', '0001999999999999Gi000002000000000000'),
|
||||
('Gi1/2/3', '0001000299999999Gi000003000000000000'),
|
||||
('Gi1/2/3/4', '0001000200039999Gi000004000000000000'),
|
||||
('Gi1/2/3/4/5', '0001000200030004Gi000005000000000000'),
|
||||
('Gi1/2/3/4/5:6', '0001000200030004Gi000005000006000000'),
|
||||
('Gi1/2/3/4/5:6.7', '0001000200030004Gi000005000006000007'),
|
||||
('Gi1:2', '9999999999999999Gi000001000002000000'),
|
||||
('Gi1:2.3', '9999999999999999Gi000001000002000003'),
|
||||
)
|
||||
|
||||
for origin, naturalized in data:
|
||||
self.assertEqual(naturalize_interface(origin, max_length=50), naturalized)
|
||||
|
||||
def test_naturalize_interface_max_length(self):
|
||||
self.assertEqual(naturalize_interface('Gi1/2/3', max_length=20), '0001000299999999Gi00')
|
||||
Reference in New Issue
Block a user