mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Merge pull request #4770 from netbox-community/3703-limit-tag-creation
Closes #3703: Restrict tag creation
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.inspectors import FieldInspector, NotHandled, PaginatorInspector, FilterInspector, SwaggerAutoSchema
|
||||
from drf_yasg.inspectors import FieldInspector, NotHandled, PaginatorInspector, SwaggerAutoSchema
|
||||
from drf_yasg.utils import get_serializer_ref_name
|
||||
from rest_framework.fields import ChoiceField
|
||||
from rest_framework.relations import ManyRelatedField
|
||||
from taggit_serializer.serializers import TagListSerializerField
|
||||
|
||||
from dcim.api.serializers import InterfaceSerializer as DeviceInterfaceSerializer
|
||||
from extras.api.customfields import CustomFieldsSerializer
|
||||
@@ -56,19 +55,6 @@ class SerializedPKRelatedFieldInspector(FieldInspector):
|
||||
return NotHandled
|
||||
|
||||
|
||||
class TagListFieldInspector(FieldInspector):
|
||||
def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs):
|
||||
SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, use_references, **kwargs)
|
||||
if isinstance(field, TagListSerializerField):
|
||||
child_schema = self.probe_field_inspectors(field.child, ChildSwaggerType, use_references)
|
||||
return SwaggerType(
|
||||
type=openapi.TYPE_ARRAY,
|
||||
items=child_schema,
|
||||
)
|
||||
|
||||
return NotHandled
|
||||
|
||||
|
||||
class CustomChoiceFieldInspector(FieldInspector):
|
||||
def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs):
|
||||
# this returns a callable which extracts title, description and other stuff
|
||||
|
@@ -12,6 +12,9 @@ class DummyQuerySet:
|
||||
def __init__(self, queryset):
|
||||
self._cache = [obj for obj in queryset.all()]
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._cache)
|
||||
|
||||
def all(self):
|
||||
return self._cache
|
||||
|
||||
|
@@ -14,7 +14,14 @@ def post_data(data):
|
||||
if value is None:
|
||||
ret[key] = ''
|
||||
elif type(value) in (list, tuple):
|
||||
ret[key] = value
|
||||
if value and hasattr(value[0], 'pk'):
|
||||
# Value is a list of instances
|
||||
ret[key] = [v.pk for v in value]
|
||||
else:
|
||||
ret[key] = value
|
||||
elif hasattr(value, 'pk'):
|
||||
# Value is an instance
|
||||
ret[key] = value.pk
|
||||
else:
|
||||
ret[key] = str(value)
|
||||
|
||||
|
@@ -6,8 +6,10 @@ from django.db.models import ForeignKey, ManyToManyField
|
||||
from django.forms.models import model_to_dict
|
||||
from django.test import Client, TestCase as _TestCase, override_settings
|
||||
from django.urls import reverse, NoReverseMatch
|
||||
from django.utils.text import slugify
|
||||
from netaddr import IPNetwork
|
||||
|
||||
from extras.models import Tag
|
||||
from users.models import ObjectPermission
|
||||
from utilities.permissions import resolve_permission_ct
|
||||
from .utils import disable_warnings, post_data
|
||||
@@ -49,7 +51,7 @@ class TestCase(_TestCase):
|
||||
obj_perm.object_types.add(ct)
|
||||
|
||||
#
|
||||
# Convenience methods
|
||||
# Custom assertions
|
||||
#
|
||||
|
||||
def assertHttpStatus(self, response, expected_status):
|
||||
@@ -75,7 +77,7 @@ class TestCase(_TestCase):
|
||||
|
||||
# TODO: Differentiate between tags assigned to the instance and a M2M field for tags (ex: ConfigContext)
|
||||
if key == 'tags':
|
||||
model_dict[key] = ','.join(sorted([tag.name for tag in value]))
|
||||
model_dict[key] = sorted(value)
|
||||
|
||||
# Convert ManyToManyField to list of instance PKs
|
||||
elif model_dict[key] and type(value) in (list, tuple) and hasattr(value[0], 'pk'):
|
||||
@@ -108,6 +110,19 @@ class TestCase(_TestCase):
|
||||
|
||||
self.assertDictEqual(model_dict, relevant_data)
|
||||
|
||||
#
|
||||
# Convenience methods
|
||||
#
|
||||
|
||||
@classmethod
|
||||
def create_tags(cls, *names):
|
||||
"""
|
||||
Create and return a Tag instance for each name given.
|
||||
"""
|
||||
tags = [Tag(name=name, slug=slugify(name)) for name in names]
|
||||
Tag.objects.bulk_create(tags)
|
||||
return tags
|
||||
|
||||
|
||||
#
|
||||
# UI Tests
|
||||
|
Reference in New Issue
Block a user