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:
@ -18,7 +18,7 @@ from netbox.choices import ButtonColorChoices
|
|||||||
from utilities.permissions import get_permission_for_model
|
from utilities.permissions import get_permission_for_model
|
||||||
from utilities.querydict import dict_to_querydict
|
from utilities.querydict import dict_to_querydict
|
||||||
from utilities.templatetags.builtins.filters import render_markdown
|
from utilities.templatetags.builtins.filters import render_markdown
|
||||||
from utilities.utils import content_type_identifier, content_type_name
|
from utilities.utils import object_type_identifier, object_type_name
|
||||||
from utilities.views import get_viewname
|
from utilities.views import get_viewname
|
||||||
from .utils import register_widget
|
from .utils import register_widget
|
||||||
|
|
||||||
@ -35,15 +35,15 @@ __all__ = (
|
|||||||
|
|
||||||
def get_object_type_choices():
|
def get_object_type_choices():
|
||||||
return [
|
return [
|
||||||
(content_type_identifier(ct), content_type_name(ct))
|
(object_type_identifier(ot), object_type_name(ot))
|
||||||
for ct in ObjectType.objects.public().order_by('app_label', 'model')
|
for ot in ObjectType.objects.public().order_by('app_label', 'model')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_bookmarks_object_type_choices():
|
def get_bookmarks_object_type_choices():
|
||||||
return [
|
return [
|
||||||
(content_type_identifier(ct), content_type_name(ct))
|
(object_type_identifier(ot), object_type_name(ot))
|
||||||
for ct in ObjectType.objects.with_feature('bookmarks').order_by('app_label', 'model')
|
for ot in ObjectType.objects.with_feature('bookmarks').order_by('app_label', 'model')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from netbox.search.utils import get_indexer
|
from netbox.search.utils import get_indexer
|
||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
from utilities.fields import RestrictedGenericForeignKey
|
from utilities.fields import RestrictedGenericForeignKey
|
||||||
from utilities.utils import content_type_identifier
|
from utilities.utils import object_type_identifier
|
||||||
from ..fields import CachedValueField
|
from ..fields import CachedValueField
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
@ -2,9 +2,10 @@ from django.contrib.contenttypes.models import ContentType
|
|||||||
from drf_spectacular.utils import extend_schema_field
|
from drf_spectacular.utils import extend_schema_field
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from core.models import ObjectType
|
||||||
from netbox.api.fields import ContentTypeField
|
from netbox.api.fields import ContentTypeField
|
||||||
from utilities.api import get_serializer_for_model
|
from utilities.api import get_serializer_for_model
|
||||||
from utilities.utils import content_type_identifier
|
from utilities.utils import object_type_identifier
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'GenericObjectSerializer',
|
'GenericObjectSerializer',
|
||||||
@ -27,9 +28,9 @@ class GenericObjectSerializer(serializers.Serializer):
|
|||||||
return model.objects.get(pk=data['object_id'])
|
return model.objects.get(pk=data['object_id'])
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
ct = ContentType.objects.get_for_model(instance)
|
object_type = ObjectType.objects.get_for_model(instance)
|
||||||
data = {
|
data = {
|
||||||
'object_type': content_type_identifier(ct),
|
'object_type': object_type_identifier(object_type),
|
||||||
'object_id': instance.pk,
|
'object_id': instance.pk,
|
||||||
}
|
}
|
||||||
if 'request' in self.context:
|
if 'request' in self.context:
|
||||||
|
@ -16,7 +16,7 @@ from extras.models import CachedValue, CustomField
|
|||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
from utilities.querysets import RestrictedPrefetch
|
from utilities.querysets import RestrictedPrefetch
|
||||||
from utilities.string import title
|
from utilities.string import title
|
||||||
from utilities.utils import content_type_identifier
|
from utilities.utils import object_type_identifier
|
||||||
from . import FieldTypes, LookupTypes, get_indexer
|
from . import FieldTypes, LookupTypes, get_indexer
|
||||||
|
|
||||||
DEFAULT_LOOKUP_TYPE = LookupTypes.PARTIAL
|
DEFAULT_LOOKUP_TYPE = LookupTypes.PARTIAL
|
||||||
@ -157,7 +157,7 @@ class CachedValueSearchBackend(SearchBackend):
|
|||||||
# related objects necessary to render the prescribed display attributes (display_attrs).
|
# related objects necessary to render the prescribed display attributes (display_attrs).
|
||||||
for object_type in object_types:
|
for object_type in object_types:
|
||||||
model = object_type.model_class()
|
model = object_type.model_class()
|
||||||
indexer = registry['search'].get(content_type_identifier(object_type))
|
indexer = registry['search'].get(object_type_identifier(object_type))
|
||||||
if not (display_attrs := getattr(indexer, 'display_attrs', None)):
|
if not (display_attrs := getattr(indexer, 'display_attrs', None)):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
from utilities.utils import content_type_identifier
|
from utilities.utils import object_type_identifier
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'get_indexer',
|
'get_indexer',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_indexer(content_type):
|
def get_indexer(object_type):
|
||||||
"""
|
"""
|
||||||
Return the registered search indexer for the given ContentType.
|
Return the registered search indexer for the given ContentType.
|
||||||
"""
|
"""
|
||||||
ct_identifier = content_type_identifier(content_type)
|
identifier = object_type_identifier(object_type)
|
||||||
return registry['search'].get(ct_identifier)
|
return registry['search'].get(identifier)
|
||||||
|
@ -20,7 +20,7 @@ from django_tables2.utils import Accessor
|
|||||||
from extras.choices import CustomFieldTypeChoices
|
from extras.choices import CustomFieldTypeChoices
|
||||||
from utilities.permissions import get_permission_for_model
|
from utilities.permissions import get_permission_for_model
|
||||||
from utilities.templatetags.builtins.filters import render_markdown
|
from utilities.templatetags.builtins.filters import render_markdown
|
||||||
from utilities.utils import content_type_identifier, content_type_name
|
from utilities.utils import object_type_identifier, object_type_name
|
||||||
from utilities.views import get_viewname
|
from utilities.views import get_viewname
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -339,12 +339,12 @@ class ContentTypeColumn(tables.Column):
|
|||||||
def render(self, value):
|
def render(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
return content_type_name(value, include_app=False)
|
return object_type_name(value, include_app=False)
|
||||||
|
|
||||||
def value(self, value):
|
def value(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
return content_type_identifier(value)
|
return object_type_identifier(value)
|
||||||
|
|
||||||
|
|
||||||
class ContentTypesColumn(tables.ManyToManyColumn):
|
class ContentTypesColumn(tables.ManyToManyColumn):
|
||||||
@ -358,11 +358,11 @@ class ContentTypesColumn(tables.ManyToManyColumn):
|
|||||||
super().__init__(separator=separator, *args, **kwargs)
|
super().__init__(separator=separator, *args, **kwargs)
|
||||||
|
|
||||||
def transform(self, obj):
|
def transform(self, obj):
|
||||||
return content_type_name(obj, include_app=False)
|
return object_type_name(obj, include_app=False)
|
||||||
|
|
||||||
def value(self, value):
|
def value(self, value):
|
||||||
return ','.join([
|
return ','.join([
|
||||||
content_type_identifier(ct) for ct in self.filter(value)
|
object_type_identifier(ot) for ot in self.filter(value)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
|
|
||||||
from utilities.utils import content_type_name
|
from utilities.utils import object_type_name
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'ContentTypeChoiceField',
|
'ContentTypeChoiceField',
|
||||||
@ -17,7 +17,7 @@ class ContentTypeChoiceMixin:
|
|||||||
|
|
||||||
def label_from_instance(self, obj):
|
def label_from_instance(self, obj):
|
||||||
try:
|
try:
|
||||||
return content_type_name(obj)
|
return object_type_name(obj)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return super().label_from_instance(obj)
|
return super().label_from_instance(obj)
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
|
|||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
from utilities.choices import unpack_grouped_choices
|
from utilities.choices import unpack_grouped_choices
|
||||||
from utilities.utils import content_type_identifier
|
from utilities.utils import object_type_identifier
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'CSVChoiceField',
|
'CSVChoiceField',
|
||||||
@ -86,7 +86,7 @@ class CSVContentTypeField(CSVModelChoiceField):
|
|||||||
STATIC_CHOICES = True
|
STATIC_CHOICES = True
|
||||||
|
|
||||||
def prepare_value(self, value):
|
def prepare_value(self, value):
|
||||||
return content_type_identifier(value)
|
return object_type_identifier(value)
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
@ -115,4 +115,4 @@ class CSVMultipleContentTypeField(forms.ModelMultipleChoiceField):
|
|||||||
app_label, model = name.split('.')
|
app_label, model = name.split('.')
|
||||||
ct_filter |= Q(app_label=app_label, model=model)
|
ct_filter |= Q(app_label=app_label, model=model)
|
||||||
return list(ContentType.objects.filter(ct_filter).values_list('pk', flat=True))
|
return list(ContentType.objects.filter(ct_filter).values_list('pk', flat=True))
|
||||||
return content_type_identifier(value)
|
return object_type_identifier(value)
|
||||||
|
@ -13,7 +13,7 @@ from taggit.managers import TaggableManager
|
|||||||
from core.models import ObjectType
|
from core.models import ObjectType
|
||||||
from users.models import ObjectPermission
|
from users.models import ObjectPermission
|
||||||
from utilities.permissions import resolve_permission_type
|
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
|
from .utils import extract_form_failures
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -114,7 +114,7 @@ class ModelTestCase(TestCase):
|
|||||||
if value and type(field) in (ManyToManyField, TaggableManager):
|
if value and type(field) in (ManyToManyField, TaggableManager):
|
||||||
|
|
||||||
if field.related_model in (ContentType, ObjectType) and api:
|
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:
|
else:
|
||||||
model_dict[key] = sorted([obj.pk for obj in value])
|
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>
|
# Replace ContentType numeric IDs with <app_label>.<model>
|
||||||
if type(getattr(instance, key)) in (ContentType, ObjectType):
|
if type(getattr(instance, key)) in (ContentType, ObjectType):
|
||||||
ct = ObjectType.objects.get(pk=value)
|
object_type = ObjectType.objects.get(pk=value)
|
||||||
model_dict[key] = content_type_identifier(ct)
|
model_dict[key] = object_type_identifier(object_type)
|
||||||
|
|
||||||
# Convert IPNetwork instances to strings
|
# Convert IPNetwork instances to strings
|
||||||
elif type(value) is IPNetwork:
|
elif type(value) is IPNetwork:
|
||||||
|
@ -16,27 +16,27 @@ def dynamic_import(name):
|
|||||||
return mod
|
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:
|
try:
|
||||||
meta = ct.model_class()._meta
|
meta = object_type.model_class()._meta
|
||||||
app_label = title(meta.app_config.verbose_name)
|
app_label = title(meta.app_config.verbose_name)
|
||||||
model_name = title(meta.verbose_name)
|
model_name = title(meta.verbose_name)
|
||||||
if include_app:
|
if include_app:
|
||||||
return f'{app_label} > {model_name}'
|
return f'{app_label} > {model_name}'
|
||||||
return model_name
|
return model_name
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Model no longer exists
|
# Model does not exist
|
||||||
return f'{ct.app_label} > {ct.model}'
|
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():
|
def local_now():
|
||||||
|
Reference in New Issue
Block a user