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

Rename content_type_identifier() and content_type_name()

This commit is contained in:
Jeremy Stretch
2024-03-21 14:44:59 -04:00
parent ae8df77cc8
commit 8ad73e3f90
10 changed files with 38 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
from django import forms
from utilities.utils import content_type_name
from utilities.utils import object_type_name
__all__ = (
'ContentTypeChoiceField',
@@ -17,7 +17,7 @@ class ContentTypeChoiceMixin:
def label_from_instance(self, obj):
try:
return content_type_name(obj)
return object_type_name(obj)
except AttributeError:
return super().label_from_instance(obj)

View File

@@ -5,7 +5,7 @@ from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db.models import Q
from utilities.choices import unpack_grouped_choices
from utilities.utils import content_type_identifier
from utilities.utils import object_type_identifier
__all__ = (
'CSVChoiceField',
@@ -86,7 +86,7 @@ class CSVContentTypeField(CSVModelChoiceField):
STATIC_CHOICES = True
def prepare_value(self, value):
return content_type_identifier(value)
return object_type_identifier(value)
def to_python(self, value):
if not value:
@@ -115,4 +115,4 @@ class CSVMultipleContentTypeField(forms.ModelMultipleChoiceField):
app_label, model = name.split('.')
ct_filter |= Q(app_label=app_label, model=model)
return list(ContentType.objects.filter(ct_filter).values_list('pk', flat=True))
return content_type_identifier(value)
return object_type_identifier(value)

View File

@@ -13,7 +13,7 @@ from taggit.managers import TaggableManager
from core.models import ObjectType
from users.models import ObjectPermission
from utilities.permissions import resolve_permission_type
from utilities.utils import content_type_identifier
from utilities.utils import object_type_identifier
from .utils import extract_form_failures
__all__ = (
@@ -114,7 +114,7 @@ class ModelTestCase(TestCase):
if value and type(field) in (ManyToManyField, TaggableManager):
if field.related_model in (ContentType, ObjectType) and api:
model_dict[key] = sorted([content_type_identifier(ct) for ct in value])
model_dict[key] = sorted([object_type_identifier(ot) for ot in value])
else:
model_dict[key] = sorted([obj.pk for obj in value])
@@ -122,8 +122,8 @@ class ModelTestCase(TestCase):
# Replace ContentType numeric IDs with <app_label>.<model>
if type(getattr(instance, key)) in (ContentType, ObjectType):
ct = ObjectType.objects.get(pk=value)
model_dict[key] = content_type_identifier(ct)
object_type = ObjectType.objects.get(pk=value)
model_dict[key] = object_type_identifier(object_type)
# Convert IPNetwork instances to strings
elif type(value) is IPNetwork:

View File

@@ -16,27 +16,27 @@ def dynamic_import(name):
return mod
def content_type_name(ct, include_app=True):
def object_type_name(object_type, include_app=True):
"""
Return a human-friendly ContentType name (e.g. "DCIM > Site").
Return a human-friendly ObjectType name (e.g. "DCIM > Site").
"""
try:
meta = ct.model_class()._meta
meta = object_type.model_class()._meta
app_label = title(meta.app_config.verbose_name)
model_name = title(meta.verbose_name)
if include_app:
return f'{app_label} > {model_name}'
return model_name
except AttributeError:
# Model no longer exists
return f'{ct.app_label} > {ct.model}'
# Model does not exist
return f'{object_type.app_label} > {object_type.model}'
def content_type_identifier(ct):
def object_type_identifier(object_type):
"""
Return a "raw" ContentType identifier string suitable for bulk import/export (e.g. "dcim.site").
Return a "raw" ObjectType identifier string suitable for bulk import/export (e.g. "dcim.site").
"""
return f'{ct.app_label}.{ct.model}'
return f'{object_type.app_label}.{object_type.model}'
def local_now():