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

Move prepare_cloned_fields() to utilities.querydict

This commit is contained in:
Jeremy Stretch
2024-03-21 14:24:10 -04:00
parent a49eb80f9e
commit 3b4898adea
4 changed files with 28 additions and 29 deletions

View File

@ -18,8 +18,7 @@ from utilities.error_handlers import handle_protectederror
from utilities.exceptions import AbortRequest, PermissionsViolation
from utilities.forms import ConfirmationForm, restrict_form_fields
from utilities.permissions import get_permission_for_model
from utilities.querydict import normalize_querydict
from utilities.utils import prepare_cloned_fields
from utilities.querydict import normalize_querydict, prepare_cloned_fields
from utilities.views import GetReturnURLMixin, get_viewname
from .base import BaseObjectView
from .mixins import ActionsMixin, TableMixin

View File

@ -1,9 +1,12 @@
from urllib.parse import urlencode
from django.http import QueryDict
from django.utils.datastructures import MultiValueDict
__all__ = (
'dict_to_querydict',
'normalize_querydict',
'prepare_cloned_fields',
)
@ -36,3 +39,26 @@ def normalize_querydict(querydict):
return {
k: v if len(v) > 1 else v[0] for k, v in querydict.lists()
}
def prepare_cloned_fields(instance):
"""
Generate a QueryDict comprising attributes from an object's clone() method.
"""
# Generate the clone attributes from the instance
if not hasattr(instance, 'clone'):
return QueryDict(mutable=True)
attrs = instance.clone()
# Prepare QueryDict parameters
params = []
for key, value in attrs.items():
if type(value) in (list, tuple):
params.extend([(key, v) for v in value])
elif value not in (False, None):
params.append((key, value))
else:
params.append((key, ''))
# Return a QueryDict with the parameters
return QueryDict(urlencode(params), mutable=True)

View File

@ -4,7 +4,7 @@ from django.urls import NoReverseMatch, reverse
from core.models import ObjectType
from extras.models import Bookmark, ExportTemplate
from utilities.utils import prepare_cloned_fields
from utilities.querydict import prepare_cloned_fields
from utilities.views import get_viewname
__all__ = (

View File

@ -1,8 +1,5 @@
from urllib.parse import urlencode
from django.db.models import Count, ManyToOneRel, OuterRef, Subquery
from django.db.models.functions import Coalesce
from django.http import QueryDict
from django.utils import timezone
from django.utils.timezone import localtime
@ -69,29 +66,6 @@ def dict_to_filter_params(d, prefix=''):
return params
def prepare_cloned_fields(instance):
"""
Generate a QueryDict comprising attributes from an object's clone() method.
"""
# Generate the clone attributes from the instance
if not hasattr(instance, 'clone'):
return QueryDict(mutable=True)
attrs = instance.clone()
# Prepare querydict parameters
params = []
for key, value in attrs.items():
if type(value) in (list, tuple):
params.extend([(key, v) for v in value])
elif value not in (False, None):
params.append((key, value))
else:
params.append((key, ''))
# Return a QueryDict with the parameters
return QueryDict(urlencode(params), mutable=True)
def content_type_name(ct, include_app=True):
"""
Return a human-friendly ContentType name (e.g. "DCIM > Site").