1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Merge branch 'develop' into 3840-limit-vlan-choices

This commit is contained in:
hSaria
2020-01-24 20:54:56 +00:00
committed by GitHub
39 changed files with 1209 additions and 182 deletions

View File

@@ -13,7 +13,6 @@ from rest_framework.response import Response
from rest_framework.serializers import Field, ModelSerializer, ValidationError
from rest_framework.viewsets import ModelViewSet as _ModelViewSet, ViewSet
from utilities.choices import ChoiceSet
from .utils import dict_to_filter_params, dynamic_import

View File

@@ -18,7 +18,7 @@ class ChoiceSet(metaclass=ChoiceSetMeta):
@classmethod
def values(cls):
return [c[0] for c in cls.CHOICES]
return [c[0] for c in unpack_grouped_choices(cls.CHOICES)]
@classmethod
def as_dict(cls):

View File

@@ -0,0 +1,50 @@
from django.test import TestCase
from utilities.choices import ChoiceSet
class ExampleChoices(ChoiceSet):
CHOICE_A = 'a'
CHOICE_B = 'b'
CHOICE_C = 'c'
CHOICE_1 = 1
CHOICE_2 = 2
CHOICE_3 = 3
CHOICES = (
('Letters', (
(CHOICE_A, 'A'),
(CHOICE_B, 'B'),
(CHOICE_C, 'C'),
)),
('Digits', (
(CHOICE_1, 'One'),
(CHOICE_2, 'Two'),
(CHOICE_3, 'Three'),
)),
)
LEGACY_MAP = {
CHOICE_A: 101,
CHOICE_B: 102,
CHOICE_C: 103,
CHOICE_1: 201,
CHOICE_2: 202,
CHOICE_3: 203,
}
class ChoiceSetTestCase(TestCase):
def test_values(self):
self.assertListEqual(ExampleChoices.values(), ['a', 'b', 'c', 1, 2, 3])
def test_as_dict(self):
self.assertEqual(ExampleChoices.as_dict(), {
'a': 'A', 'b': 'B', 'c': 'C', 1: 'One', 2: 'Two', 3: 'Three'
})
def test_slug_to_id(self):
self.assertEqual(ExampleChoices.slug_to_id('a'), 101)
def test_id_to_slug(self):
self.assertEqual(ExampleChoices.id_to_slug(101), 'a')

View File

@@ -1,6 +1,6 @@
import re
from django.core.validators import _lazy_re_compile, BaseValidator, URLValidator
from django.core.validators import _lazy_re_compile, URLValidator
class EnhancedURLValidator(URLValidator):
@@ -26,19 +26,3 @@ class EnhancedURLValidator(URLValidator):
r'(?:[/?#][^\s]*)?' # Path
r'\Z', re.IGNORECASE)
schemes = AnyURLScheme()
class MaxPrefixLengthValidator(BaseValidator):
message = 'The prefix length must be less than or equal to %(limit_value)s.'
code = 'max_prefix_length'
def compare(self, a, b):
return a.prefixlen > b
class MinPrefixLengthValidator(BaseValidator):
message = 'The prefix length must be greater than or equal to %(limit_value)s.'
code = 'min_prefix_length'
def compare(self, a, b):
return a.prefixlen < b