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

#5306: Introduce CSVContentTypeField for cable termination types

This commit is contained in:
Jeremy Stretch
2020-12-01 11:03:05 -05:00
parent 23b305847e
commit cadba74b1f
3 changed files with 24 additions and 12 deletions

View File

@@ -6,12 +6,11 @@ from io import StringIO
import django_filters
from django import forms
from django.forms.fields import JSONField as _JSONField, InvalidJSONInput
from django.core.exceptions import MultipleObjectsReturned
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db.models import Count
from django.forms import BoundField
from django.urls import reverse
from utilities.api import get_serializer_for_model
from utilities.choices import unpack_grouped_choices
from utilities.validators import EnhancedURLValidator
from . import widgets
@@ -21,6 +20,7 @@ from .utils import expand_alphanumeric_pattern, expand_ipaddress_pattern
__all__ = (
'CommentField',
'CSVChoiceField',
'CSVContentTypeField',
'CSVDataField',
'CSVModelChoiceField',
'DynamicModelChoiceField',
@@ -141,6 +141,19 @@ class CSVModelChoiceField(forms.ModelChoiceField):
)
class CSVContentTypeField(CSVModelChoiceField):
def to_python(self, value):
try:
app_label, model = value.split('.')
except ValueError:
raise forms.ValidationError(f'Object type must be specified as "<app>.<model>"')
try:
return self.queryset.get(app_label=app_label, model=model)
except ObjectDoesNotExist:
raise forms.ValidationError(f'Invalid object type')
class ExpandableNameField(forms.CharField):
"""
A field which allows for numeric range expansion