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

Move object_type_identifier() & object_type_name() to utilities.object_types

This commit is contained in:
Jeremy Stretch
2024-03-21 14:51:50 -04:00
parent 8ad73e3f90
commit 6ac700e43f
11 changed files with 37 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
from django import forms
from utilities.utils import object_type_name
from utilities.object_types import object_type_name
__all__ = (
'ContentTypeChoiceField',

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 object_type_identifier
from utilities.object_types import object_type_identifier
__all__ = (
'CSVChoiceField',

View File

@@ -0,0 +1,29 @@
from .string import title
__all__ = (
'object_type_identifier',
'object_type_name',
)
def object_type_identifier(object_type):
"""
Return a "raw" ObjectType identifier string suitable for bulk import/export (e.g. "dcim.site").
"""
return f'{object_type.app_label}.{object_type.model}'
def object_type_name(object_type, include_app=True):
"""
Return a human-friendly ObjectType name (e.g. "DCIM > Site").
"""
try:
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 does not exist
return f'{object_type.app_label} > {object_type.model}'

View File

@@ -12,8 +12,8 @@ from taggit.managers import TaggableManager
from core.models import ObjectType
from users.models import ObjectPermission
from utilities.object_types import object_type_identifier
from utilities.permissions import resolve_permission_type
from utilities.utils import object_type_identifier
from .utils import extract_form_failures
__all__ = (

View File

@@ -2,8 +2,6 @@ from django.db.models import ManyToOneRel
from django.utils import timezone
from django.utils.timezone import localtime
from .string import title
def dynamic_import(name):
"""
@@ -16,29 +14,6 @@ def dynamic_import(name):
return mod
def object_type_name(object_type, include_app=True):
"""
Return a human-friendly ObjectType name (e.g. "DCIM > Site").
"""
try:
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 does not exist
return f'{object_type.app_label} > {object_type.model}'
def object_type_identifier(object_type):
"""
Return a "raw" ObjectType identifier string suitable for bulk import/export (e.g. "dcim.site").
"""
return f'{object_type.app_label}.{object_type.model}'
def local_now():
"""
Return the current date & time in the system timezone.