mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 04:58:16 -06:00
Clean up obsolete logic for view/serializer resolution
This commit is contained in:
parent
3d24239b68
commit
f53a3aa42b
@ -31,24 +31,13 @@ def get_serializer_for_model(model, prefix=''):
|
|||||||
"""
|
"""
|
||||||
Dynamically resolve and return the appropriate serializer for a model.
|
Dynamically resolve and return the appropriate serializer for a model.
|
||||||
"""
|
"""
|
||||||
app_name, model_name = model._meta.label.split('.')
|
app_label, model_name = model._meta.label.split('.')
|
||||||
# TODO: Remove this logic
|
serializer_name = f'{app_label}.api.serializers.{prefix}{model_name}Serializer'
|
||||||
# Serializers for Django's auth models are in the users app
|
|
||||||
if app_name == 'auth':
|
|
||||||
app_name = 'users'
|
|
||||||
# Account for changes using Proxy model
|
|
||||||
if app_name == 'users':
|
|
||||||
if model_name == 'NetBoxUser':
|
|
||||||
model_name = 'User'
|
|
||||||
elif model_name == 'NetBoxGroup':
|
|
||||||
model_name = 'Group'
|
|
||||||
|
|
||||||
serializer_name = f'{app_name}.api.serializers.{prefix}{model_name}Serializer'
|
|
||||||
try:
|
try:
|
||||||
return dynamic_import(serializer_name)
|
return dynamic_import(serializer_name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise SerializerNotFound(
|
raise SerializerNotFound(
|
||||||
f"Could not determine serializer for {app_name}.{model_name} with prefix '{prefix}'"
|
f"Could not determine serializer for {app_label}.{model_name} with prefix '{prefix}'"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import json
|
import json
|
||||||
import nh3
|
|
||||||
import re
|
import re
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from itertools import count, groupby
|
from itertools import count, groupby
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
|
import nh3
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
from django.db.models import Count, ManyToOneRel, OuterRef, Subquery
|
from django.db.models import Count, ManyToOneRel, OuterRef, Subquery
|
||||||
@ -23,7 +24,6 @@ from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
|
|||||||
from extras.utils import is_taggable
|
from extras.utils import is_taggable
|
||||||
from netbox.config import get_config
|
from netbox.config import get_config
|
||||||
from netbox.plugins import PluginConfig
|
from netbox.plugins import PluginConfig
|
||||||
from urllib.parse import urlencode
|
|
||||||
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
|
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
|
||||||
from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
|
from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
|
||||||
|
|
||||||
@ -48,27 +48,16 @@ def get_viewname(model, action=None, rest_api=False):
|
|||||||
model_name = model._meta.model_name
|
model_name = model._meta.model_name
|
||||||
|
|
||||||
if rest_api:
|
if rest_api:
|
||||||
|
viewname = f'{app_label}-api:{model_name}'
|
||||||
if is_plugin:
|
if is_plugin:
|
||||||
viewname = f'plugins-api:{app_label}-api:{model_name}'
|
viewname = f'plugins-api:{viewname}'
|
||||||
else:
|
|
||||||
# TODO: Remove this logic
|
|
||||||
# Alter the app_label for group and user model_name to point to users app
|
|
||||||
if app_label == 'auth' and model_name in ['group', 'user']:
|
|
||||||
app_label = 'users'
|
|
||||||
if app_label == 'users' and model._meta.proxy and model_name in ['netboxuser', 'netboxgroup']:
|
|
||||||
model_name = model._meta.proxy_for_model._meta.model_name
|
|
||||||
|
|
||||||
viewname = f'{app_label}-api:{model_name}'
|
|
||||||
# Append the action, if any
|
|
||||||
if action:
|
if action:
|
||||||
viewname = f'{viewname}-{action}'
|
viewname = f'{viewname}-{action}'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
viewname = f'{app_label}:{model_name}'
|
viewname = f'{app_label}:{model_name}'
|
||||||
# Prepend the plugins namespace if this is a plugin model
|
|
||||||
if is_plugin:
|
if is_plugin:
|
||||||
viewname = f'plugins:{viewname}'
|
viewname = f'plugins:{viewname}'
|
||||||
# Append the action, if any
|
|
||||||
if action:
|
if action:
|
||||||
viewname = f'{viewname}_{action}'
|
viewname = f'{viewname}_{action}'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user